0% found this document useful (0 votes)
1 views5 pages

Queue Using Linked List

Uploaded by

koushalpradhan27
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)
1 views5 pages

Queue Using Linked List

Uploaded by

koushalpradhan27
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

Program

----------------------------------------------------------------------------------------------------------------------
#include<stdio.h> // Standard Input Output header
#include<stdlib.h> // For exit() function

void EnQueue();
void DeQueue();
void Display(); // Function declarations

struct node
{
int data;
struct node *link;
}; // Node structure for Linked List

struct node *front = NULL;


struct node *rear = NULL;
struct node *tmp; // Global pointers

void main()
{
int ch;
char repeat; // Local variables are declared

do // Menu-driven loop
{
printf("-----------------\n");
printf("***MENU***\n");
printf("1. ENQUEUE\n");
printf("2. DEQUEUE\n");
printf("3. DISPLAY\n");
printf("-----------------\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1:
EnQueue(); // Calls EnQueue operation
break;

case 2:
DeQueue(); // Calls DeQueue operation
break;

case 3:
Display(); // Calls Display operation
break;

default:
printf("Invalid choice. Try Again."); // Handles wrong input
}

printf("\n---------------------------------------------------");
printf("\nDo you want to continue(Y/y for YES or N/n for NO)? ");
scanf(" %c", &repeat); // " %c" ensures newline from buffer is ignored

}
while(repeat == 'Y' || repeat == 'y');

printf("You are out of my Software.");


exit(0);
}

void EnQueue()
{
int n; // Local variable is declared

printf("Enter an element: ");


scanf("%d", &n);
tmp = malloc(sizeof(struct node));
if(tmp == NULL)
{
printf("Memory allocation failed! Insert operation not possible.\n");
return;
}
tmp->data = n;
tmp->link = NULL;
if(front == NULL)
{
front = tmp;
}
else
{
rear->link = tmp;
}
rear = tmp;
printf("A new Node with value %d has been added to the rear of the QUEUE.", n);
}

void DeQueue()
{
if(front == NULL)
{
printf("QUEUE Underflow.");
return;
}
tmp = front;
front = front->link;
if(front == NULL)
{
rear = NULL;
}
printf("Node having value %d has been removed from the front of the QUEUE.", tmp->data);
free(tmp);
}

void Display()
{
if(front == NULL)
{
printf("QUEUE is empty.");
return;
}
printf("The QUEUE elements are: ");
tmp = front;
while(tmp != NULL)
{
printf("%d ", tmp->data);
tmp = tmp->link;
}
}

Variable Description Table


----------------------------------------------------------------------------------------------------------------------
Name Type Purpose
data int Stores the integer value inside a Queue’s node.
link struct node* Points to the next node in the Queue.
front struct node* Points to the first node in the Queue.
rear struct node* Points to the last node in the Queue.
Temporary pointer used during EnQueue(), DeQueue and Display()
tmp struct node*
operations.
ch Int Stores user’s menu choice (1 = EnQueue, 2 = DeQueue, 3 = Display).
repeat char Stores user’s choice to repeat menu (Y/y for yes, N/n for no).
n Int Stores the element entered by user to be added onto the Queue.
Output Console
---------------------------------------------------------------------------------------------------------------------------------
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 1
Enter an element: 10
A new Node with value 10 has been added to the rear of the QUEUE.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 1
Enter an element: 20
A new Node with value 20 has been added to the rear of the QUEUE.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 1
Enter an element: 30
A new Node with value 30 has been added to the rear of the QUEUE.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 3
The QUEUE elements are: 10 20 30
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 2
Node having value 10 has been removed from the front of the QUEUE.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 3
The QUEUE elements are: 20 30
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 2
Node having value 20 has been removed from the front of the QUEUE.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 2
Node having value 30 has been removed from the front of the QUEUE.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? y
-----------------
***MENU***
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
-----------------
Enter your choice: 2
QUEUE Underflow.
---------------------------------------------------
Do you want to continue(Y/y for YES or N/n for NO)? n
You are out of my Software.
Process returned 0 (0x0) execution time : 1150.702 s
Press any key to continue.

You might also like