cursor

pictures

Friday, 17 August 2012

QUEUE IMPLEMENTATION using LINKED LIST

Here i attached the program codings of Queue Implementation using linked list....!

Hopeit will be helpful..! :)


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
} *front, *rear;
void enqueue(int ele);
int dequeue();
void display();
int main()
{
    int ch, ele;
    rear = NULL;
    front = NULL;
    while (1)
    {   printf("\n***QUEUE IMPLEMENTATION USING LINKED LIST*** \n");
        printf("************ Menu ***************");
        printf("\nEnter:\n1 . Enqueue (INSERTION)\n2 . Dequeue (DELETION)\n3 . Display\n4 . Exit\n");
        printf("Enter your choice :: ");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
            printf("Enter The Element Value\n");
            scanf("%d", &ele);
            enqueue(ele);
            break;
            case 2:
            ele = dequeue();
            printf("The deleted element = %d\n", ele);
            break;
            case 3:
            display();
            break;
            case 4 :
            exit(0);
            break;
            default:
            printf("Invalid choice..!");
            getch();
            break;
        }
    }
}
void enqueue(int ele)
{
    struct node *p;
    p = (struct node*)malloc(sizeof(struct node));
    p->data = ele;
    p->next = NULL;
    if (rear == NULL || front == NULL)
    front = p;
    else
    rear->next = p;
    rear = p;
    display();
}
int dequeue()
{
    struct node *p;
    int ele;
    if (front == NULL || rear == NULL)
    {
        printf("\nUnder Flow");
        getch();
        exit(0);
    }
    else
    {
        p = front;
        ele = p->data;
        front = front->next;
        free(p);
    }
    return (ele);
    display();
}
void display()
{
    struct node *t;
    t = front;
    while (front == NULL || rear == NULL)
    {
        printf("\nQueue is empty");
        getch();
        exit(0);
    }
    while (t != NULL)
    {
        printf("->%d", t->data);
        t = t->next;
    }
}

No comments:

Post a Comment