0% found this document useful (0 votes)
8 views9 pages

Rajshahi University of Engineering & Technology: Lab Report

The lab report discusses the implementation of queue data structures in C/C++, covering operations such as enqueue, dequeue, and display. It includes three problems: implementing a queue using an array, reversing the elements of a queue, and sorting the elements of a queue, each with corresponding code and algorithms. The report highlights effective handling of boundary cases and provides a foundation for further exploration of advanced queue implementations.

Uploaded by

marmasunmoon9
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)
8 views9 pages

Rajshahi University of Engineering & Technology: Lab Report

The lab report discusses the implementation of queue data structures in C/C++, covering operations such as enqueue, dequeue, and display. It includes three problems: implementing a queue using an array, reversing the elements of a queue, and sorting the elements of a queue, each with corresponding code and algorithms. The report highlights effective handling of boundary cases and provides a foundation for further exploration of advanced queue implementations.

Uploaded by

marmasunmoon9
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/ 9

Heaven’s Light is Our Guide

Rajshahi University of Engineering &


Technology
Department of Electrical & Computer Engineering
LAB REPORT

❖ Course title(Sessional) : Data Structure and Algorithms


❖ Course Code : ECE-2104
❖ Date of submission : 05/01/2025

Submitted By : Submitted To :

Name : Sanumong Marma Md. Omaer Faruq Goni

Roll : 2210061 Assistant Professor

Reg no. : 1115 Department of ECE,

Series : 22 RUET

Department of ECE, RUET


Lab no. : 03
Theory: 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.

Problem 01 : Write a C/C++ program to implement a queue using an array.Containing


inserting element, displaying queue elements, removing queue elements and checking
whether the queue is empty or not.
Code:
1. #include <stdio.h>
2. #define size 5
3. void enQueue(int);
4. void deQueue();
5. void display();
6. int items[size], front = -1, rear = -1;
7. int main() {
8. deQueue();
9. enQueue(1);
10. enQueue(2);
11. enQueue(3);
12. enQueue(4);
13. enQueue(5);
14. enQueue(6);
15. display();
16. deQueue();
17. display();
18. return 0;
19. }
20. void enQueue(int value) {
21. if (rear == size - 1)
22. printf("\nQueue is Full!!");
23. else {
24. if (front == -1)
25. front = 0;
26. rear++;
27. items[rear] = value;
28. printf("Enqueued element %d\n", value);
29. }
30. }
31. void deQueue() {
32. if (front == -1)
33. printf("Queue is Empty!!\n");
34. else {
35. printf("Deleted : %d\n", items[front]);
36. front++;
37. if (front > rear)
38. front = rear = -1;
39. }
40. }
41. void display() {
42. if (rear == -1)
43. printf("Queue is Empty!!!\n");
44. else {
45. int i;
46. printf("Queue elements are:\n");
47. for (i = front; i <= rear; i++)
48. printf("%d ", items[i]);
49. }
50. printf("\n");
51. }

Output:
Queue is Empty!!

Enqueued element 1

Enqueued element 2

Enqueued element 3

Enqueued element 4

Enqueued element 5

Queue is Full!!Queue elements are:

1 2 3 4 5

Deleted : 1

Queue elements are:

2 3 4 5

Algorithm:

Enqueue:

1. Check if the queue is full:

• Condition: rear == size - 1.

• If true, print Queue is Full!! and exit the function.


2. If the queue is not full:

• If front == -1 (queue is empty), set front = 0.

• Increment rear by 1.

• Add the value to items[rear].

• Print the enqueued element.

Dequeue:

1. Check if the queue is empty:

• Condition: front == -1.

• If true, print Queue is Empty!! and exit the function.

2. If the queue is not empty:

• Print the element at items[front] (the front of the queue).

• Increment front by 1 to point to the next element.

• If front > rear (queue becomes empty after dequeue):

• Reset both front and rear to -1.

Display:

1. Check if the queue is empty:

• Condition: rear == -1.

• If true, print Queue is Empty!!! and exit the function.

2. If the queue is not empty:

• Loop from front to rear and print each element.

Discussion:
The program handles boundary cases effectively and provides a foundation for exploring
advanced queue implementations like circular queues or dynamic queues.

Problem 02 : Write a C/C++ program to reverse the elements of a queue.


Code:
1. #include <stdio.h>
2. #include<stdlib.h>
3. #define MAX_SIZE 100
4. struct Queue
5. {
6. int items[MAX_SIZE];
7. int front;
8. int rear;
9. };
10. void enqueue (struct Queue *q, int item)
11. {
12. if (q->rear == MAX_SIZE - 1)
13. {
14. printf ("Queue overflow\n");
15. return;
16. }
17. if (q->front == -1)
18. {
19. q->front = 0;
20. }
21. q->rear++;
22. q->items[q->rear] = item;
23. }
24. int dequeue (struct Queue *q)
25. {
26. if (q->front == -1)
27. {
28. printf ("Queue underflow\n");
29. return -1;
30. }
31. int item = q->items[q->front];
32. q->front++;
33. if (q->front > q->rear)
34. {
35. q->front = -1;
36. q->rear = -1;
37. }
38. return item;
39. }
40. void display (struct Queue *q)
41. {
42. if (q->rear == -1)
43. {
44. printf ("Queue is empty\n");
45. return;
47. }
48. for (int i = q->front; i <= q->rear; i++)
49. {
50. printf ("%d ", q->items[i]);
51. }
52. printf ("\n");
53. }
55. void reverse (struct Queue *q)
56. {
57. if (q->front == -1)
58. {
59. return;
60. }
61. int item = dequeue (q);
62. reverse (q);
63. enqueue (q, item);
64. }
65. int main ()
66. {
67. struct Queue q;
67. q.front = -1;
68. q.rear = -1;
69. enqueue (&q, 1);
70. enqueue (&q, 2);
71. enqueue (&q, 3);
72. enqueue (&q, 4);
73. printf ("Queue before reversing:\n");
74. display (&q);
75. reverse (&q);
76. printf ("Queue after reversing:\n");
78. display (&q);
79. return 0;
80. }

