Experiment 7
Experiment 7
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.
Algorithm:
Insertion of element(Enqueue)
---------------------------------------------
Algorithm
---------------------------------------------
3. p=(node *)malloc(sizeof(node));
4. p->data=n p->next=NULL
7. Stop
Program
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
}*front,*rear,*temp,*empty;
void dequeue();
void display();
int peek();
int count = 0;
void main()
do
printf("___________MENU___________\n");
printf("_____________________________________\n");
switch(option)
case 1:
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)
else
printf("EXITING....\n\n");
break;
case 5:
printf("\n\n");
break;
default:
printf("\n");
break;
}while(option!=5);
if (rear == NULL)
rear->pointer = NULL;
rear->data = data;
front = rear;
else
rear->pointer = temp;
temp->data = data;
temp->pointer = NULL;
rear = temp;
count++;
void display()
empty = front;
return;
}
printf("%d\t", empty->data);
empty = empty->pointer;
if (empty == rear)
printf("%d", empty->data);
void dequeue()
empty = front;
if (empty == NULL)
return;
else
if (empty->pointer != NULL)
empty = empty->pointer;
free(front);
front = empty;
else
free(front);
front = NULL;
rear = NULL;
count--;
int peek()
return(front->data);
else
return 0;