Tuesday, 9 October 2018

Implementation of Queue using Link List (Menu driven Program)

Implementation of Queue using Link List (Menu driven Program)



Queue is linear data structure which operates on particular order of operation such as First in First out(FIFO).


Example: 

Line of student at admission counter, line of people at ATM machine etc

Pointer used for queue:
front-point to first arrived i.e. oldest element
rear-point to last arrived i.e.latest element 


Main operations

Enqueue:

Add element into queue at rear side, at every new arrival of element rear will point to latest arrivel element .

Dequeue:

Delete element of queue at front side,front will updated to next of front ,use dummy pointer for this operstion.


Node Structure:

struct node
{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;


Functions used in this program:

int frontelement();

void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

Menu Driven Program:

int no, ch, e;
    printf("\n 1 - Enque");
    printf("\n 2 - Deque");
    printf("\n 3 - Front element");
    printf("\n 4 - Empty");
    printf("\n 5 - Display");
    printf("\n 6 - Queuesize");
    printf("\n 7 - Exit");
    create();
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Enter data for Enque: ");
            scanf("%d", &no);
            enq(no);
                        printf("\n Element of Queue after Enqueque operation:");
                        display();
           
            break;
        case 2:
            deq();
            printf("\n Element of Queue after deque operation:");
                        display();
            break;
        case 3:
            e = frontelement();
            if (e != 0)
                printf("Front element : %d", e);
            else
                printf("\n No front element in Queue as queue is empty");
            break;
        case 4:
            empty();
            break;
        case 5:
        display();
            break;
          
        case 6: queuesize();
            break;
           
        case 7:
            exit(0);
        default:
            printf("Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}


No comments:

Post a Comment