Friday, 22 June 2012

write a menu driven program using c for dynamic implementation of Queue for character.


/*write a menu driven program using c for dynamic implementation
of Queue for character. the menu include
-Insert
-delete
-display
-exit
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct queue
{
char c;
struct queue *next;
}*start=NULL;
void insert();
void delete();
void display();
void main()
{
int choice;
char ch='y';
clrscr();
do
{
printf("\n1:insert\n");
printf("\n2:delete\n");
printf("\n3:display\n");
printf("\n4:exit\n");
printf("\nenter your choice:-");
scanf("\n%d",&choice);
switch(choice)
{
case 1:insert();
       break;
case 2:delete();
       break;
case 3:display();
       break;
case 4:exit();
default:printf("\nyou entered wrong choice");
}
printf("\ndo you want to continue(Y|N):-");
scanf("%s",&ch);
}while(ch=='y'||ch=='Y');
getch();
}
void insert()
{
struct queue *p,*temp;
temp=start;
p=(struct queue *)malloc(sizeof(struct queue));
printf("\nenter the character:-");
flushall();
scanf("%c",&p->c);
p->next=NULL;
if(start==NULL)
start=p;
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
void delete()
{
struct queue *temp;
char value;
if(start==NULL)
printf("\nQueue is empty");
else
{
temp=start;
value=temp->c;
printf("\nthe deleted character is :-%c",value);
start=start->next;
free(temp);
}
}
void display()
{
struct queue *temp;
temp=start;
if(start==NULL)
printf("\nQueue is empty");
else
{
printf("\nthe characters in a Queue are:-\n");
while(temp->next!=NULL)
{
printf("%c\n",temp->c);
temp=temp->next;
}
printf("%c\n",temp->c);
}
}
/*
1:insert

2:delete

3:display

4:exit

enter your choice:-1

enter the character:-c

do you want to continue(Y|N):-y

1:insert

2:delete

3:display

4:exit

enter your choice:-1

enter the character:-d

do you want to continue(Y|N):-y

1:insert

2:delete

3:display

4:exit

enter your choice:-2

the deleted character is :-c
do you want to continue(Y|N):-y

1:insert

2:delete

3:display

4:exit

enter your choice:-3

the characters in a Queue are:-
d

do you want to continue(Y|N):-n
*/
/*
1:insert

2:delete

3:display

4:exit

enter your choice:-5

you entered wrong choice
do you want to continue(Y|N):-y

1:insert

2:delete

3:display

4:exit

enter your choice:-2

Queue is empty
do you want to continue(Y|N):-y

1:insert

2:delete

3:display

4:exit

enter your choice:-3

Queue is empty
do you want to continue(Y|N):-n
*/

No comments: