0
I need 4-5 Simple Linked List programs (complete and not partial like in books) with with c++
1. Making the link list with integer or name/age/height. 2. Making the link list with integer or name/age/height. and then display the list. 3. Now to add one node to the existing list.(non header) 4. To add the new node as new header. 5. Now to delete one node from the list.
2 Answers
+ 3
I suggest you use the STL linked list template...
0
1. struct MyList {
MyList(){next=NULL;}
string name;
//Add age/height as required...
struct MyList* next;
};
In code create header of linked list for 1st entry:
MyList* hdr = new MyList();
hdr->name = "Johny";
2. void displayList(MyList* hdr){
MyList* nxt = hdr;
while(nxt){
cout<<"name="<<nxt->name<<endl;
nxt = nxt->next;
}
}
3. void appendToList(MyList* hdr, string name) {
MyList *nxt = hdr;
while(nxt->next)
nxt = nxt->next;
nxt->next = new MyList();
nxt->next->name = name;
}
4. void prependToList(MyList** phdr, string name) {
MyList* n = *phdr;
*phdr = new MyList();
(*phdr)->name = name;
(*phdr)->next = n;
}
5. void deleteFromList(MyList** hdr, string name) {
MyList *prv, *nxt = *hdr;
while(nxt->name != name) {
prv = nxt;
nxt = nxt->next;
}
if(nxt == *hdr)
*hdr = (*hdr)->next;
else {
prv->next = nxt->next;
}
delete nxt;
}