Linked Representation of queues
Linked Representation of queues
We have seen how a queue is created using an array. Although this technique of creating a queue is easy, its drawback is that array must be declared to have some fixed size. the array implementation can not be used for large-scale applications where the queues are implemented. One of the alternatives to array implementation is the linked list implementation of a queue.
The storage requirement of linked representation of a queue with n elements is o(n) while the time required for operations is o(1).
In a linked queue, each node of the queue consists of two parts i.e. data part and the linked part. Each element of the queue points to its immediate next element in the memory.
There are two pointers maintained in the memory in the linked queue, 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 the rear and front end respectively. If the front and rear both are NULL, it indicates that the queue is empty.
Operation on Linked Queue
Insert operation
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
In the second case, the queue contains more than one element. The condition front = NULL becomes false. In this scenario, we need to update the end pointer rear so that the next pointer of the rear will point to the new node ptr. Since this is a linked queue, hence we also need to make the rear pointer point to the newly added node ptr. We also need to make the next pointer of the rear point NULL.
rear = ptr;
rear->next = NULL;
Algorithm
Step 1: Allocate the space for the new node PTR
Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
Step 4: END
C Function
- void insert(struct node *ptr, int item; )
- {
- ptr = (struct node *) malloc (sizeof(struct node));
- if(ptr == NULL)
- {
- printf("\nOVERFLOW\n");
- return;
- }
- else
- {
- ptr -> data = item;
- if(front == NULL)
- {
- front = ptr;
- rear = ptr;
- front -> next = NULL;
- rear -> next = NULL;
- }
- else
- {
- rear -> next = ptr;
- rear = ptr;
- rear->next = NULL;
- }
- }
- }
Deletion operation removes the element that is first inserted among all the queue elements. Firstly, we need to check either the list is empty or not. The condition front == NULL becomes true if the list is empty, in this case, we simply write underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy the node pointed by the front pointer into the pointer ptr. Now, shift the front pointer, point to its next node, and free the node pointed by the node ptr. This is done by using the following statements.
ptr = front;front = front -> next;
free(ptr);
Algorithm
Step 1: IF FRONT = NULL
Write " Underflow "
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END
C Function
- void delete (struct node *ptr)
- {
- if(front == NULL)
- {
- printf("\nUNDERFLOW\n");
- return;
- }
- else
- {
- ptr = front;
- front = front -> next;
- free(ptr);
- }
- }
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *front;
- struct node *rear;
- void insert();
- void delete();
- void display();
- void main ()
- {
- int choice;
- while(choice != 4)
- {
- printf("\n*************************Main Menu*****************************\n");
- printf("\n=================================================================\n");
- printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
- printf("\nEnter your choice ?");
- scanf("%d",& choice);
- switch(choice)
- {
- case 1:
- insert();
- break;
- case 2:
- delete();
- break;
- case 3:
- display();
- break;
- case 4:
- exit(0);
- break;
- default:
- printf("\nEnter valid choice??\n");
- }
- }
- }
- void insert()
- {
- struct node *ptr;
- int item;
- ptr = (struct node *) malloc (sizeof(struct node));
- if(ptr == NULL)
- {
- printf("\nOVERFLOW\n");
- return;
- }
- else
- {
- printf("\nEnter value?\n");
- scanf("%d",&item);
- ptr -> data = item;
- if(front == NULL)
- {
- front = ptr;
- rear = ptr;
- front -> next = NULL;
- rear -> next = NULL;
- }
- else
- {
- rear -> next = ptr;
- rear = ptr;
- rear->next = NULL;
- }
- }
- }
- void delete ()
- {
- struct node *ptr;
- if(front == NULL)
- {
- printf("\nUNDERFLOW\n");
- return;
- }
- else
- {
- ptr = front;
- front = front -> next;
- free(ptr);
- }
- }
- void display()
- {
- struct node *ptr;
- ptr = front;
- if(front == NULL)
- {
- printf("\nEmpty queue\n");
- }
- else
- { printf("\nprinting values .....\n");
- while(ptr != NULL)
- {
- printf("\n%d\n",ptr -> data);
- ptr = ptr -> next;
- }
- }
- }
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ? 1
Enter value?
123
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter value?
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
123
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?2
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?4
Comments
Post a Comment