List Using Linked Memory Various cells of memory

































- Slides: 33
List Using Linked Memory Various cells of memory are not allocated consecutively in memory. Not enough to store the elements of the list. With arrays, the second element was right next to the first element. Now the first element must explicitly tell us where to look for the second element. Do this by holding the memory address of the second element
Linked List Create a structure called a Node. object next The object field will hold the actual list element. The next field in the structure will hold the starting location of the next node. Chain the nodes together to form a linked list.
Linked List Picture of our list (2, 6, 7, 8, 1) stored as a linked list: head 2 6 current 8 7 1 size=5
Linked List Note some features of the list: Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is. The current here is a pointer, not an index. The next field in the last node points to nothing. We will place the memory address NULL which is guaranteed to be inaccessible.
Linked List 1051 Actual picture in memory: current head 6 1052 1063 1053 1063 1054 2 1055 1051 1056 2 6 8 7 1 current 1057 7 1058 1060 1059 head 1060 1 1061 0 1062 1054 1063 8 1064 1057 1065
Linked List Operations 1. 2. 3. 4. Traversing Insertion Deletion Searching
Linked List Algorithm 1. Define structure of node 2. Declare three pointer object start, current, & next of type node. 3. Create 1 st node and assign its address to “Start” also assign value to the node. start = new-node=value 4. Assign address of the 1 st node to the current node to processed next.
Linked List Algorithm 5. Repeat step 6 to 7 for c = 1 to 9 6. Create new node [assign value and address of the new node to the current node And address of new node to current. ] 7. new-node-data = value current pointer = new node Current = new node [ End of step 5 loop] 8. Exit
Linked List Implementation #include<conio. h> #include<stdio. h> struct node{ int data; node *link; } class list { private: node *start, *cur, *temp; Public: list() { Start == NULL; }
//member function to create and add data into list append(int n) { If ( start == NULL) // create first node and assign data { start= new node; start->data=n; start->link=NULL; } else { Cur = start; // go to end of the list While( cur->link!=NULL) Cur=cur->link; // create and add new node at end temp=new node; temp->data=n; temp->link= NULL; cur->link=temp; } }
// member function to print data from nodes print() { cout<<”n. Data in link listnn”; cur=start; while(cur->link!=NULL) { cout<< cur->data<<endl; cur=cur->link; } // print last node value Cout<<cur->data<<endl; } };
void main() { list obj; int val, p; clrscr(); cout<<"Enter five valuesn"; for(int i=1; i<=5; i++) { cin>>val; obj. add_item(val); } obj. print(); getch(); }
Traversing Single Linked List Algorithm 1. Repeat step 2 to 3 while temp->link != NULL 2. Print temp->data 3. Temp=temp->link *[move to next node] [End of step-1 loop] 4. Exit
Insertion in Single Linked List Insertion in a linked list is pobbible by three locations. 1. At the end of the list 2. At the Beginning of list 3. At any specified location in the list
Insertion At The Beginning Of Linked List Algorithm To add new node with Q at the beginning of list 1. Create the List 2. Create a new node using the “new” operator 3. Go to the beginning of the list, i. e. to the first node of the list.
Insertion At The Beginning Of Linked List Algorithm 4. Interchange the starting address of list with new created node. The address of the newly created node become the starting address of the list and previous starting address is assigned to the pointer field of newly created node. 5. Assign value L to the data field of newly Created field.
Insertion At A Specified Location in Linked List Algorithm To add new node having value N to be inserted between nodes having M and O at the beginning of linked list shown below. 1. Create the List 2. Create a new node and assign value N to its data field. 3. Go to the node after which the new node is to be inserted
Insertion At A Specified Location in Linked List Algorithm 4. Assign the address of newly created node to the pointer filed of the node that is to precede it. Assign the value of pointer field of the preceding node to the pointer field of the newly created node.
Insertion At The End Of Linked List Algorithm To add new node with Q at end of the above list 1. Create the List 2. Create a new node using the “new” operator 3. Go to the end of the list and interchange the values of pointer field s of newly created node and last node of the list.
Insertion At The End Of Linked List Algorithm 4. Assign NULL value to the pointer filed of the newly created field and assign value Q to its data field. The newly created node become the last node of the list. Suppose the newly created node has address 106, the list will be shown below.
Linked List Implementation At Specific Location #include<iostream. h> #include<conio. h> struct node { int data; node *link; }; class list { private: node *start, *cur; public: list() { start= NULL; }
add_item(int n) { node *temp; // Create first node & assign data if(start == NULL) { start = new node; start->data = n; start->link = NULL; } else { cur= start; // go to end of list while(cur->link!=NULL) cur=cur->link; // create and add new nodes at end temp= new node; temp->data=n; temp->link=NULL; cur->link=temp; } }
// member function to insert node insert(int n, int pos){ // go to specifed location cur=start; for(int i=1; i<pos-1; i++) { cur=cur->link; if(cur==NULL) { cout<<"Invalid Position"; return 0; } } // create and insert node *temp; temp=new node; temp-> data=n; temp->link=cur->link; cur->link=temp; }
print() { cout<<"Data in Link Listn"; cur=start; while(cur->link!=NULL) { cout<<cur->data<<endl; cur=cur->link; } // print last node value cout<<cur->data<<endl; } };
void main() { list obj; int val, p; clrscr(); cout<<"Enter five valuesn"; for(int i=1; i<=5; i++) { cin>>val; obj. add_item(val); } cout<<"Enter to insert? "; cin>>val; cout<<"Enter position in list? "; cin>>p; obj. insert(val, p); obj. print(); getch(); }
Linked List Deletion node Algorithm 1. Define structure of node 2. Set start null [Add 5 items into the list ] Repeat step -4 for I =1 to 5 3. Input value in n [create node and add items into the link list] If start = null then Start = new node Start -> data = n Start -> link=null Else Cur=start REPEAT WHILL (CUR->LINK !=NULL) [Go To end of list] Cur=cur->link [End of loop ] [create and add new nodes at end] Temp=new node; Temp -> data = n Temp -> link =null Cur -> link=temp END IF [end of steps-3 loop]
Linked List Deletion node Algorithm 5. Input value to delete in N 6. Set cur = temp= start 7. [GO TO SPECIFIED NODE ] REPEAT WHILL CUR->LINK !=NULL IF CUR -> DATA = N THEN PRINT ‘’NUMBER FOUND & DELETED ‘’ TEMP -> LINK = CUR -> LINK DELETE CUR RETURN End if Temp=cur Cur = cur -> link [end of step-7 loop ] If cur= null then PRINT ‘’DATA NOT FOUND’’ End IF 8. EXIT
Linked List Deletion Implementation #include<iostream. h> #include<conio. h> struct node { Int data; node *link; }; class list { private: node *start, *cur, *temp; public: list() { start= NULL; }
add_item(int n) { node *temp; // Create first node & assign data if(start == NULL) { start = new node; start->data = n; start->link = NULL; } else { cur= start; // go to end of list while(cur->link!=NULL) cur=cur->link; // create and add new nodes at end temp= new node; temp->data=n; temp->link=NULL; cur->link=temp; } }
// member function to Delete node del(int n) { // go to specifed node cur=temp=start; while(cur->link!=NULL) { if(cur->data==n) { cout<<"n. Number found and deleten"; temp->link=cur->link; delete cur; break; } temp=cur; cur=cur->link; } cout<<"n Data Not Foundn"; }
//member function to print value print() { cout<<"Data in Link Listn"; cur=start; while(cur->link!=NULL) { cout<<cur->data<<endl; cur=cur->link; } // print last node value cout<<cur->data<<endl; } };
void main() { list obj; int val; clrscr(); cout<<"Enter five valuesn"; for(int i=1; i<=5; i++) { cin>>val; obj. add_item(val); } cout<<"Enternumber to search & delete? "; cin>>val; obj. del(val); obj. print(); getch(); }
ASSIGNMENTS Q 1. Write Algorithm and program to count total number of nodes in linked list. Q 2. Write a program to insert a node at beginning of linked list. Q 3. Write a program to insert a node at end of linked list. Note: All implement must be in class list.