0% found this document useful (0 votes)
6 views4 pages

QueueUsingLL Tilak Jain 42 H2

The document contains a C program that implements a queue using a linked list. It provides functions to enqueue, dequeue, and display elements in the queue, along with a simple menu-driven interface for user interaction. The program handles memory allocation and checks for underflow and overflow conditions.

Uploaded by

bitforbyte0108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

QueueUsingLL Tilak Jain 42 H2

The document contains a C program that implements a queue using a linked list. It provides functions to enqueue, dequeue, and display elements in the queue, along with a simple menu-driven interface for user interaction. The program handles memory allocation and checks for underflow and overflow conditions.

Uploaded by

bitforbyte0108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CODE :

#include <stdio.h>
#include <stdlib.h>

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

struct Node* front = NULL;


struct Node* rear = NULL;

void enqueue() {
int value;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap Overflow Cannot enqueue more elements\n");
return;
}
printf("Enter the value to enqueue\n");
scanf("%d", &value);
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued to queue\n", value);
}

void dequeue() {
if (front == NULL) {
printf("Queue Underflow Cannot dequeue from an empty queue\n");
} else {
struct Node* temp = front;
printf("%d dequeued from queue\n", temp->data);
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(temp);
}
}

void display() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
struct Node* temp = front;
printf("Queue elements ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int choice;
while (1) {
printf("\n1 Enqueue\n2 Dequeue\n3 Display\n4 Exit\n");
printf("Enter your choice\n");
scanf("%d", &choice);

switch (choice) {
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: printf("Exiting program\n"); return 0;

default: printf("Invalid choice Please try again\n");

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy