While creating linked lists how do I assign value for a node? [closed]
#include <iostream>
using namespace std;
int main()
{
struct list
{
string name;
int age;
double height;
list *next;
};
list *first,*temp,*temp2;
for (int i=0 ;i<4;i++)
{
list *newlist;
newlist = new list;
cout << " Enter the name : ";
cin >> newlist->name;
cout << " Enter the age : ";
cin >> newlist->age;
cout << " Enter the height : ";
cin >> newlist->height;
cout << " Name is: " << newlist->name << " " ;
cout << " Age is: " << newlist->age << " ";
cout << " Height is: " << newlist->height <<endl;
}
{
list *newlist1;
newlist1 = new list;
newlist1->name = "Steve";
newlist1->age = 23;
newlist1->height = 2.3;
newlist1->next=temp2;
temp->next=newlist1;
newlist1->next = temp2;
temp->next = newlist1;
temp2 = newlist1->next;
temp2->next = newlist1->next;
delete temp2;
cout << " Name is: " << newlist1->name << " ";
cout << " Age is: " << newlist1->age << " ";
cout << " Height is: " << newlist1->height;
}
}
Basically, what I'm doing is creating a linked list and inserting a new node in between node 2 and node 3 and deleting node number 3 out of 4 nodes (note for loop is 4 times). And the next code after the loop is where I tried using code for insertion of a new node.
But after executing it says incompatible types in assignment of 'int' to char[20]'
I don't understand that.
Also, I wanted to know if my code is correct for the above intention.
I took temp2 to be the 3rd code by linking the newnode to the next node and temp to be the second node...
So can someone explain what the error meant so I can get it right? Thank you!
I did your homework, spend some attention on the comments:
#include <iostream>
// you forgot to include string!
#include <string>
using namespace std;
// type definitions usually go outside the main function!
struct list
{
string name;
int age;
double height;
list *next;
};
int main()
{
// mark the beginning with NULL!
list *first = NULL,*temp,*temp2;
for (int i=0 ;i<4;i++)
{
list *newlist;
newlist = new list;
cout << " Enter the name : ";
cin >> newlist->name;
cout << " Enter the age : ";
cin >> newlist->age;
cout << " Enter the height : ";
cin >> newlist->height;
cout << " Name is: " << newlist->name << " " ;
cout << " Age is: " << newlist->age << " ";
cout << " Height is: " << newlist->height <<endl;
// is this the first node?
if(first == NULL) {
// yes, so set the first node
first = newlist;
} else {
// no, set this node as the next node of the previous node!
temp->next = newlist;
}
// set temp to the end of the list for next iteration
temp = newlist;
}
// mark ending of list with NULL!
temp->next = NULL;
// creating extra node
list *newlist1;
newlist1 = new list;
newlist1->name = "Steve";
newlist1->age = 23;
newlist1->height = 2.3;
// insert between 2 and 3
// temp2 holds node 3
temp2 = first->next->next;
// set the "next" pointer of element 2 to the new node
first->next->next = newlist1;
// append the rest of the old tail to the new node
newlist1->next=temp2;
// delete node 4
// why 4? because 4 was the old 3 ! If we deleted the current node 3
// we would delete the node that we just have inserted!
//temp2 holds node 4
temp2 = first->next->next->next;
// link node 3 to 5
first->next->next->next = temp2->next;
// delete node 4
delete temp2;
// set to beginning of list
temp = first;
// separator
cout<<"---------------"<<endl;
// output the list to make sure it's correct!
while(temp != NULL) {
cout << " Name is: " << temp->name << " ";
cout << " Age is: " << temp->age << " ";
cout << " Height is: " << temp->height<<endl;
temp = temp->next;
}
}
You have to alloc memory for temp2, because you're deleting tmp2 and it didn't have got memory allocated...
Why don't you use classes to handle all that "repetitive" stuff for you?
struct NODE
{
string name;
int age;
double height;
NODE * next;
};
class LIST
{
private:
LIST();
~LIST();
public:
NODE* first = NULL; // *should* be private, but what the heck; don't edit this
unsigned long nodes = 0; // *should* be private, but what the heck; don't edit this
NODE* get(unsigned long);
NODE* insert(NODE*, unsigned long);
void destroy(unsigned long);
};
// Your constructor executed at object initialization.
LIST::LIST()
{
first = new NODE;
nodes = 1;
}
// Your destructor executed at object destruction. (Duh? ;) )
LIST::~LIST()
{
while(nodes > 0)
{
destroy(nodes - 1);
}
}
// gets the node at the specified index ("x")
NODE* LIST::get(unsigned long x)
{
NODE* ret;
ret = first;
// Illegal!
if(x >= nodes)
return(ret);
for(unsigned long i = 0; i < x; i++)
ret = ret->next;
return(ret);
}
// inserts the node before the specified "idx" in the list
NODE* LIST::insert(NODE* val, unsigned long idx = nodes)
{
if(idx > nodes)
return(val);
else if(idx == nodes)
val->next = NULL;
else
val->next = get(idx);
if(idx > 0)
{
// You can probably optimize this easily.
// HINT: Why use 2 get()s when you can do it in one?
NODE* prev;
prev = get(idx - 1);
prev->next = val;
}
else
{
first = val;
}
return(val);
}
void LIST::destroy(unsigned long idx)
{
if(idx >= nodes)
return;
NODE* toDel;
toDel = get(idx);
if(idx == 0)
{
first = NULL;
}
else
{
NODE* prev;
prev = get(idx - 1);
if((idx + 1) < nodes)
prev->next = get(idx + 1);
else
prev->next = NULL;
}
delete toDel;
nodes--;
}
Now you just do something similar to this (I didn't understand what you wanted, so I'm assuming it's this):
LIST list;
NODE* nTemp;
NODE* nTemp2;
for(int i = 0; i < 4; i++)
{
nTemp = new NODE;
cout << " Enter the name: ";
cin >> nTemp->name;
cout << " Enter the age: ";
cin >> nTemp->age;
cout << " Enter the height: ";
cin >> nTemp->height;
cout << " Name is: " << nTemp->name << " ";
cout << " Age is: " << nTemp->age << " ";
cout << " Height is: " << nTemp->height << endl;
list.insert(nTemp);
}
nTemp = new NODE;
nTemp->name = "Steve";
nTemp->age = 23;
nTemp->height = 2.3;
// Insert nTemp between "node" 2 and 3. (Assuming "node" numbering starts with 1.)
// So between list.first->next and list.first->next->next
list.insert(nTemp, 2);
// Display everything!
cout << "Displaying contents of <list>" << endl;
for(unsigned long i = 0; i < list.nodes; i++)
{
cout << "Name is: " << list.get(i)->name << " ";
cout << "Age is: " << list.get(i)->age << " ";
cout << "Height is: " << list.get(i)->height << endl;
}
Have fun!
精彩评论