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

C Queue Implementation: Array & Linked List

The document outlines two experiments for implementing queue operations in C programming: one using arrays and the other using linked lists. Each implementation includes functions for enqueue, dequeue, and display, along with example code and user interaction through a menu. The conclusion states that queues have been successfully implemented using both methods.

Uploaded by

bro665444
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 views8 pages

C Queue Implementation: Array & Linked List

The document outlines two experiments for implementing queue operations in C programming: one using arrays and the other using linked lists. Each implementation includes functions for enqueue, dequeue, and display, along with example code and user interaction through a menu. The conclusion states that queues have been successfully implemented using both methods.

Uploaded by

bro665444
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

EXPERIMENT – 4 (A)

AIM

Write a program to implement Queue operations such as enqueue, dequeue and display using:

(a) Array.

INTRODUCTION

In C programming, a queue is a linear data structure that follows the First In First Out (FIFO) principle.
It is a list in which all additions to the list are made at one end, and all deletions from the list are made
at the other end.

In a queue, the first element that is added to the list is the first element to be removed from the list.
The element which is first pushed into the order, the operation is first performed on that.

In C, a queue can be implemented using an array or a linked list. The array implementation of a queue
requires two variables, one for the front of the queue and the other for the rear of the queue. The
linked list implementation of a queue requires a pointer to the front of the queue and a pointer to the
rear of the queue.

CREATING A QUEUE VIA ARRAY

This is a C program that implements a queue using an array. The program defines a queue of maximum
size MAX_SIZE and provides the following operations:

• Enqueue: Adds an element to the queue. If the queue is full (if rear==maxsize-1), it prints
“Queue overflow.” and returns. Otherwise, it stores the element at the next available index in
the array and increments rear by 1.

• Dequeue: Removes an element from the queue. If the queue is empty (if rear||front==-1), it
prints “Queue underflow.” and returns. Otherwise, it prints the element at the front of the
queue and increments front by 1. If front becomes greater than rear, it means the queue is
now empty and both front and rear are reset to -1.

• Display: Prints all elements of the queue. If the queue is empty, it prints “Queue is empty.”
and returns.

The program uses a while loop to repeatedly prompt the user to choose an operation to perform. The
user’s choice is read from standard input using scanf(). The program then uses a switch statement to
call the appropriate function based on the user’s choice.
CODE OF QUEUE WITH ARRAY

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

int queue[MAX_SIZE];

int front = -1, rear = -1;

void enqueue(int value) {

if (rear == MAX_SIZE - 1) {

printf("Queue overflow.\n");

return;

if (front == -1) {

front = 0;

queue[++rear] = value;

printf("Element inserted successfully.\n");

void dequeue() {

if (front == -1) {

printf("Queue underflow.\n");

return;

printf("Element dequeued: %d\n", queue[front++]);

if (front > rear) {

front = rear = -1;


}

void display() {

if (front == -1) {

printf("Queue is empty.\n");

return;

printf("Elements of the queue are: ");

for (int i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\nChoose an operation to perform:\n");

printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to insert: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

display();

break;
case 4:

exit(0);

default:

printf("Invalid choice.\n");

return 0;

RESULT

MAIN MENU

INSERTION OF 1ST ELEMENT

INSERTION OF ONE MORE ELEMENT

DISPLAY OF FINAL QUEUE

DEQUEUE

DISPLAY AFTER DEQUEUEING

CONCLUSION

Queue has been successfully implemented using arrays having operations enqueue(), dequeue(),
display() in c.
EXPERIMENT 4(B)

AIM

Write a program to implement Queue operations such as enqueue, dequeue and display using Linked
lists.

THEORY

This is a C program that implements a queue using a linked list. The program defines a struct Node that
contains an integer data value and a pointer to the next node in the list. The program also defines two
pointers front and rear that point to the front and rear of the queue, respectively.

The program provides the following operations:

• Enqueue: Adds an element to the queue. If the queue is empty, it creates a new node and sets
both front and rear to point to it. Otherwise, it creates a new node and sets the next pointer
of the current rear node to point to it. It then sets rear to point to the new node.

• Dequeue: Removes an element from the queue. If the queue is empty, it prints “Queue
underflow.” and returns. Otherwise, it removes the node pointed to by front and sets front to
point to the next node in the list. If front becomes NULL, it means the queue is now empty and
both front and rear are reset to NULL.

• Display: Prints all elements of the queue. If the queue is empty, it prints “Queue is empty.”
and returns.

The program uses a while loop to repeatedly prompt the user to choose an operation to perform. The
user’s choice is read from standard input using scanf(). The program then uses a switch statement to
call the appropriate function based on the user’s choice.
QUEUE USING LINKED LISTS

#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));

newNode->data = value;

newNode->next = NULL;

if (front == NULL && rear == NULL) {

front = rear = newNode;

return;

rear->next = newNode;

rear = newNode;

void dequeue() {

if (front == NULL) {

printf("Queue underflow.\n");

return;

struct Node* temp = front;

if (front == rear) {

front = rear = NULL;

} else {

front = front->next;
}

free(temp);

void display() {

if (front == NULL) {

printf("Queue is empty.\n");

return;

printf("Elements of the queue are: ");

struct Node* temp = front;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\nChoose an operation to perform:\n");

printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to insert: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;
case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice.\n");

return 0;

RESULT

MAIN MENU

INSERTION OF ELEMENT 23

INSERTION OF ANOTHER ELEMENT 56

DISPLAY OF ALL ELEMENTS IN THE QUEUE

DEQUEUING THE QUEUE OR DELETING DATA 23 (ENTERING CHOICE AS 2)

DISPLAYING AGAIN AFTER DEQUEUING THE QUEUE

CONCLUSION

A queue has been successfully implemented using arrays and linked lists in c.

You might also like