You are on page 1of 12

Practical no.

: 8
Name: Aishwarya Pardhi

Roll no.: IT1-19

Subject: Data structures

Aim: Write a program to implement stack and queue using linked list.
Description:
STACK: This program contains algorithm for implementing stack using
linked list maintaining LIFO principle. In the push() function
createNode() method is called so that node gets created for pushing the
value. In the node, there are two blocks; 1st block contains data of stack
whereas 2nd block contains address of next node. The pop() function
basically performs the action of deleting node from the end.

QUEUE: We can implement queue using linked list, this practical


provides algorithm for queue using linked list which comprises of three
important functions. These functions ensure that queue follows FIFO
principle. In the enqueue() function value is passed so as to get stored in
node. The dequeue() function ensures that first entry of queue that is first
node in linked list is deleted. The display() function displays all the
entries of queue.
The algorithm for the proposed task is as follows-
STACK: struct node1*
createNode()
1. declare variable, struct node1 *
2. create node dynamically.
3. return n
void push(struct node1 **s, int
value)
1. declare variable, struct
node1 *
2. Call new<-createNode()
3. Pass value in node,new-
>item.
4. Check start pointer, if
NULL
then, assign new->next=NULL
else, new->next=*s, *s=new.
5. end
void pop(struct node1 **s)
1. declare variable, struct
node1 *temp
2. check for null condition,
If(*s==NULL), display
message “Unable to pop.”
3. else,
temp=*s, *s=temp->next
display the value popped.
4. free(temp).
5. end.
void display1(struct node1 **s)
1. declare variables, struct
node1 *t
2. assign, t<-*s
3. check for Empty condition,
If *s==NULL, display
message “Stack is underflow”
4. else,
4.1 print value
4.2 t=t->next
5. repeat steps 4.1 and 4.2 until
t!=NULL
6. end
QUEUE: void enq(int data)
1. if, rear==NULL,
1.1rear=(struct
node*)malloc(sizeof(struct
node));
1.2 assign, rear->ptr=NULL
1.3 assign, rear->info=data
1.4 assign front=rear
2. else,
2.1 temp=(struct
node*)malloc(sizeof(struct
node))
2.2 rear->ptr=temp;
2.3 temp->info=data;
2.4 temp->ptr=NULL
2.5 rear=temp;
3. Count++;
void display()
1. assign, front1=front
2. if, front1==NULL &&
rear==NULL
then display message of empty
Queue
return
3. display queue elements
4. front1=front1->ptr
5. repeat steps 3 and 4 until
front!=rear
6. if front1==rear,
Display value in front1->info
void deq()
1. assign, front1=front
2. if, front1==NULL,
display error message, return
3. else
3.1 if, front1->ptr!=NULL
3.1.1 front1=front1->ptr
3.1.2 display value to be
dequeued
3.1.3 free front node
3.1.4 front=front1
3.2 else,
3.2.1 display value to be dequeued
3.2.2 free the first node
3.2.3 front=NULL,
rear=NULL
4. count--
int frontelement()
1. if, front!=NULL &&
rear!=NULL
return(front->info)
2. else, return 0

Code:-
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
struct node1
{
int item; 
struct node1 *next;
};
struct node1* createNode()
{
struct node1 *n;
n=(struct
node1*)malloc(sizeof(struct
node1));
return n;
}
void push(struct node1 **s, int
value)
{
struct node1 *new;
new=createNode();
new->item=value;
if(*s==NULL)
{
*s=new;
new->next=NULL;
printf("\nValue has been pushed successfully");
}
else
{
new->next=*s;
*s=new;
printf("\nValue has been pushed successfully");
}
}
void pop(struct node1 **s)
{
struct node1 *temp;
if(*s==NULL) 
{
printf("\nUnable to pop");
}
else{
temp=*s;
*s=temp->next;
printf("\n%d has been popped out",temp->item);
free(temp);}
}
void display1(struct node1
**s)
{
struct node1 *t;
t=*s;
if(*s==NULL)
printf("\nStack is underflow");
else{
while(t!=NULL)
{
printf("%d\n",t->item);
t=t->next;
}
}
}
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq(); 
void empty();
void display();
void create();
void queuesize();
int count = 0;
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d",
count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node
*)malloc(1*sizeof(struct
node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node
*)malloc(1*sizeof(struct
node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;

count++;
}
void display()
{
front1 = front;
if ((front1 == NULL) &&
(rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d",
front->info); 
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d",
front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear
!= NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear
== NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
int main()
{
int no, ch, e;
struct node1 *start=NULL;
int choice,value;
while(1) 
{
printf("\n\nDS WORLDS\n1. Enter Stack World\n2. Enter Queue World");
printf("\nEnter your Choice:");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\n\nWELCOME TO STACK WORLD");
printf("\n\nMENU:\n1.Push the value\n2.Pop the value\n3.Display Stack\n4.Exit");
while(1)
{printf("\nEnter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the value you want to push:");
scanf("%d",&value);
push(&start,value);
break;
case 2:
pop(&start);
break;
case 3:
display1(&start);
break; 
case 4:
exit(0);
break;
default:
printf("\nInvalid choice");
break;
}
}
break;
case 2:
printf("\n\nWELCOME TO QUEUE WORLD");
printf("\n\nMENU:\n1 - Enqueue");
printf("\n 2 - Dequeue");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while(1)
{printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3: 
e = frontelement();
if (e != 0)
printf("Front element : %d",
e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
break;
default:
printf("\n\nChoose Displayed Worlds only");
break;
}
}
return 0;
}

OUTPUT-
Conclusion:
Here , The stack and queue were implemented using linked lists
concept.

You might also like