Create a menu driven program to implement stack, queue and circular queue using linked list and store your name.
#include<stdio.h>#include<conio.h>
#include<stdlib.h>
#include<string.h>
# define MAX 20
struct node
{
char name[MAX];
struct node *link;
};
int stack()
{
int opt;
char na[MAX];
struct node *tmp,*top=NULL,*q;
printf("\n............STACK.............");
while(1)
{
printf("\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.BACK TO MAIN MENU");
scanf("%d",&opt);
switch(opt)
{
case 1:
tmp=malloc(sizeof(struct node));
printf("\nEnter the name:\t");
scanf("%s",&na);
strcpy(tmp->name,na);
tmp->link=top;
top=tmp;
break;
case 2:
if(top==NULL)
printf("\nUNDERFLOW");
else
{
printf("\nPoped element:");
printf("\t%s",top->name);
top=top->link;
}
break;
case 3:
q=top;
if(q==NULL)
printf("\nEmpty stack");
else
{
printf("\nStack elements:");
while(q!=NULL)
{
printf("\t%s",q->name);
q=q->link;
}
}
break;
case 4:
return(0);
default:
printf("\nWrong choice");
break;
}
}
}
int queue()
{
int opt;
char na[MAX];
struct node *tmp,*front=NULL,*rear=NULL,*q;
printf("\n............Queue.............");
while(1)
{
printf("\n\t1.INSERTION\n\t2.DELETION\n\t3.DISPLAY\n\t4.BACK TO MAIN MENU");
scanf("%d",&opt);
switch(opt)
{
case 1:
tmp=malloc(sizeof(struct node));
printf("\nEnter the name:\t");
scanf("%s",&na);
strcpy(tmp->name,na);
tmp->link=NULL;
if(front==NULL)
front=rear=tmp;
else
{
rear->link=tmp;
rear=tmp;
}
break;
case 2:
q=front;
if(front==NULL)
printf("\nUNDERFLOW\n");
else
{
printf("\nDeleted element::\t");
printf("\t%s",front->name);
front=front->link;
free(q);
}
break;
case 3:
q=front;
if(q==NULL)
printf("\nEmpty Queue");
else
{
printf("\nQueue elements::\t");
while(q!=NULL)
{
printf("\t%s",q->name);
q=q->link;
}
}
break;
case 4:
return(0);
default:
printf("\nWrong choice");
break;
}
}
}
int cqueue()
{
int opt;
char na[MAX];
struct node *tmp,*front=NULL,*rear=NULL,*q;
printf("\n............Circular Queue.............");
while(1)
{
printf("\n\t1.INSERTION\n\t2.DELETION\n\t3.DISPLAY\n\t4.BACK TO MAIN MENU");
scanf("%d",&opt);
switch(opt)
{
case 1:
tmp=malloc(sizeof(struct node));
printf("\nEnter the name:\t");
scanf("%s",&na);
strcpy(tmp->name,na);
if(front==NULL)
{
front=rear=tmp;
front->link=rear;
rear->link=front;
}
else
{
rear->link=tmp;
tmp->link=front;
rear=tmp;
}
break;
case 2:
q=front;
if(front==NULL)
printf("\nUNDERFLOW\n");
else
{
printf("\nDeleted element::");
printf("\t%s",front->name);
if(front->link!=front)
{
front=front->link;
rear->link=front;
}
else
front=NULL;
free(q);
}
break;
case 3:
q=front;
if(q==NULL)
printf("\nEmpty Queue");
else
{
printf("\nQueue elements::");
printf("\t%s",q->name);
q=q->link;
while(q!=front)
{
printf("\t%s",q->name);
q=q->link;
}
}
break;
case 4:
return 0;
default:
printf("\nWrong choice");
break;
}
}
}
void main()
{
int opt;
clrscr();
while(1)
{
printf("\n\t......................MAIN MENU.......................\n");
printf("\n\t1.Stack\n\t2.Queue\n\t3.Circular Queue\n\t4.Exit");
scanf("%d",&opt);
switch(opt)
{
case 1:stack();break;
case 2:queue();break;
case 3:cqueue();break;
case 4:exit(0);
default:printf("Invalid option");break;
}
}
}
0 comments:
Post a Comment