WHAT'S NEW?
Loading...

Queue implemented in linked list


/*Queue implemented in linked list*/
#include<stdio.h>

struct llist {
    int info;
    struct llist *next;
};
struct llist *front, *rear;

void insrt() {
    struct llist *nnode;
    nnode = (struct llist *) malloc(sizeof(struct llist));
    printf("\nEnter the data: ");
    scanf("%d", &nnode->info);
    nnode->next = NULL;
    if (front == NULL)
        front = rear = nnode;
    else {
        rear->next = nnode;
        rear = nnode;
    }
}

void delt() {
    if (front == NULL) {
        printf("\nEmpty queue!");
        return;
    }
    if (front->next == NULL) {
        printf("\nThe deleted item is: %d", front->info);
        front = rear = NULL;
        return;
    }
    printf("\nThe deleted item is: %d", front->info);
    front = front->next;
}

void display() {
    struct llist *temp;
    if (front == NULL) {
        printf("\nEmpty queue!");
        return;
    }
    temp = front;
    printf("\nThe queue items are:\n");
    while (temp->next != NULL) {
        printf("%5d", temp->info);
        temp = temp->next;
    }
    printf("%5d", temp->info);
}

void main() {
    int ch;
    front = rear = NULL;
    while (1) {
        printf("\nMENU");
        printf("\n1.Insert\n2.Delete");
        printf("\n3.Display all elements\n4.Exit");
        printf("\n\nEnter the choice: ");
        scanf("%d", &ch);
        switch (ch) {
        case 1:
            insrt();
            break;
        case 2:
            delt();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(1);
        default:
            printf("\nInvalid Choice!\n");
        }
    }
}

0 comments:

Post a Comment