Output:
Queue before reversing:

1234

Queue after reversing:

4321

Algorithm:
1. Base Case

• Check if the queue is empty:

• Condition: q->front == -1.

• If true, return. (The recursion ends here.)

2. Recursive Steps

1. Dequeue the front element of the queue:

• Use the function dequeue(q) to remove and retrieve the element at the front.

• Store the dequeued element in a variable, item.

2. Recursively call the reverse() function to process the remaining elements of the queue:

• reverse(q).
3. Enqueue the dequeued element back into the queue at the rear:

• Use the function enqueue(q, item) to add the element at the rear of the queue.

Discussion:
The implemented functions worked as intended, handling both normal and edge cases.
While the recursive approach is elegant and easy to implement, iterative methods may be
better suited for larger datasets due to stack limitations. The experiment serves as a strong
foundation for understanding queue operations and recursive algorithms.

Problem 03 : Write a c program to sort the elements of queue.


Code:
1. #include <stdio.h>
2. #define MAX_SIZE 100
3. struct Queue {
4. int items[MAX_SIZE];
5. int front;
6. int rear;
7. };
8. void enqueue(struct Queue *q, int value);
9. int dequeue(struct Queue *q);
10. void display(struct Queue *q);
11. void sortQueue(struct Queue *q);
12. int main() {
13. struct Queue q;
14. q.front = -1;
15. q.rear = -1;
16. enqueue(&q, 4);
17. enqueue(&q, 1);
18. enqueue(&q, 3);
19. enqueue(&q, 2);
20. enqueue(&q, 5);
21. printf("Queue before sorting:\n");
22. display(&q);
23. sortQueue(&q);
24. printf("Queue after sorting:\n");
25. display(&q);
26. return 0;
27. }
28. void enqueue(struct Queue *q, int value) {
29. if (q->rear == MAX_SIZE - 1) {
30. printf("Queue overflow!\n");
31. return;
32. }
33. if (q->front == -1) {
34. q->front = 0;
35. }
36. q->rear++;
37. q->items[q->rear] = value;
38. }
39. int dequeue(struct Queue *q) {
40. if (q->front == -1) {
41. printf("Queue underflow!\n");
42. return -1;
43. }
44. int item = q->items[q->front];
45. q->front++;
46. if (q->front > q->rear) {
47. q->front = -1;
48. q->rear = -1;
49. }
50. return item;
51. }
52. void display(struct Queue *q) {
53. if (q->front == -1) {
54. printf("Queue is empty!\n");
55. return;
56. }
57. for (int i = q->front; i <= q->rear; i++) {
58. printf("%d ", q->items[i]);
59. }
60. printf("\n");
61. }
62. void sortQueue(struct Queue *q) {
63. if (q->front == -1) {
64. printf("Queue is empty! Nothing to sort.\n");
65. return;
66. }
67. int temp[MAX_SIZE];
68. int size = q->rear - q->front + 1;
69. for (int i = 0; i < size; i++) {
70. temp[i] = dequeue(q);
71. }
72. for (int i = 0; i < size - 1; i++) {
73. for (int j = 0; j < size - i - 1; j++) {
74. if (temp[j] > temp[j + 1]) {
75. int tempVal = temp[j];
76. temp[j] = temp[j + 1];
77. temp[j + 1] = tempVal;
78. }
79. }
80. }
81. for (int i = 0; i < size; i++) {
82. enqueue(q, temp[i]);
83. }
84. }
Output:
Queue before sorting:

54321

Queue after sorting:

12345

Algorithm:
Using an Auxiliary Array (Efficient Approach)

1. Step 1: Copy Elements to an Array

• Create a temporary array arr of size n .

• Dequeue all elements from the queue and store them in the array.

2. Step 2: Sort the Array

• Sort the array arr using a standard sorting algorithm (e.g., Quick Sort or Bubble Sort).

3. Step 3: Refill the Queue

• Enqueue the sorted elements back into the queue.

Using a Priority Queue (Min-Heap Approach)

1. Step 1: Transfer Elements to a Min-Heap

• Create an empty priority queue (min-heap).

• Dequeue all elements from the queue and insert them into the priority queue.

2. Step 2: Refill the Queue

• While the priority queue is not empty:

• Extract the smallest element from the priority queue.

• Queue the extracted element back into the queue.

Discussion:
The experiment successfully demonstrated sorting techniques for queues. Both approaches
were effective, with the choice of method depending on the specific use case. This
foundational experiment provides insights into how queues can be manipulated for
enhanced functionality in data structure applications.

You might also like