Search This Blog

Wednesday 13 January 2016

Queue using Linked List

/*Queue using Linked List*/

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>

    typedef struct queue
    {

    int data;
    struct queue *next;

    }node;

    node *rear=NULL;
    node *front=NULL;
    node *getnode()
    {

    int val;
    node *temp;
    printf("Enter val:");
    scanf("%d",&val);
    temp=(node *)malloc(sizeof(node));
    temp->data=val;
    temp->next=NULL;
    return temp;

    }
    void insert()
    {

    node *neww;
    neww=getnode();
    if(front==NULL)
    {
         front=rear=neww;
    }
    else
    {
         rear->next=neww;
         rear=neww;
    }

    }
    void del()
    {

    if(front==NULL)
    {
         printf("\nQueue is Empty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n%d is deleted\n",front->data);
         front=front->next;
         t->next=NULL;
         free(t);
    }

    }
    void display()
    {

    if(front==NULL)
    {
          printf("\nEmpty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n");
         while(t)
         {
              printf("%d ",t->data);
              t=t->next;
         }
         printf("\n");
    }

    }
    void main()
    {

    int ch;
    clrscr();
    while(1)
    {
    printf("\n1.insert\n2.delete\n3.display\n4.exit\nYour choice:");
    scanf("%d",&ch);

    switch(ch)
    {
    case 1:insert();
    break;

    case 2:del();
    break;

    case 3:display();
    break;

    case 4:exit(0);
    break;

    default:printf("\nProper choice plz!\n");
    }

    }
    }

No comments:

Post a Comment