0% found this document useful (0 votes)
11 views6 pages

Experiment 7

The document discusses the implementation of a queue using linked lists, highlighting the advantages of dynamic memory allocation. It outlines the basic operations of a queue, including enqueue, dequeue, display, and peek, along with a C program that demonstrates these functionalities. Additionally, it provides real-life applications of queues, such as task scheduling and call center management.

Uploaded by

muthusamthevar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Experiment 7

The document discusses the implementation of a queue using linked lists, highlighting the advantages of dynamic memory allocation. It outlines the basic operations of a queue, including enqueue, dequeue, display, and peek, along with a C program that demonstrates these functionalities. Additionally, it provides real-life applications of queues, such as task scheduling and call center management.

Uploaded by

muthusamthevar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Aim: Linked List implementation of Queue

Theory : Instead of using arrays, we can also use linked lists to implement stack and queue. Linked
list allocates the memory dynamically. However, time complexity in both the scenarios is same for all
the operations i.e. push, pop search. 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 overflow if the space left in the memory heap is not enough to create a
node.

Applications of Queue

Queue, as the name suggests is used whenever we need to manage any group of objects in
an order in which the first one coming in, also gets out first while the others wait for their turn, like
in the following scenarios :

1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.

2. In real life scenarios, Call Center phone systems use Queues to hold people calling them
in an order, until a service representative is free.

3. Handling of interrupts in real-time systems. The interrupts are handled in the same order
as they arrive i.e First come first served.

Basic operations performed on Queue

1. Adding new elements(enqueue)

2. Deleting existing elements(dequeue)

3. Viewing the frontmost element(peek)

4. Displaying all the element(Display)

Algorithm:

Insertion of element(Enqueue)

---------------------------------------------

Enqueue is used to add elements to the rear of the queue.

Algorithm

---------------------------------------------

1. Declare a node and variable n node *p; int n;

2. Allocate memory to the new node

3. p=(node *)malloc(sizeof(node));
4. p->data=n p->next=NULL

5. if(front==NULL and rear==NULL) { p->next=NULL front=rear=p }

6. else { rear_next=p; rear=p; }

7. Stop

Program

#include <stdio.h>

#include <stdlib.h>

struct node

int data;

struct node *pointer;

}*front,*rear,*temp,*empty;

void enqueue(int data);

void dequeue();

void display();

int peek();

int count = 0;

void main()

int no, option, e;

do

printf("___________MENU___________\n");

printf("1. TO INSERT AN ELEMENT\n");

printf("2. TO DELETE AN ELEMENT\n");

printf("3. TO DISPLAY ALL ELEMENTS\n");

printf("4. TO DISPLAY FRONT ELEMENT\n");

printf("5. TO EXIT THE PROGRAM\n");

printf("_____________________________________\n");

printf("ENTER YOUR OPTION : ");


scanf("%d", &option);

switch(option)

case 1:

printf("ENTER THE ELEMENT TO BE INSERTED : ");

scanf("%d", &no);

enqueue(no);

printf("\n\n");

break;

case 2:

dequeue();

printf("\n\n");

break;

case 3:

display();

printf("\n\n");

break;

case 4:

e = peek();

if (e != 0)

printf("ELEMENT IN THE FRONT IS : %d", e);

else

printf("\nEMPTY QUEUE :/");

printf("EXITING....\n\n");

break;

case 5:

printf("\n\n");

break;

default:

printf("CHOICE IS INVALID ");

printf("\n");
break;

}while(option!=5);

void enqueue(int data)

if (rear == NULL)

rear = (struct node *)malloc(1*sizeof(struct node));

rear->pointer = NULL;

rear->data = data;

front = rear;

else

temp=(struct node *)malloc(1*sizeof(struct node));

rear->pointer = temp;

temp->data = data;

temp->pointer = NULL;

rear = temp;

count++;

printf("ELEMENT INSERTED SUCCESSFULLY:)");

void display()

empty = front;

if ((empty == NULL) && (rear == NULL))

printf("\nEMPTY QUEUE :/");

return;
}

while (empty != rear)

printf("%d\t", empty->data);

empty = empty->pointer;

if (empty == rear)

printf("%d", empty->data);

void dequeue()

empty = front;

if (empty == NULL)

printf("\nEMPTY QUEUE :/");

return;

else

if (empty->pointer != NULL)

empty = empty->pointer;

printf("\nDELETED ELEMENT IS : %d", front->data);

free(front);

front = empty;

else

printf("\nDELETED ELEMENT IS : %d", front->data);

free(front);

front = NULL;
rear = NULL;

count--;

int peek()

if ((front != NULL) && (rear != NULL))

return(front->data);

else

return 0;

You might also like