PROGRAM-4: Implementing stack using Linked List.
Date: 29-08-2025
#include<stdio.h>
#include<stdlib.h>
int q[30],n,f=-1,r=-1;
void enqueue(int x){
if(r==n-1){
printf("\noverflow\n");
else if(f==-1){
f=0;
r=0;
q[r]=x;
printf("\nElement entered\n");
else{
q[++r]=x;
printf("\nElement entered\n");
void dequeue(){
if(r==-1){
printf("\nunderflow\n");
else if(f==r && r!=-1){
printf("Element deleted: %d\n",q[r--]);
else{
printf("Elemenet deleted: %d\n",q[r--]);
void display(){
if(r==-1){
printf("\nqueue empty\n");
else{
printf("\nThe queue elements are:");
for(int i=f;i<=r;i++){
printf("\t%d",q[i]);
void quit(){
printf("\nExiting the program.");
exit(0);
int main()
int choice,x;
printf("Name: Krishna \nRoll No: 24E51A6792\n");
printf("\nEnter the size: ");
scanf("%d",&n);
while(1){
printf("\n1 for enqueue \n2 for dequeue \n3 for display \n4 for exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the element to be inserted: ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
quit();
break;
default:
printf("Invalid input\n");
break;
return 0;
}
PROGRAM-5: Implementing Queue using Linked List. Date: 29-08-2025
#include<stdio.h>
#include<malloc.h>
struct node
int val;
struct node *next;
};
struct node *head=NULL,*p,*q,*nw;
void enqueue();
void dequeue();
void display();
int main()
printf("Name: Krishna \nRoll No: 24E51A6792\n");
int ch;
while (1)..
printf(" \nEnter 1 for Enqueue \nEnter 2 for Dequeue \nEnter 3 for display \
nEnter 4 for exit: ");
scanf("%d",&ch);
if (ch==1)
enqueue();
else if (ch==2)
dequeue();
else if (ch==3)
display();
else if (ch==4)
break;
else
printf("\nInvalid choice.\n");
void enqueue()
int x;
printf("\nEnter value: ");
scanf("%d",&x);
nw=(struct node*)malloc(sizeof (struct node));
nw->val=x;
nw->next=NULL;
if(head==NULL)
head=nw;
else
p=head;
while(p->next!=NULL)
{
p=p->next;
p->next=nw;
printf("\nvalue inserted.\n");
void dequeue()
if(head==NULL)
printf("\nLinked list is empty.\n");
else
p=head;
head=p->next;
free(p);
printf("\nvalue deleted.\n");
void display()
if(head==NULL)
printf("\nLinked list is empty.\n");
else
{
p=head;
printf("\nThe linked list is: ");
while(p!=NULL)
printf("%d-->",p->val);
p=p->next;
}.