Stack n Queue Using Sll
Stack n Queue Using Sll
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:
Problem Write a program in C to perform stack and queue using linked list. Verify its operations by
Statement following their principle of operation as LIFO, FIFO.
//STACK
#include<stdio.h>
struct stack
{
int data;
struct stack *next;
};
typedef struct stack stk;
stk *top=NULL;
void push(int value)
{
stk *temp;
temp=(stk *)malloc(sizeof(stk));
temp->next=NULL;
temp->data=value;
if(top==NULL)
{
top=temp;
}
else
{
temp->next=top;
top=temp;
}
printf("\nElement pushed is %d",value);
}
void pop()
{
stk *temp;
if(top==NULL)
{
printf("\nStack underflow");
}
else
{
temp=top;
printf("\nPopped element is %d",top->data);
top=top->next;
free(temp);
}
}
void display()
{
if(top==NULL)
{
printf("\nStack underflow");
}
else
{
stk *trav;
printf("\nStack is:");
for(trav=top;trav!=NULL;trav=trav->next)
{
printf("\n%d",trav->data);
}
}
}
void main()
{
int c,value;
while(1)
{
printf("\n\nEnter option:\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nEnter value to be pushed: ");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------
//OUTPUT
Enter option:
1.Push
2.Pop
3.Display
4.Exit
1
Element pushed is 4
Enter option:
1.Push
2.Pop
3.Display
4.Exit
1
Element pushed is 5
Enter option:
1.Push
2.Pop
3.Display
4.Exit
1
Element pushed is 6
Enter option:
1.Push
2.Pop
3.Display
4.Exit
3
Stack is:
6
5
4
Enter option:
1.Push
2.Pop
3.Display
4.Exit
2
Popped element is 6
Enter option:
1.Push
2.Pop
3.Display
4.Exit
2
Popped element is 5
Enter option:
1.Push
2.Pop
3.Display
4.Exit
3
Stack is:
4
Enter option:
1.Push
2.Pop
3.Display
4.Exit
4
------------------------------------------------------------------------------------------------------------------------------------------
//QUEUE
#include<stdio.h>
struct queue
{
int data;
struct queue *next;
};
typedef struct queue Q;
Q *front=NULL;
Q *rear=NULL;
void insert(value)
{
Q *temp;
temp=(Q *)malloc(sizeof(Q));
temp->next=NULL;
temp->data=value;
printf("\nInserted value is %d",value);
if(front==NULL)
{
front=temp;
}
else
{
rear->next=temp;
}
rear=temp;
}
void del()
{
if(front==NULL)
{
printf("\nQueue is empty");
}
else
{
Q *temp;
temp=front;
printf("\nRemoved elment is: %d",front->data);
front=front->next;
if(front==NULL)
{
rear=NULL;
}
free(temp);
}
}
void display()
{
if(front==NULL)
{
printf("\nQueue is empty");
}
else
{
Q *trav;
trav=front;
printf("\nQueue is: ");
while(trav!=NULL)
{
printf("%d ",trav->data);
trav=trav->next;
}
}
}
void main()
{
int c,value;
while(1)
{
printf("\n\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nEnter value to be added: ");
scanf("%d",&value);
insert(value);
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}
//OUTPUT
1.Insert
2.Delete
3.Display
4.Exit
1
Inserted value is 1
1.Insert
2.Delete
3.Display
4.Exit
1
Inserted value is 2
1.Insert
2.Delete
3.Display
4.Exit
1
Inserted value is 3
1.Insert
2.Delete
3.Display
4.Exit
3
Queue is: 1 2 3
1.Insert
2.Delete
3.Display
4.Exit
2
1.Insert
2.Delete
3.Display
4.Exit
2
1.Insert
2.Delete
3.Display
4.Exit
3
Queue is: 3
1.Insert
2.Delete
3.Display
4.Exit
4