Queue using linklist

Program of queue using linked list
# include<stdio.h>
# include<malloc.h>
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
main()
{
int choice;
while(1)
{
       printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()
*/
insert()
{
struct node *temp;
int added_item;
temp = (struct node *)malloc(sizeof(struct node));
printf("Input the element for adding in queue : ");
scanf("%d",&added_item);
temp->info = added_item;
temp->link=NULL;
if(front==NULL) /*If Queue is empty*/
front=temp;
else
rear->link=temp;
rear=temp;
}/*End of insert()*/
del()
{
struct node *temp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
temp=front;
printf("Deleted element is  %d\n",temp->info);
front=front->link;
free(temp);
}
}/*End of del()*/
display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("Queue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
printf("\n");
}/*End of else
*/
}/*End of display()*/

ليست هناك تعليقات:

إرسال تعليق