Queue Data Structure

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Queue Data Structure

A Queue Data Structure is a fundamental concept in computer science used for storing and
managing data in a specific order. It follows the principle of “First in, First out” (FIFO), where
the first element added to the queue is the first one to be removed. Queues are commonly
used in various algorithms and applications for their simplicity and efficiency in managing
data flow.

#include <stdio.h>

# define SIZE 100

void enqueue();

void dequeue();

void show();

int inp_arr[SIZE];

int Rear = - 1;

int Front = - 1;

main()

int ch;

while (1)

printf("1.Enqueue Operation\n");

printf("2.Dequeue Operation\n");

printf("3.Display the Queue\n");


printf("4.Exit\n");

printf("Enter your choice of operations : ");

scanf("%d", &ch);

switch (ch)

case 1:

enqueue();

break;

case 2:

dequeue();

break;

case 3:

show();

break;

case 4:

exit(0);

default:

printf("Incorrect choice \n");

void enqueue()

int insert_item;

if (Rear == SIZE - 1)

printf("Overflow \n");

else

if (Front == - 1)
Front = 0;

printf("Element to be inserted in the Queue\n : ");

scanf("%d", &insert_item);

Rear = Rear + 1;

inp_arr[Rear] = insert_item;

void dequeue()

if (Front == - 1 || Front > Rear)

printf("Underflow \n");

return ;

else

printf("Element deleted from the Queue: %d\n", inp_arr[Front]);

Front = Front + 1;

void show()

if (Front == - 1)

printf("Empty Queue \n");

else

printf("Queue: \n");

for (int i = Front; i <= Rear; i++)


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

printf("\n");

Copy

Output:

1.Enqueue Operation

2.Dequeue Operation

3.Display the Queue

4.Exit

Enter your choice of operations : 1

Element to be inserted in the Queue: 10

1.Enqueue Operation

2.Dequeue Operation

3.Display the Queue

4.Exit

Enter your choice of operations : 1

Element to be inserted in the Queue: 20

1.Enqueue Operation

2.Dequeue Operation

3.Display the Queue

4.Exit

Enter your choice of operations : 3

Queue:

10 20

1.Enqueue Operation

2.Dequeue Operation

3.Display the Queue


4.Exit

Enter your choice of operations : 2

Element deleted from the Queue: 10

1.Enqueue Operation

2.Dequeue Operation

3.Display the Queue

4.Exit

Enter your choice of operations: 3

Queue:

20

Copy

1. #define MAX_SIZE 100

2.

3. int queue[MAX_SIZE];

4. int front = -1;

5. int rear = -1;

6.

7. void enqueue(int element) {

8. if (rear == MAX_SIZE - 1) {

9. printf("Queue is full");

10. return;

11. }

12. if (front == -1) {

13. front = 0;

14. }

15. rear++;

16. queue[rear] = element;

17. }

18.
19. int dequeue() {

20. if (front == -1 || front > rear) {

21. printf("Queue is empty");

22. return -1;

23. }

24. int element = queue[front];

25. front++;

26. return element;

27. }

28.

29. int main() {

30. enqueue(10);

31. enqueue(20);

32. enqueue(30);

33. printf("%d ", dequeue());

34. printf("%d ", dequeue());

35. printf("%d ", dequeue());

36. printf("%d ", dequeue());

37. return 0;

38. }

The output of the code will be:

Output:

10 20 30 Queue is empty-1

Explanation:

1. First, we enqueue three elements 10, 20 and 30 into the queue.

2. Then, we dequeue and print the front element of the queue, which is 10.

3. Next, we dequeue and print the front element of the queue again, which is 20.

4. Then, we dequeue and print the front element of the queue again, which is 30.

5. Finally, we make a dequeue from an empty queue that outputs "Queue is empty" and
returns -1.
Stack Queue

Last In First Out First In First Out

Linear Data structure Linear Data structure

Add and delete elements from Add and delete elements from both sides. Adds elements to rear list
the top of a list Delete an element from the Front of a list

Add an element to Stack called


Add an element to Queue called Enqueue
Push

Removes an element from a


Removes an element is called Dequeue
stack called Pop

Recursion used for Data storage sequential processing for data process

You might also like