0% found this document useful (0 votes)
192 views

Queue Operation of Link List: Lab Report No.13

The document discusses implementing a queue data structure using a linked list. A queue implemented with an array has a fixed size, but a linked list implementation can support a variable number of elements. The linked list queue uses two pointers - front points to the first element and rear points to the last. Elements can be added to the rear of the queue using enQueue() and removed from the front using deQueue(). Code examples are provided to demonstrate enqueue and dequeue operations on a linked list queue.

Uploaded by

Mian Tauseef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

Queue Operation of Link List: Lab Report No.13

The document discusses implementing a queue data structure using a linked list. A queue implemented with an array has a fixed size, but a linked list implementation can support a variable number of elements. The linked list queue uses two pointers - front points to the first element and rear points to the last. Elements can be added to the rear of the queue using enQueue() and removed from the front using deQueue(). Code examples are provided to demonstrate enqueue and dequeue operations on a linked list queue.

Uploaded by

Mian Tauseef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Report no.

13

Queue Operation Of Link List


The major problem with the queue implemented using array is, It will work for only fixed
number of data. That means, the amount of data must be specified in the beginning itself.
Queue using array is not suitable when we don't know the size of data which we are going to
use. A queue data structure can be implemented using linked list data structure. The queue
which is implemented using linked list can work for unlimited number of values. That means,
queue using linked list can work for variable size of data (No need to fix the size at beginning
of the implementation). The Queue implemented using linked list can organize as many data
values as we want. In linked list implementation of a queue, the last inserted node is always
pointed by 'rear' and the first node is always pointed by 'front'.

Example:

In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted
node is 10 and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.

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.
Main Program:
#include<stdio.h>
#include<malloc.h>
void queue_operation();
void enque_operation();
void deque_operation();
void transverse();
int count;
struct node{
int data;
struct node *ptr;
}*front,*rear,*temp,*front1;
int main(){
int ch=0;
while(ch<10){
printf("\n\t1-creation""\n\t2-insertion""\n\t3-Deletion""\n\t4-Searching""\n\t5-sorting""\n\t6-
transverse""\n\t7-Stack Operation""\n\t8-Queu Operation""\n\t9-exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
Roll no.16-ELE-03

59
Lab Report no.13

switch(ch){
case 1:break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
case 7:break;
case 8:queu_operation();break;
default:break;}}}
Code of Queue Operation:
void queue_operation(){
int ch=0;
int no, e;
front = rear = NULL;
while(ch<4){
printf("\t1-Enque operation\n\t2-Deque operation\n\t3-Transverse\n\t4-Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:enque_operation();break;
case 2:deque_operation();break;
case 3:transverse();break;
default:break;}}}
Code of Deque operation:
void enque_operation(){
int data;
printf("Enter data:");
scanf("%d",&data);
if (rear == NULL) {
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->data = data;
front = rear;}
else {
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->data = data;
temp->ptr = NULL;
rear = temp; }
count++;}

Roll no.16-ELE-03

60
Lab Report no.13

output of Enque Operation:

Code of Deque Operation:


void deque_operation(){
front1 = front;
if (front1 == NULL){
printf("\n Error: Trying to display elements from empty queue\n");
return;}
else
if (front1->ptr != NULL) {
front1 = front1->ptr;
printf("\n Dequed value : %d\n", front->data);
free(front);
front = front1;}
else{
printf("\n Dequed value : %d\n", front->data);
free(front);
front = NULL;
rear = NULL; }
count--;}
void transverse(){
front1 = front;
if ((front1 == NULL) && (rear == NULL)){

Roll no.16-ELE-03

61
Lab Report no.13

printf("Queue is empty");
return; }
while (front1 != rear) {
printf(" %d \n", front1->data);
front1 = front1->ptr;}
if (front1 == rear)
printf("%d\n", front1->data)}
void queuesize(){
printf("\n Queue size : %d", count);}
Output of Deque Operation:

Roll no.16-ELE-03

62

You might also like