Queue – Linked List ImplementationIn the previous post, we introduced Queue and discussed array implementation. In this post, linked list implementation is discussed. The following two main operations must be implemented efficiently. Show Implement a stack using singly linked listTo implement a stack using singly linked list concept , all the singly linked list operations are performed based on Stack operations LIFO(last in first out) and with the help of that knowledge we are going to implement a stack using single linked list. Using singly linked lists , we implement stack by storing the information in the form of nodes and we need to follow the stack rules and implement using singly linked list nodes . So we need to follow a simple rule in the implementation of a stack which is last in first out and all the operations can be performed with the help of a top variable .Let us learn how to perform Pop , Push , Peek ,Display operations in the following article . A stack can be easily implemented using the linked list. In stack Implementation, a stack contains a top pointer. which is “head” of the stack where pushing and popping items happens at the head of the list. First node have null in link field and second node link have first node address in link field and so on and last node address in “top” pointer.
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output: 44->33->22->11-> Top element is 44 22->11-> Top element is 22 Time Complexity: The time complexity for all push(), pop(), and peek() operations is O(1) as we are not performing any kind of traversal over the list. We perform all the operations through the current pointer only.
Article Tags :
Linked List Stack Technical Scripter
Technical Scripter 2018 Practice Tags :
Linked List Stack Linked List implementation of QueueDue to the drawbacks discussed in the previous section of this tutorial, the array implementation can not be used for the large scale applications where the queues are implemented. One of the alternative of array implementation is linked list implementation of queue. The storage requirement of linked representation of a queue with n elements is o(n) while the time requirement for operations is o(1). In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each element of the queue points to its immediate next element in the memory. In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer. The front pointer contains the address of the starting element of the queue while the rear pointer contains the address of the last element of the queue. Insertion and deletions are performed at rear and front end respectively. If front and rear both are NULL, it indicates that the queue is empty. The linked representation of queue is shown in the following figure. C program to Implement a Queue using Linked List/* * C Program to Implement Queue Data Structure using Linked List */ #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; } *front, *back; /* Create an empty queue */ void initialize() { front = back = NULL; } /* Returns queue size */ int getQueueSize() { struct node *temp = front; int count = 0; if(front == NULL && back == NULL) return 0; while(temp != back){ count++; temp = temp->next; } if(temp == back) count++; return count; } /* Returns Frnt Element of the Queue */ int getFrontElement() { return front->data; } /* Returns the Rear Element of the Queue */ int getBackElement() { return back->data; } /* Check's if Queue is empty or not */ void isEmpty() { if (front == NULL && back == NULL) printf("Empty Queue\n"); else printf("Queue is not Empty\n"); } /* Adding elements in Queue */ void enqueue(int num) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->data = num; temp->next = NULL; if (back == NULL) { front = back = temp; } else { back->next = temp; back = temp; } } /* Removes an element from front of the queue */ void dequeue() { struct node *temp; if (front == NULL) { printf("\nQueue is Empty \n"); return; } else { temp = front; front = front->next; if(front == NULL){ back = NULL; } printf("Removed Element : %d\n", temp->data); free(temp); } } /* Print's Queue */ void printQueue() { struct node *temp = front; if ((front == NULL) && (back == NULL)) { printf("Queue is Empty\n"); return; } while (temp != NULL) { printf("%d", temp->data); temp = temp->next; if(temp != NULL) printf("-->"); } } int main() { /* Initializing Queue */ initialize(); /* Adding elements in Queue */ enqueue(1); enqueue(3); enqueue(7); enqueue(5); enqueue(10); /* Printing Queue */ printQueue(); /* Printing size of Queue */ printf("\nSize of Queue : %d\n", getQueueSize()); /* Printing front and rear element of Queue */ printf("Front Element : %d\n", getFrontElement()); printf("Rear Element : %d\n", getBackElement()); /* Removing Elementd from Queue */ dequeue(); dequeue(); dequeue(); dequeue(); dequeue(); dequeue(); return 0; } Output1-->3-->7-->5-->10 Size of Queue : 5 Front Element : 1 Rear Element : 10 Removed Element : 1 Removed Element : 3 Removed Element : 7 Removed Element : 5 Removed Element : 10 Queue is Empty Queue using linked listQueue using an array - drawbackIf we implement the queue using an array, we need to specify the array size at the beginning(at compile time). We can't change the size of an array at runtime. So, the queue will only work for a fixed number of elements. SolutionWe can implement the queue data structure using the linked list. In the linked list, we can change its size at runtime. |