You are on page 1of 5

Department of Information Technology

Semester S.E. Semester III – INFT


Subject DSA
Laboratory AVL
Teacher:
Laboratory L05

Student Name Siddhi Shinde


Roll Number 22101A0002
Grade and
Subject Teacher’s
Signature

Experiment 6
Number
Problem write a C program to implement queue using Linked List
Statement

Resources / Hardware: Desktop/Laptop Software: Dev C++


Apparatus
Required
Code: #include <stdio.h>
#include <stdlib.h>

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

struct node *front = NULL;


struct node *rear = NULL;

void push()
{
int x;
printf("Enter the data: ");
scanf("%d", &x);
struct node *newnode = (struct node *)malloc(sizeof(struct
node));
newnode->data = x;
newnode->next = NULL;
if (rear == NULL) // Check rear for emptiness
{
front = rear = newnode;
}
else
{
rear->next = newnode;
rear = newnode;
}
}

void pop()
{
if (front == NULL)
printf("Underflow\n");
else
{
struct node *temp = front;
printf("%d",front->data);
front = front->next;
free(temp); // Free memory of the removed node
}
}

void display()
{
struct node *temp = front; // Start from the front
printf("The elements are: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main()
{
int ch;
do
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
scanf("%d", &ch);

switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid input\n");
}
} while (ch != 4);

return 0;
}

Output:

You might also like