Friday, 22 June 2012

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


/*write a menu driven program using c for dynamic implementation
of Queue for integer. the menu include
-Insert
-delete
-display
-exit
*/
#include<stdio.h>
#include<conio.h>
struct queue
{
int no;
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 element:-");
scanf("%d",&p->no);
p->next=NULL;
if(start==NULL)
start=p;
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
void delete()
{
struct queue *temp;
int value;
if(start==NULL)
printf("\nQueue is empty");
else
{
temp=start;
value=temp->no;
printf("\nthe deleted element is :-%d",value);
start=start->next;
free(temp);
}
}
void display()
{
struct queue *temp;
temp=start;
if(start==NULL)
printf("\nQueue is empty");
else
{
printf("\nthe element in Queue are:-\n");
while(temp->next!=NULL)
{
printf("%d\n",temp->no);
temp=temp->next;
}
printf("%d\n",temp->no);
}
}

/*
1:insert

2:delete

3:display

4:exit

enter your choice:-1

enter the element:-6

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

2:delete

3:display

4:exit

enter your choice:-1

enter the element:-7

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

1:insert

2:delete

3:display

4:exit

enter your choice:-1

enter the element:-4

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

1:insert

2:delete

3:display

4:exit

enter your choice:-2

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

1:insert

2:delete

3:display

4:exit

enter your choice:-3

the element in Queue are:-
7
4

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):-n
*/
/*

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: