CSE Department LINKED LIST A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. Linked List is a very commonly used linear data structure which consists of group of nodes in a sequence. Each node holds its own data and the address of the next node hence forming a chain like structure. Linked Lists are used to create trees and graphs. CONTD…. CONTD…. You have to start somewhere, so we give the address of the first node a special name called HEAD. Also, the last node in the linked list can be identified because its next portion points to NULL. A linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. USES OF LINKED LIST The list is not required to be contiguously present in the memory. The node can reside any where in the memory and linked together to make a list. This achieves optimized utilization of space. The list size is limited to the memory size and doesn't need to be declared in advance. Empty node can not be present in the linked list.
We can store values of primitive types or objects in
the singly linked list. ADVANTAGES OF LINKED LISTS They are a dynamic in nature which allocates the memory when required. Insertion and deletion operations can be easily implemented. Stacks and queues can be easily executed. Linked List reduces the access time. DISADVANTAGES OF LINKED LISTS The memory is wasted as pointers require extra memory for storage. No element can be accessed randomly; it has to access each node sequentially. Reverse Traversing is difficult in linked list. APPLICATIONS OF LINKED LISTS
Linked lists are used to implement
stacks, queues, graphs, etc. Linked lists let you insert elements at the beginning and end of the list. In Linked Lists we don't need to know the size in advance. WHY USE LINKED LIST OVER ARRAY? Till now, we were using array data structure to organize the group of elements that are to be stored individually in the memory. However, Array has several advantages and disadvantages which must be known in order to decide the data structure which will be used throughout the program. Array contains following limitations: The size of array must be known in advance before using it in the program. Increasing size of the array is a time taking process. It is almost impossible to expand the size of the array at run time. All the elements in the array need to be contiguously stored in the memory. Inserting any element in the array needs shifting of all its predecessors. CONTD…. Linked list is the data structure which can overcome all the limitations of an array. It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored in the memory and linked together with the help of pointers. Sizing is no longer a problem since we do not need to define its size at the time of declaration. List grows as per the program's demand and limited to the available memory space. LINKED LISTS TYPES OF LINKED LISTS There are 3 different implementations of Linked List available, they are: Singly Linked List
Doubly Linked List
Circular Linked List
Singly Linked List
SINGLY LINKED LISTS Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in the sequence of nodes. The operations we can perform on singly linked list are insertion, deletion and traversal. A node in the singly linked list consist of two parts: data part and link part. Data part of the node stores actual information that is to be represented by the node while the link part of the node stores the address of its immediate successor. One way chain or singly linked list can be traversed only in one direction. In other words, we can say that each node contains only next pointer, therefore we can not traverse the list in the reverse direction. CONTD…. DOUBLY LINKED LIST In a doubly linked list, each node contains a data part and two addresses, one for the previous node and one for the next node. Doubly linked list is a sequence of elements in which every node has link to its previous node and next node. Traversing can be done in both directions and displays the contents in the whole list. A Doubly Linked List contains an extra memory to store the address of the previous node, together with the address of the next node and data which are there in the singly linked list. So, here we are storing the address of the next as well as the previous nodes. CONTD…. ADVANTAGES OVER SINGLY LINKED LIST It can be traversed both forward and backward direction. The delete operation is more efficient if the node to be deleted is given. The insert operation is more efficient if the node is given before which insertion should take place. DISADVANTAGES OVER SINGLY LINKED LIST
Itwill require more space as each node has an
extra memory to store the address of the previous node. The number of modification increase while doing various operations like insertion, deletion, etc. CIRCULAR LINKED LIST In circular linked list the last node of the list holds the address of the first node hence forming a circular chain. Circular linked list is similar to singly linked list. The only difference is that in circular linked list, the last node points to the first node in the list. It is a sequence of elements in which every element has link to its next element in the sequence and has a link to the first element in the sequence. A circular linked list is either a singly or doubly linked list in which there are no NULL values. Here, we can implement the Circular Linked List by making the use of Singly or Doubly Linked List. In the case of a singly linked list, the next of the last node contains the address of the first node and in case of a doubly-linked list, the next of last node contains the address of the first node and prev of the first node contains the address of the last node. CONTD…. BASIC OPERATIONS ON LINKED LIST
Traversal: To traverse all the nodes one after another.
Insertion: To add a node at the given position. Deletion: To delete a node. Searching: To search an element(s) by value. Updating: To update a node. Sorting: To arrange nodes in a linked list in a specific order. Merging: To merge two linked lists into one. REPRESENTATION
A linked list is represented by a pointer to the first node of
the linked list. The first node is called the head. If the linked list is empty, then the value of the head is NULL. Each node in a list consists of at least two parts: 1) data 2) Pointer (Or Reference) to the next node In C, we can represent a node using structures. ALGORITHM FOR INSERTING A NODE CONTD…. ALGORITHM FOR DELETING A NODE CONTD…. ALGORITHM FOR SEARCHING A NODE TRAVERSING A LINKED LIST ALGORITHM LINKED LIST IMPLEMENTATION OF STACK Instead of using array, we can also use linked list to implement stack. Linked list allocates the memory dynamically. However, time complexity in both the scenario is same for all the operations i.e. push, pop and peek. In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each node contains a pointer to its immediate successor node in the stack. Stack is said to be overflown if the space left in the memory heap is not enough to create a node. ADDING A NODE TO THE STACK (PUSH OPERATION)
Adding a node to the stack is referred to as push operation.
Pushing an element to a stack in linked list implementation is different from that of an array implementation. In order to push an element onto the stack, the following steps are involved. Create a node first and allocate memory to it. If there are some nodes in the list already, then we have to add the new element in the beginning of the list (to not violate the property of the stack). For this purpose, assign the address of the starting element to the address field of the new node and make the new node, the starting node of the list. Time Complexity : o(1) CONTD…. DELETING A NODE FROM THE STACK (POP OPERATION) Deleting a node from the top of stack is referred to as pop operation. Deleting a node from the linked list implementation of stack is different from that in the array implementation. In order to pop an element from the stack, we need to follow the following steps : Check for the underflow condition: The underflow condition occurs when we try to pop from an already empty stack. The stack will be empty if the head pointer of the list points to null. Adjust the head pointer accordingly: In stack, the elements are popped only from one end, therefore, the value stored in the head pointer must be deleted and the node must be freed. The next node of the head node now becomes the head node. Time Complexity : o(n) DISPLAY THE NODES (TRAVERSING) Displaying all the nodes of a stack needs traversing all the nodes of the linked list organized in the form of stack. For this purpose, we need to follow the following steps. Copy the head pointer into a temporary pointer. Move the temporary pointer through all the nodes of the list and print the value field attached to every node. Time Complexity : o(n) HEADER NODE CONTD…. LINKED LIST IMPLEMENTATION OF QUEUES CONTD…. CONTD…. CONTD…. OPERATIONS OF DOUBLY LINK LIST INSERTION CONTD…. CONTD…. CONTD…. DELETTION CONTD…. CONTD…. CONTD…. TRAVERSING INSERTION AT THE FRONT OF CIRCULAR LINKED LIST Procedure for insertion a node at the beginning of list Step1. Create the new node Step2. Set the new node’s next to itself (circular!) Step3. If the list is empty,return new node. Step4. Set our new node’s next to the front. Step5. Set tail’s next to our new node. Step6. Return the end of the list. ALGORITHM FOR INSERTION AT THE FRONT OF CIRCULAR LINKED LIST node* AddFront(node* tail, int num) { node *temp = (node*)malloc(sizeof(node)); temp->data = num; temp->next = temp; if (tail == NULL) return temp; temp->next = tail->next; tail->next = temp; return tail; INSERTION AT THE END OF CIRCULAR LINKED LIST Procedure for insertion a node at the end of list Step1. Create the new node Step2. Set the new node’s next to itself (circular!) Step3. If the list is empty,return new node. Step4. Set our new node’s next to the front. Step5. Set tail’s next to our new node. Step6. Return the end of the list. ALGORITHM FOR INSERTION AT THE END OF CIRCULAR LINKED LIST node* AddEnd(node* tail, int num) { node *temp = (node*)malloc(sizeof(node)); temp->data = num; temp->next = temp; if (tail == NULL) return temp; temp->next = tail->next; tail->next = temp; return temp; } DELETION AT BEGINNING OF THE CIRCULAR LINKED LIST Algorithm for deleting a node from the Beginning in circular linked list Step 1: Check for Overflow if start = Null then print list is empty Exit End if Step 2: set ptr = start Step 3: set start = start -> next Step 4: print Element deleted is , ptr -> info Step 5: set last -> next = start Step 6: free ptr Step 7: EXIT ALGORITHM FOR DELETING A NODE FROM THE END IN CIRCULAR LINKED LIST Step 1: IF START = NULL Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Steps 4 and 5 while PTR NEXT != START Step 4: SET PREPTR = PTR Step 5: SET PTR = PTR NEXT [END OF LOOP] Step 6: SET PREPTR NEXT = START Step 7: FREE PTR Step 8: EXIT CIRCULAR LINKED LIST (TRAVERSAL) In a conventional linked list, we traverse the list from the head node and stop the traversal when we reach NULL. In a circular linked list, we stop traversal when we reach the first node again. STEP 1: SET PTR = HEAD STEP 2: IF PTR = NULL WRITE "EMPTY LIST" GOTO STEP 8 END OF IF STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR → NEXT != HEAD STEP 5: PRINT PTR → DATA STEP 6: PTR = PTR → NEXT [END OF LOOP] STEP 7: PRINT PTR→ DATA STEP 8: EXIT