Queue using linked list c program

#include
#include
struct node{
       int info;
       struct node *link;
       }*front=NULL,*rear=NULL;
       
       void insert(int num);
       int del();
       void display();
       
       main()
       {
             int choice, num;
             while(1)
             {
             printf("\n1 Insert\n");
             printf("2 Delete\n");
             printf("3 Display\n");
             printf("4 Exit\n");
             printf("Enter your choice\n");
             scanf("%d",&choice);
             switch(choice)
             {
                           case 1:
                                 printf("Enter an element to insert ");
                                 scanf("%d",&num);
                                 insert(num);
                                 break;
                           case 2:
                                del();
                                break;
                           case 3:
                                display();
                                break;
                           case 4:
                           	exit(1);
                           	
                           default:
                           	printf("Wrong Input\n");
                           	
             }
       }
}

void insert(int num)
{
     struct node *temp;
     temp=(struct node *)malloc(sizeof(struct node));
     if(temp==NULL)
     {
                  printf("Overflow\n");
                  return;
     }
     temp->info=num;
     temp->link=NULL;
     if(front==NULL)
      {
       front=temp;
      }
     else
      {
      rear->link=temp;
      }
       
	   rear=temp;
      }
     
     int del()
     {
          int num;
          struct node *temp;
          if(front==NULL)
          {
                         printf("Underflow\n");
                         return;
          }
          temp=front;
          num=temp->info;
          front=front->link;
          free(temp);
          return num;		
     }
     
     void display()
     {
                         struct node *p;
                         if(front==NULL)
                         {
                         printf("Underflow\n");
                         return;
                         }
                         p=front;
                         printf("\nQueue Elements:\n");
                         while(p!=NULL)
                         {
                                         printf("%d ",p->info);
                                         p=p->link;
                         }
                                         
                                  
                                  }         
Previous Post
Queue using linked list

Related Posts

1 Comment. Leave new

  • Teacher, I am having a problem printing/display alphabet with numbers, e.g. WKK123, it only display WKK1. I wonder you can help me on this. This is a question assign by my lecturer. hope you can help me with this
    You are required to code and test a C program to simulate a queue of buses at a bus stop.
    When a bus arrives, it joins the rear of the queue. When a bus leaves the queue, the
    bus at the front of the queue leaves. Initially the queue is empty. Each bus is identified
    by a bus registration number, bus type and capacity.

    The application program is to be menu-driven for the following tasks:-

    1. arrive: Display the bus details which is attached to the queue.
    2. leave: Display the bus details which leaves the queue.
    3. search: Display if a particular bus, given its registration number is in the queue.
    4. print: Display the registration numbers of all buses in the queue starting from the
    beginning of the queue.
    5. length: Display the number of buses in the queue.
    6. exit: Terminate the program

    The queue is to be implemented as linear linked list. Bus registration numbers are to be alpha-numeric, e.g. WKK1234. The menu options for the tasks above are to be strings, e.g. “arrive” etc.

    Both programs submitted should compile and be executed without errors. Besides that, validation should be done for each entry from the users in order to avoid logical errors.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu