How a queue can be implemented using singly linked list?

Queue – Linked List Implementation

In 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.
In a Queue data structure, we maintain two pointers, front and rear. The front points the first item of queue and rear points to last item.
enQueue() This operation adds a new node after rear and moves rear to the next node.
deQueue() This operation removes the front node and moves front to the next node.

Linked List implementation of Queue

Due 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.



Basic Operation:

Following are basic operations of Queue:

Main Queue Operations:
1)EnQueue(): Inserts an element at the rear of the Queue.
2)DeQueue(): Remove and return the front element of the Queue.

Auxiliary Queue operation:

1)Front(): Display the data front of the Queue.
2)QueueSize(): Returns the number of elements stored in the Queue.
3)display(): Display elements of queue.

There are many different operations that we can implement.
For ex.:- Merging of Two Queue,Sorting of Queue etc..

  • Let's see implementation of Queue and its operations,with example.
    For linked list we will make structure of Node using struct keyword.In which we here taking int data and one pointer of that struct to point another Node.
  • First we will take two pointers of that struct Node, front=NULL and rear=NULL.

EnQueue

Inserting an element in Queue.

  • Initially both of our pointers pointing to the front and rear are NULL.
  • Now,If We enter the 12 in Queue we will checking weather there is any element present or not.If rear is NULL,We make our front and rear pointers to point our newly inserted Node with having data '12'.
  • Now,again if we want to enter new data,Let say 45.We will check for rear NULL or not.Now our rear would be pointing to '12'.So 45 will added next to the rear Node and 45 would be our rear Node.
  • Same way if i add 26, it would be added after 45 and rear will point to the Node having data '26'.


Pseudocode:

Step 1 : Create a newNode with given value and set 'newNode → next' to NULL. Step 2 : Check whether queue is Empty (rear == NULL) Step 3 : If it is Empty then, set front = newNode and rear = newNode. Step 4 : If it is Not Empty then, set rear → next = newNode and rear = newNode.

DeQueue

Deleting an element from Queue.

  • Now, by doing three EnQueue operation we will be having Queue having 3 elements as shown in figure.
  • First we check that front is NULL?If it is then Queue is empty we can not perform DeQueue operation.But here front will be pointing to '12'.
  • So if we want to delete element from Queue. As our Queue is FIFO(first in first out) 12 is first inserted, so 12 will be removed.Our front pointer will be pointing to the '12'.To,remove it first we make our front pointer to point the element inserted after '12'.And then simply we free the memory.
  • As simple as that 45 and 26 will be removed if we perform two more DeQueue operation.And again our Queue will be empty(rear and front will be eqal to NULL).
Step 1 : Check whether queue is Empty (front == NULL). Step 2 : If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the function Step 3 : If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'. Step 4 : Then set 'front = front → next' and delete 'temp' (free(temp)).

Display

  • To display all elements present in Queue, we will take one temp(you can give whatever name you want) pointer and make that to point front.
  • Then we traverse up-to rear or we can say temp!=NULL using temp=temp->next (which will make our pointer to move towards rear) and print temp->data.
Step 1 : Check whether queue is Empty (front == NULL). Step 2 : If it is Empty then, display 'Queue is Empty!!!' and terminate the function. Step 3 : If it is Not Empty then, define a Node pointer 'temp' and initialize with front. Step 4 : Display 'temp → data -->' and move it to the next node. Repeat the same until 'temp' reaches to 'rear' (temp → next != NULL). Step 5 : Finally! Display 'temp → data --> NULL'.

Queue using linked list

Queue using an array - drawback

If 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.

Solution

We can implement the queue data structure using the linked list.

In the linked list, we can change its size at runtime.



How to implement a queue using a linked list

A queue is a first-in-first-out (FIFO) data structure. It can be implemented using a linked list.

The following are the three main operations that can be performed on a queue:

  • enqueue: adding an element to the end of the list.

  • dequeue: popping an item from the head of the list.

  • peeking: displaying the element at the tail of the list.

A queue implemented using a linked list.

Video liên quan

Postingan terbaru

LIHAT SEMUA