Welcome To Uday Satya Blog

15 . QUEUE LINKED LIST



/* ****************************************************
    15 . TO PERFORM Q OPERATN USIN LINKED LISTS .
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*stdlib.h>
#include<*alloc.h>
struct link
{
int info;
struct link*next;
}*front,*rear,*new_rec,*temp;
void create();
void display();
void insert_rear();
void del_front();
void display()
{
temp=front;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
void create()
{
if(front==NULL)
{
front=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter first node : ");
scanf("%d",&front->info);
front->next=NULL;
rear=front;
}
else
printf("\n\n list already created");
}
void insert_rear()
{
if(front!=NULL)
{
printf("\n\n enter data into node : ");
new_rec=(struct link*)malloc(sizeof(struct link));
scanf("%d",&new_rec->info);
new_rec->next=NULL;
rear->next=new_rec;
rear=new_rec;
}
}
void del_front()
{
if(front==NULL)
{
printf("\n\n queue empty");
}
else
{
if(front==rear)
{
temp=front;
front=rear=NULL;
free(temp);
}
else
{
temp=front;
front=front->next;
free(temp);
}
printf("\n\n display of list after deletion : ");
display();
}}
void main()
{
int ch;
clrscr();
front=rear=NULL;
printf("\n");
printf("\n1.create");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.display");
printf("\n5.exit");
ch=1;
do
{
switch(ch)
{
case 1 : create();
            printf("\n\n displaying the elements in queue : ");
            display();
            break;
case 2 : insert_rear();
       printf("\n\n displaying the elements in queue after             
             insertion : ");
       display();
       break;
case 3 : printf("\n\n displaying the elements in queue after deletion : ");
       del_front();
       break;
case 4 : printf("\n\n displaying the elements in queue : ");
      display();
       break;
case 5 : printf("\n\n end of the program ");
      getch();
      exit(1);
}
printf("\n\n enter your choice(1-5) : ");
scanf("%d",&ch);
}
while(ch<=5);
getch();
}

OUTPUT

1.create
2.insert
3.delete
4.display
5.exit

 enter first node : 1

 displaying the elements in queue : 1

 enter your choice(1-5) : 2

 enter data into node : 2

 displaying the elements in queue after insertion : 1   2

 enter your choice(1-5) : 2

 enter data into node : 3

 displaying the elements in queue after insertion : 1   2       3

 enter your choice(1-5) : 1

 list already created

 displaying the elements in queue : 1   2       3

 enter your choice(1-5) : 4

 displaying the elements in queue : 1   2       3

 enter your choice(1-5) : 3

 displaying the elements in queue after deletion :

 display of list after deletion : 2     3

 enter your choice(1-5) : 3

 displaying the elements in queue after deletion :

 display of list after deletion : 3

 enter your choice(1-5) : 3

 displaying the elements in queue after deletion :

 display of list after deletion :

 enter your choice(1-5) : 5

 end of the program

0 comments:

Post a Comment