# Midterm Tomorrow, Link List help

• 11-19-2002
WildLyxn
Can someone give me a link to a linkList tutorial? or help me understand the code for a linkList. Thanks
• 11-20-2002
OneStiffRod
http://www.cprogramming.com/tutorial/lesson15.html

A singly-linked list just simply holds a ptr that points to the next object in the list.
• 11-20-2002
vasanth
a double linked list hold a ptr to the previous node as well as the next one... below is a double linked list prog...

Code:

``` # include <iostream.h> # include <conio.h> # include <process.h> class linked{                 private:                         int count;                         struct node    {                                         int val;                                         struct node *next;                                         struct node *prev;                                         };                         struct node *head;                         struct node *tail;                         struct node *tailprev;                 public:                 linked();                 createnode(int);                 deletenode(int);                 insertnode(int);                 modify(int);                 view(int);                 search(int);                 viewall();                 browse();                 int getnum();                 }; linked::search(int sear) { if(count==0) cout<<"\nThe linked list is empty.."; else { cout<<"\nThe nodes with that value are > \n\n\n"; node *present=head;         for(int i=0;i<count;i++)         {if(present->val==sear)         {         if(i<9)         cout<<"\nNode 00"<<i+1<<")                "<<present->val<<"      @ "<<present;         else         if(i>=9 && i<100)         cout<<"\nNode 0"<<i+1<<")                "<<present->val<<"      @ "<<present;         else         cout<<"\nNode "<<i+1<<")                "<<present->val<<"      @ "<<present;         }         present=present->next;         } } getch(); return 0; } linked::insertnode(int nodeadd) { if(nodeadd>count || nodeadd<1) cout<<"Sorry the node number does not exist"; else { node* newnode=new node; int nodeval; cout<<"\nEnter the value for the inserted node > "; cin>>nodeval; newnode->val=nodeval; node *present=head; for(int i=1;i<nodeadd;i++) { present=present->next; } if(nodeadd!=1) { present->prev->next=newnode; newnode->prev=present->prev; newnode->next=present; newnode->val=nodeval; present->prev=newnode; } else { newnode->prev=present->prev; newnode->next=present; newnode->val=nodeval; present->prev=newnode; head=newnode; } count++; cout<<"\nNode sucessfully inserted"; } getch(); return 0; } linked::browse() { node *present=head; char ch; int co=1; if(count==0) cout<<"\nSorry no nodes present"; while(count!=0) { gotoxy(30,20); cout<<"                                                "; gotoxy(30,20); cout<<"Node "<<co<<")        "<<present->val<<" @ address "<<present; ch=getch(); if(ch==0x4d) { if(present->next!=NULL) { present=present->next; co++; } } if(ch==0x4b) { if(present->prev!=NULL) { present=present->prev; co--; } } if(ch==27) break; } getch(); return 0; } linked::modify(int nodeno) { node *present=head; if(nodeno>count || nodeno<1) cout<<"\nThe node no "<<nodeno<<" does not exist."; else { for(int i=1;i<nodeno;i++) { present=present->next; } cout<<"\nThe node "<<nodeno<<" contains the value "<<present->val<<" @ "<<present; int newval; cout<<"\nEnter the new value for the node > "; cin>>newval; present->val=newval; cout<<"\nNode sucessfully modified."; } getch(); return 0; } linked::view(int nodeno) { node *present=head; if(nodeno>count || nodeno<1) cout<<"\nThe node no does not exist."; else { for(int i=1;i<nodeno;i++) { present=present->next; } cout<<"\nThe node "<<nodeno<<" contains the value "<<present->val<<" @ "<<present; } getch(); return 0; } int linked::getnum() { return count; } linked::deletenode(int nodeno) { node *present=head; if(nodeno>count || nodeno<1) cout<<"\nThe node no does not exist."; else { for(int i=1;i<nodeno;i++) { present=present->next; } node *temp=present; if(temp->prev!=NULL && (nodeno!=count)&& nodeno!=1) present->prev->next=present->next; if(temp->next!=NULL &&(nodeno!=1)) present->next->prev=present->prev; if(temp->prev==NULL) { present->next->prev=NULL; head=present->next; } if(temp->next==NULL) {  present->next=NULL;  tail=present; } cout<<"\n The Node no "<<nodeno<<" with value "<<present->val<<" sucessfully deleted"; count--; } getch(); return 0; } linked::viewall() { if(count==0) cout<<"\nThe linked list is empty.."; else { cout<<"\nThe nodes are > \n\n\n"; node *present=head;         for(int i=0;i<count;i++)         {         if(i<9)         cout<<"\nNode 00"<<i+1<<")                "<<present->val<<"      @ "<<present;         else         if(i>=9 && i<100)         cout<<"\nNode 0"<<i+1<<")                "<<present->val<<"      @ "<<present;         else         cout<<"\nNode "<<i+1<<")                "<<present->val<<"      @ "<<present;         present=present->next;         } } return 0; } linked::linked() {  count=0;  head=new node();  head->val=0;  head->next=NULL;  head->prev=NULL;  tail=head;  tailprev=tail; } linked::createnode(int newnod) { if(count==0) { head->val=newnod; } else { tail=new node; tail->val=newnod; tailprev->next=tail; tail->next=NULL; tail->prev=tailprev; tailprev=tail; } count++; return 0; } main() { int nodeno=0; linked l1; clrscr(); int option=0; while(option!=10) { clrscr(); cout<<"\n                                M E N U"; cout<<"\n                                * * * *"; cout<<"\n\n\n                                No of nodes present > "<<l1.getnum(); cout<<"\n\n\n\n\n                                1) Create a node"; cout<<"\n\n                                    2) Delete a node"; cout<<"\n\n                                3) View All node"; cout<<"\n\n                                4) View Particular node"; cout<<"\n\n                                5) Modify a node"; cout<<"\n\n                                6) Browse through node"; cout<<"\n\n                                7) Insert node"; cout<<"\n\n                                8) Search node"; cout<<"\n\n                                9) Use loop to create nodes"; cout<<"\n\n                                10) Exit"; cout<<"\n\n\n\n\n                                Option > "; cin>>option; clrscr(); if(option==1) { cout<<"\nEnter the value for node "<<l1.getnum()+1<<" > "; cin>>nodeno; l1.createnode(nodeno); } if(option==2) { cout<<"\nEnter the node no to be deleted > "; cin>>nodeno; l1.deletenode(nodeno); } if(option==3) { l1.viewall(); getch(); } if(option==4) { cout<<"\nEnter the node no you want to view > "; cin>>nodeno; l1.view(nodeno); } if(option==5) {  int newval;  cout<<"\nEnter the node no you want to modify > ";  cin>>nodeno;  l1.modify(nodeno); } if(option==6) {  cout<<"\nTo browse use the left and right arrow keys...press esc to exit";  l1.browse(); } if(option==7) { cout<<"\nEnter the position to insert node > "; int node1; cin>>node1; l1.insertnode(node1); } if(option==8) { int searc; cout<<"\nEnter the value you want to search > "; cin>>searc; l1.search(searc); } if(option==9) { int a,b,c; cout<<"\nEnter the start point of loop > "; cin>>a; cout<<"Enter the limit for the loop > "; cin>>b; cout<<"Enter the step value > "; cin>>c; int cou=0; for(int i=a;i<b;i=i+c) { cou++; l1.createnode(i); cout<<"\ncreating new node with value > "<<i; } cout<<"\n A total of "<<cou<<" nodes have been created"; getch(); } } return 0; }```