C Program to implement a Queue using a linked list...

#include<stdio.h>

int flag;     // Temporary variable to handle the error condition in dequeue function...

struct node
{
    int data;
    struct node* next;
};

struct queue
{
    struct node* front,* rear;
};

void enqueue(struct queue *p,int x);
int dequeue(struct queue *p);
void display(struct queue q);

void main()
{
    struct queue q;
    q.front=NULL;
    q.rear=NULL;
    int ch,val;
    int breakcondition=0;      // Temporary variable to exit the menu driven Program..
    printf("Choice:\n1) Enqueue.\n2) Dequeue.\n3) Display.\n4) Exit.");
    while(1){
        printf("\nEnter your choice..");
        scanf("%d",&ch);
        switch(ch){
                case 1: {
                            printf("\nEnter the element that you want to insert..");
                            scanf("%d",&val);
                            enqueue(&q,val);
                            display(q);
                            break;
                        }
                case 2:{
                            flag=0;
                            int temp=dequeue(&q);
                            if(flag==0){
                                printf("\nThe element which is deleted is..%d",temp);
                                display(q);
                            }
                            break;
                        }
                case 3:{
                            display(q);
                            break;
                        }
                case 4:{
                            breakcondition=1;
                            break;
                        }
                default: printf("\nEnter  a valid choice..");
        }
        if(breakcondition==1)break;
    }

}

void enqueue(struct queue *p,int x)
{
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=x;
    if(p->rear==NULL){
        newnode->next=NULL;
        p->front=p->rear=newnode;
    }
    else{
        p->rear->next=newnode;
        newnode->next=NULL;
        p->rear=newnode;
    }
}

int dequeue(struct queue *p) 
{
    int ans;
    struct node* temp=p->front;
    if(temp==NULL){                   // Corner case to handle if the queue is empty....
        flag=1;
        printf("\nThe queue is already Empty..");
        return ans;
    }
    else if(p->front==p->rear){     // Corner case to handle if the queue contains only one element..
        ans=temp->data;
        p->front=p->rear=NULL;
        free(temp);
        return ans;
    }
    ans=temp->data;
    p->front=p->front->next;
    free(temp);
    return ans;
}

void display(struct queue s)
{
    struct node* trav=s.front;
    if(trav==NULL){                      // Corner case to handle if the queue is empty....
        printf("\nThe queue is empty..");
        return;
    }
    printf("\nThe queue is ..");
    while(trav!=NULL){
        printf("%d ",trav->data);
        trav=trav->next;
    }
}





No comments:

Post a comment