Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1 Linked Lists 2 Linked Lists • START = NULL if there is no list (i.e.; NULL list or empty list). 3 ADVANTAGES OF LINKED LIST Linked list have many advantages and some of them are: 1. Linked list are dynamic data structure. That is, they can grow or shrink during the execution of a program. 2. Efficient memory utilization: Memory is allocated whenever it is required. And it is deallocated when it is not needed. 3. Insertion and deletion are easier and efficient. 4. Many complex applications can be easily carried out with linked list. 4 OPERATIONS ON LINKED LISTS The primitive operations performed on the linked list are as follows 1. Creation 2. Insertion 3. Deletion 4. Traversing 5. Searching 6. Concatenation 5 OPERATIONS ON LINKED LISTS • Creation operation is used to create a linked list. • Insertion operation is used to insert a new node at any specified location in the linked list. A new node may be inserted. (a) At the beginning of the linked list (b) At the end of the linked list (c) At any specified position in between in a linked list • Deletion operation is used to delete a node from the linked list. A node may be deleted from the (a) Beginning of a linked list (b) End of a linked list (c) Specified location of the linked list • Traversing is the process of going through all the nodes from one end to another end of a linked list. • Concatenation is the process of appending the second list to the end of the first list. 6 TYPES OF LINKED LIST 1. Singly linked list 2. Doubly linked list 3. Circular linked list 7 SINGLY LINKED LIST 8 SINGLY LINKED LIST 9 SINGLY LINKED LIST 10 SINGLY LINKED LIST 11 ALGORITHM FOR INSERTING A NODE Suppose • START is the first position in linked list. • DATA be the element to be inserted in the new node. • POS is the position where the new node is to be inserted. • TEMP is a temporary pointer to hold the node address. 12 ALGORITHM FOR INSERTING A NODE Insert a Node at the beginning 1. Input DATA to be inserted 2. Create a NewNode 3. NewNode → DATA = DATA 4. If (SATRT equal to NULL) (a) NewNode → Next = NULL 5. Else (a) NewNode → Next = START 6. START = NewNode 7. Exit 13 ALGORITHM FOR INSERTING A NODE Insert a Node at the end 1. Input DATA to be inserted 2. Create a NewNode 3. NewNode → DATA = DATA 4. NewNode → Next = NULL 8. If (SATRT equal to NULL) (a) START = NewNode 9. Else (a) TEMP = START (b) While (TEMP → Next not equal to NULL) (i) TEMP = TEMP → Next 10. TEMP → Next = NewNode 11. Exit 14 ALGORITHM FOR INSERTING A NODE Insert a Node at any specified position (POS=1 means insert after first node. POS=0 not allowed) 1. Input DATA and POS to be inserted 2. initialise TEMP = START; and k = 1 3. Repeat the step 3 while( k is less than POS) (a) TEMP = TEMP Next (b) If (TEMP is equal to NULL) (i) Display “Node in the list less than the position” (ii) Exit (c) k = k + 1 4. Create a New Node 5. NewNode DATA = DATA 6. NewNode Next = TEMP → Next 7. TEMP Next = NewNode 8. Exit 15 ALGORITHM FOR DELETING A NODE Suppose • START is the first position in linked list. Let • DATA be the element to be deleted. • TEMP, HOLD is a temporary pointer to hold the node address. 16 ALGORITHM FOR DELETING A NODE 1. Input the DATA to be deleted 2. if ((START → DATA) is equal to DATA) // Delete first node (a) TEMP = START (b) START = START Next (c) Set free the node TEMP, which is deleted (d) Exit 3. HOLD = START 4. while ((HOLD Next Next) not equal to NULL)) // Between first & last (a) if ((HOLD NEXT DATA) equal to DATA) (i) TEMP = HOLD Next (ii) HOLD Next = TEMP Next (iii) Set free the node TEMP, which is deleted (iv) Exit (b) HOLD = HOLD Next 17 ALGORITHM FOR DELETING A NODE 5. if ((HOLD next DATA) = = DATA) (a) TEMP = HOLD → Next (b) Set free the node TEMP, which is deleted (c) HOLD Next = NULL (d) Exit 6. Disply “DATA not found” 7. Exit // last node 18 ALGORITHM FOR SEARCHING A NODE 1. Input the DATA to be searched 2. Initialize TEMP = START; POS = 1; 3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL) 4. If (TEMP DATA is equal to DATA) (a) Display “The data is found at POS” (b) Exit 5. TEMP = TEMP Next 6. POS = POS+1 7. If (TEMP is equal to NULL) (a) Display “The data is not found in the list” 8. Exit 19 ALGORITHM FOR DISPLAY ALL NODES 1. If (START is equal to NULL) (a) Display “The list is Empty” (b) Exit 2. Initialize TEMP = START 3. Repeat the step 4 and 5 until (TEMP = = NULL ) 4. Display “TEMP DATA” 5. TEMP = TEMP Next 6. Exit 20 Programming Linked List class LinkedList{ //Structure declaration for the node struct node{ int info; node *next; }; //private structure variable declared node *start; public: LinkedList()//Constructor defined { start = NULL; } //public fucntion declared void Create_List(int); void AddAtBeg(int); void AddAfter(int, int); void Delete(); void Count(); void Search(int); void Display(); }; 21 Programming Linked List //This function will create a new linked list of elements void LinkedList::Create_List(int data) { node *q, *newNode; //New node is created with new operator newNode = new node; newNode->info = data; newNode->next = NULL; if (start == NULL) //If list is empty start = newNode; else { //Element inserted at the end */ q = start; while(q->next != NULL) q = q->next; q->next = newNode; } }/*End of create_list()*/ 22 Programming Linked List //following function will add new element at the beginning void LinkedList::AddAtBeg(int data) { node *newNode; newNode = new node; newNode->info = data; newNode->next = start; start = newNode; }/*End of addatbeg()*/ 23 Programming Linked List //This function will add new element at any specified position void LinkedList::AddAfter(int data, int pos) { node *newNode, *q; int i; q = start; //Finding the position in the linked list to insert for(i = 0; i<pos-1; i++){ q = q->next; if(q == NULL) { cout<<"\n\nThere are less than "<<pos<<" elements" << endl; return; } }/*End of for*/ newNode = new node; newNode->next = q->next; newNode->info = data; q->next = newNode; }/*End of addafter()*/ 24 Programming Linked List void LinkedList::Delete() { node *tmp, *q; int data; if(start == NULL) { cout<<"\n\nList is empty"<<endl; return; } cout<<"\n\nEnter the element for deletion : "; cin>>data; if(start->info == data){ tmp = start; start = start->next; //First element deleted delete(tmp); return; } 25 Programming Linked List q = start; while(q->next->next ! = NULL) { if(q->next->info == data) { //Element deleted in between tmp = q->next; q->next = tmp->next; delete(tmp); return; } q = q->next; }/*End of while */ if(q->next->info == data) { //Last element deleted tmp = q->next; delete(tmp); q->next = NULL; return; } cout<<"\n\nElement "<<data<<" not found"<<endl; }/*End of del()*/ 26 Programming Linked List void LinkedList::Display() { // Fill in the code that displays the contents of the linked list }/*End of display() */ void LinkedList::Count() { // Fill in the code that counts the nodes of the linked list }/*End of count() */ void LinkedList::Search(int data) { // Fill in the code that will find the position of a node that holds the ‘data’ } 27 Programming Linked List int main() { int choice, n, m, position, i; LinkedList po; while(1) { cout<<"1.Create List\n"; cout<<"2.Add at beginning\n"; cout<<"3.Add after \n"; cout<<"4.Delete\n"; cout<<"5.Display\n"; cout<<"6.Count\n"; cout<<“7.Search\n"; cout<<“8.Quit\n"; cout<<"\nEnter your choice:"; cin>>choice; 28 Programming Linked List switch(choice) { case 1: cout<<"\n\nHow many nodes you want:"; cin>>n; for(i = 0;i<n;i++) { cout<<"\nEnter the element:"; cin>>m; po.Create_List(m); } break; case 2: cout<<"\n\nEnter the element:"; cin>>m; po.AddAtBeg(m); break; 29 Programming Linked List case 3: cout<<"\n\nEnter the element:"; cin>>m; cout<<"\nEnter the position after the inserted element :"; cin>>position; po.AddAfter(m,position); break; case 4: po.Delete(); break; case 5: po.Display(); break; case 6: po.Count(); break; case 7: cout<<"\n\nEnter the element to be searched:"; 30 Programming Linked List cin>>m; po.Search(m); break; case 8: exit(0); default: cout<<"\n\nWrong choice"; }/*End of switch */ }/*End of while */ } 31 STACK USING LINKED LIST PUSH 32 STACK USING LINKED LIST POP 33 STACK USING LINKED LIST ALGORITHM FOR PUSH OPERATION • TOP is NULL when the stack is empty. • DATA is the data item to be pushed. 1. Input the DATA to be pushed 2. Creat a New Node 3. NewNode → DATA = DATA 4. NewNode → Next = TOP 5. TOP = NewNode 6. Exit 34 STACK USING LINKED LIST • ALGORITHM FOR POP OPERATION 1. if (TOP is equal to NULL) (a) Display “The stack is empty” 2. Else (a) TEMP = TOP (b) Display “The popped element TOP → DATA” (c) TOP = TOP → Next (d) TEMP → Next = NULL (e) Free the TEMP node 3. Exit 35 QUEUE USING LINKED LIST ADD to queue 36 QUEUE USING LINKED LIST REMOVE from queue 37 QUEUE USING LINKED LIST ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE 1. Input the DATA element to be pushed 2. Create a New Node 3. NewNode → DATA = DATA 4. NewNode → Next = NULL 5. If(REAR not equal to NULL) // if it is not empty (a) REAR → next = NewNode (b) REAR = NewNode 6. Else REAR = FRONT = NewNode 7. Exit 38 QUEUE USING LINKED LIST ALGORITHM FOR POPPING AN ELEMENT FROM A QUEUE 1. If (FRONT is equal to NULL) (a) Display “The Queue is empty” 2. Else (a) Display “The popped element is FRONT → DATA” (b) If(FRONT is not equal to REAR) (i ) FRONT = FRONT → Next (c) Else (i ) FRONT = NULL (ii) REAR = NULL 3. Exit 39 DOUBLY LINKED LIST Every nodes in the doubly linked list has three fields: LeftPointer, RightPointer and DATA. 40 REPRESENTATION OF DOUBLY LINKED LIST • A node in the doubly linked list can be represented with the following declarations: struct Node { int DATA; Node * next; Node * prev; }; Node * START; • All operations performed on singly linked list can also be performed on doubly linked list. 41 REPRESENTATION OF DOUBLY LINKED LIST 42 CIRCULAR LINKED LIST • A circular linked list is one, which has no beginning and no end. A singly linked list can be made a circular linked list by simply storing the address of the very first node in the linked field of the last node. 43 CIRCULAR DOUBLY LINKED LIST • A circular doubly linked list has both the successor pointer and predecessor pointer in circular manner. 44