Untitled
unknown
plain_text
5 months ago
2.1 kB
3
Indexable
#include <stdio.h> #include <stdlib.h> // Structure to create a node with data and the next pointer struct node { int data; struct node * next; }; struct node * front = NULL; struct node * rear = NULL; // Enqueue() operation on a queue void enqueue(int value) { struct node * ptr; ptr = (struct node * ) malloc(sizeof(struct node)); ptr -> data = value; ptr -> next = NULL; if ((front == NULL) && (rear == NULL)) { front = rear = ptr; } else { rear -> next = ptr; rear = ptr; } printf("Node is Inserted\n\n"); } // Dequeue() operation on a queue int dequeue() { if (front == NULL) { printf("\nUnderflow\n"); return -1; } else { struct node * temp = front; int temp_data = front -> data; front = front -> next; free(temp); return temp_data; } } // Display all elements of the queue void display() { struct node * temp; if ((front == NULL) && (rear == NULL)) { printf("\nQueue is Empty\n"); } else { printf("The queue is \n"); temp = front; while (temp) { printf("%d--->", temp -> data); temp = temp -> next; } printf("NULL\n\n"); } } int main() { int choice, value; printf("\nImplementation of Queue using Linked List\n"); while (choice != 4) { printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n"); printf("\nEnter your choice : "); scanf("%d", &choice); switch (choice) { case 1: printf("\nEnter the value to insert: "); scanf("%d", &value); enqueue(value); break; case 2: printf("Popped element is :%d\n", dequeue()); break; case 3: display(); break; case 4: exit(0); break; default: printf("\nWrong Choice\n"); } } return 0; }
Editor is loading...
Leave a Comment