Answer to Question #253228 in C for henry

Question #253228

Write a menu driven program to implement queue operations

such as Enqueue, Dequeue, Peek, Display of elements, isEmpty

using linked list.


1
Expert's answer
2021-10-18T18:23:06-0400
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int info;
    struct node *next;
};


struct node *top = NULL, *bottom = NULL;


void enqueue(int num)
{
    struct node *newNode = malloc(sizeof(struct node));
    newNode->info = num;
    newNode->next = NULL;


    if(top == NULL && bottom == NULL)
        top = bottom = newNode;
    else
    {
        bottom->next = newNode;
        bottom = newNode;
    }
}


void dequeue()
{
    struct node *temp;


    if(top == NULL)
         printf("Queue is Empty!\n");
    else
    {
        temp = top;
        top = top->next;
        if(top == NULL)
            bottom = NULL;
       free(temp);
    }


}


void display()
{
    struct node *temp = top;


    while(temp)
    {
        printf("%d->",temp->info);
        temp = temp->next;
    }
    printf("NULL\n");
}


int main()
{
    enqueue(15);
    enqueue(25);
    enqueue(35);
    printf("Queue :");
    display();
    dequeue();
    printf("New Queue after dequeue:");
    display();
    dequeue();
    printf("New Queue after dequeue::");
    display();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS