Wednesday, 21 September 2016

Queue Using Singly Linked List

Queue Using Singly Linked List



#include<stdio.h>
#include<stdlib.h>
struct queue
{
            int data;
            struct queue *link;
};

typedef struct queue * NODE;

NODE front = NULL;
NODE rear = NULL;

NODE getnode()
{
            NODE x;
            x = (NODE)malloc(sizeof(struct queue));
            x->link = NULL;
}

void insert_rear(int item)
{
            int val;
            NODE temp, cur;
            temp = getnode();
            temp->data = item;

            /*Case 1: List is empty*/
            if(front == NULL)
            {
                        front = rear =temp;
            }
            else
            {
                        rear->link = temp;
                        rear = rear->link;
            }
}

NODE delete_front()
{
            NODE temp;
            if(front == NULL)
            {
                        printf("\nQueue underflow");
                        return NULL;
            }
            else
            {
                        temp = front;
                        front = front->link;
                        temp->link = NULL;
                        printf("\nItem got deleted is: %d", temp->data);
                        free(temp);
                        return front;
            }
}

void display()
{
            NODE cur;
            if(front == NULL)
            {
                        printf("\nQueue is empty");
             }
            else
            {
                        printf("\nQueue contents are:\n");
                        for(cur = front; cur != rear->link ; cur  = cur->link)
                                    printf("%d   ", cur->data);
            }
}
void main()
{
            int ch, item;
            while(1)
            {
                        printf("\n~~MENU~~");
                        printf("\n1.Insert operation");
                        printf("\n2.Delete operation");
                        printf("\n3.Display");
                        printf("\nEnter your choice: ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1: printf("\nEnter the item to be inserted: ");
                                                scanf("%d", &item);
                                                insert_rear(item);
                                                display();
                                                break;
                                    case 2: front = delete_front();
                                                display();
                                                break;
                                    case 3: display();
                                                break;
                                    case 4: exit(0);
                        }
            }
}

Output
~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 1

Enter the item to be inserted: 11
Queue contents are:
11

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 1

Enter the item to be inserted: 12
Queue contents are:
11        12

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 1

Enter the item to be inserted: 13
Queue contents are:
11        12        13

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 1

Enter the item to be inserted: 14
Queue contents are:
11        12        13         14

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 2

Item got deleted is: 11
Queue contents are:
12        13        14

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 2

Item got deleted is: 12
Queue contents are:
13        14

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 2

Item got deleted is: 13
Queue contents are:
14

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 2

Item got deleted is: 14
Queue is empty

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 2

Queue underflow
Queue is empty

~~MENU~~
1.Insert operation
2.Delete operation
3.Display
Enter your choice: 4