Program of queue using Linked List

As in Stacks, a queue could be implemented using a Linked List. A queue can be implemented by the following declarations and algorithms:

 

# include<stdio.h>

# include<malloc.h>

struct node

{

                int infoo;

                struct node *link;

}*front=NULL,*rear=NULL;

main()

{

                int choicee;

                clrscr();

                while(1)

                {       printf("Press 1.Insert\n");

                                printf("Press 2.Delete\n");

                                printf("Press 3.Display\n");

                                printf("Press 4.Quit\n");

                                printf("Enter your choicee : ");

                                scanf("%d", &choicee);

                                switch(choicee)

                                {

                                case 1:

                                                insert();

                                                break;

                                case 2:

                                                del();

                                                break;

                                case 3:

                                                display();

                                                break;

                                case 4:

                                                exit(1);

                                default :

                                                printf("Wrong choicee\n");

                                }/* switch end*/

                }/* while end*/

}/*main() end*/

insert()

{

                struct node *tmp;

                int added_itemm;

                tmp = (struct node *)malloc(sizeof(struct node));

                printf("Insert the element  : ");

                scanf("%d",&added_itemm);

                tmp->infoo = added_itemm;

                tmp->link=NULL;

                if(front==NULL)                               

                                front=tmp;

                else

                                rear->link=tmp;

                rear=tmp;

}/* insert() end*/

del()

{

                struct node *tmp;

                if(front == NULL)

                                printf("Queue Underflow\n");

                else

                {

                                tmp=front;

                                printf("Deleted element is  %d\n",tmp->infoo);

                                front=front->link;

                                free(tmp);

                }

}/* del() end*/

display()

{

                struct node *ptr;

                ptr = front;

                if(front == NULL)

                                printf("Oh!Queue is empty\n");

                else

                {

                                printf("Queue elements… :\n");

                                while(ptr != NULL)

                                {

                                                printf("%d ",ptr->infoo);

                                                ptr = ptr->link;

                                }

                                printf("\n");

                }/* else end*/

}/* display() end*/

Output.....

Queue with Linked List