You are on page 1of 8

Republic of the Philippines

PARTIDO STATE UNIVERSITY


Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

College of Arts and Sciences

Module 7
QUEUE

Name of Student : _______________________________ Week number(s) : 13-14


Course Code : CC4 Name of Faculty : Shane C. Briones
Course Title : Data Structures and Algorithms

INTRODUCTION
This module will introduce you to queue which is a list-like structure that provides restricted access
to its elements just like the stack. Queue is a very predominant model for data flow in real life just like for
example a queue of consumers for a resource where the consumer that came first is served first.

I. OBJECTIVES

1. Identify applications of queue.


2. Apply the concept of queue (its characteristics and functions) in data insertion and deletion
through array-based /linked-list.

II. LESSONS

A Queue is a linear structure which follows a


particular order in which the operations are
performed. The difference between stack and queue is
in its insertion/adding and deletion/removing. Stack
adds and removes at one end only while queue adds in
one side and removes on the other side that makes it a
First-In-First-Out (FIFO) structure. Queue is a container
Image Source: Malte Mueller/Getty Images at of elements where elements may only be inserted at
https://www.thoughtco.com/queuing-theory-4171870 the rear and removed from the front.

Queue is an abstract data structure and very useful in programming. It follows FIFO rule - the
item that goes in first is the item that comes out first too.

If one of the most useful applications of stack is the evaluation of an arithmetic expression, queue
have also numerous significant contributions in the field of computing. Consider an office with a
network printer, if there are 20 computers connected in a single printer and say 5 of them will send
print commands, their printing tasks will be queued and the first computer to send command will be

1
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

the first to execute or print while the rest will have to wait for their turns. Another application of
queue is the Operating System; it uses a number of different queues to control processes within a
computer. The scheduling of what gets done next is typically based on a queuing algorithm that tries
to execute programs as quickly as possible and serve as many users as it can. Also, as we type,
sometimes keystrokes get ahead of the characters that appear on the screen. This is due to the
computer doing other work at that moment. The keystrokes are being placed in a queue-like buffer
so that they can eventually be displayed on the screen in the proper order.

Basic operations/functions that you can use in implementing queue in a program:


• enqueue() – add an element to the rear of the queue
• dequeue() – remove an element from the front of the queue
• empty() – check if the queue is empty
• front() – get the value of the front of the queue without removing it
• size() – returns the size of the queue or the number of elements in the queue
Note: these are user-defined function names which are used in Table 1.

Enqueue Operation
1. Check if the queue is full.
2. If the queue is full, produce overflow error and exit.
3. If the queue is not full, increment rear pointer to point the next empty space.
4. Add data element to the queue location where the rear is pointing.
5. Return success.

Dequeue Operation
1. Check if the queue is empty.
2. If it is empty, produce underflow error and exit.
3. If it is not empty, access the data where front is pointing.
4. Increment front pointer to point to the next available data element.
5. Return success.

Table 1. Sample of Series of Queue Operations


Operation Output Content of Queue (front to rear)
enqueue(29) - (29)
enqueue(11) - (29,11)
enqueue(20) - (29,11,20)
front( ) 29 (29,11,20)
size( ) 3 (29,11,20)
dequeue( ) - (11,20)
enqueue(17) - (11,20,17)
front( ) 11 (11,20,17)
dequeue( ) - (20,17)
dequeue( ) - (17)
dequeue( ) “error” ( )
empty( ) true ( )

2
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

Types of Queue:
There are many types of queues but I will only cite the two basic types: Simple and Circular
Queues.

Simple Queue
Insertion takes place at the rear and removal occurs at the front. It strictly follows FIFO
rule. Take example the following operations. We will use the legend below to represent the
front and rear of a queue.

▪ enqueue(2) Legend:

front

2 rear

▪ enqueue(4)

2 4

▪ enqueue(6)

2 4 6

▪ dequeue()

4 6

▪ dequeue()

Circular Queue
The last element points to the first element making a circular link. The advantage of this
over a simple queue is better memory utilization. If the last position is full and the first
position is empty then, an element can be inserted in the first position. This action is not
possible in a simple queue.

3
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

Take for example the list of elements below:

2 4 6

Legend:
▪ enqueue(8)
front

2 4 6 8 rear

▪ enqueue(10)

2 4 6 8 10
▪ dequeue()

4 6 8 10
▪ dequeue()

6 8 10
▪ enqueue(3)

3 6 8 10
▪ enqueue(9)

3 9 6 8 10

Summary:
Queue is an abstract data structure that follows a FIFO rule. It is similar with stack in a way that
it has adding and deleting operations; in stack adding is called push while deleting is pop; in queue
adding is called enqueue and deleting is called dequeue. You can only enqueue at the rear and
dequeue at the front. Simple and Circular queues are its basic types.

III. ACTIVITY

Study and understand how queue operations are implemented in a program by looking at the
sample Queue C++ program from www.programiz.com/dsa/queue which is shown on the
following pages.

4
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

1. #include <iostream>
2. #define SIZE 5
3.
4. using namespace std;
5.
6. class Queue {
7. private:
8. int items[SIZE], front, rear;
9.
10. public:
11. Queue() {
12. front = -1;
13. rear = -1;
14. }
15.
16. bool isFull()
17. {
18. if (front == 0 && rear == SIZE - 1)
19. {
20. return true;
21. }
22. return false;
23. }
24.
25. bool isEmpty()
26. {
27. if (front == -1)
28. return true;
29. else
30. return false;
31. }
32.
33. void enQueue(int element)
34. {
35. if (isFull())
36. {
37. cout << "\nQueue is full\n";
38. } else {
39. if (front == -1)
40. front = 0;
41. rear++;
42. items[rear] = element;
43. cout << endl << "Inserted " << element << endl;
44. }
45. }
46.
47. int deQueue()
48. {
49. int element;
50. if (isEmpty())
51. {
52. cout << "Queue is empty" << endl;
53. return (-1);
54. }

5
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

55. else {
56. element = items[front];
57. if (front >= rear)
58. {
59. front = -1;
60. rear = -1;
61. }else {
62. front++;
63. }
64. cout << endl << "Deleted -> " << element << endl;
65. return (element);
66. }
67. }
68.
69. void display()
70. {
71. /* Function to display elements of Queue */
72. int i;
73. if (isEmpty())
74. {
75. cout << endl << "Empty Queue" << endl;
76. } else {
77. cout << endl << "Front index-> " << front;
78. cout << endl << "Items -> ";
79. for (i = front; i <= rear; i++)
80. cout << items[i] << " ";
81. cout << endl << "Rear index-> " << rear << endl;
82. }
83. }
84. };
85.
86. int main()
87. {
88. Queue q;
89.
90. //deQueue is not possible on empty queue
91. q.deQueue();
92.
93. //enQueue 5 elements
94. q.enQueue(90);
95. q.enQueue(56);
96. q.enQueue(73);
97. q.enQueue(44);
98. q.enQueue(82);
99. q.enQueue(101);
100.
101. q.display();
102.
103. q.deQueue();
104.
105. q.display();
106.
107. return 0;
108. }
6
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

Sample Run:

IV. ASSESSMENT
Perform and illustrate the process for each of the following operations using Simple Queue:
1. Enqueue(99)
2. Enqueue(88)
3. Enqueue(77)
4. Enqueue(66)
5. Dequeue()
6. Dequeue()

REFERENCES
Tutorials Point India Limited. (04 November 2019). https://www.tutorialspoint.com
Njoroge, H. (2017). Data Structure and Algorithms. African Virtual University CC-BY, SA.
Nievergelt, J. & Hinrichs, K. (2011). Algorithms and Data Structures with Applications to Graphics and
Geometry, CC-BY.
Shaffer,C. (2012). Data Structures and Algorithm Analysis. http://people.cs.vt.edu/~shaffer/Book
Barnett, G. & Del Tongo, L. (2008). Data Structures and Algorithm: Annotated Reference with Examples.
DotNetSlackers. http://dotnetslackers.com
Software Testing Help. (2020, September 13). Introduction to Sorting Technique in C++. Software Testing
Help. https://www.softwaretestinghelp.com/sorting-techniques-in-cpp/

7
Republic of the Philippines
PARTIDO STATE UNIVERSITY
Camarines Sur
PSY-SYL-___-__TRAINING AGREEMENT AND LIABILITY WAIVER

Halim, S. (2020). Visualgo.net. https://visualgo.net


Goodrich, M., Tamassia, R. & Mount, D. (2011). Data Structures and Algorithm in C++. John Wiley & Sons, Inc.

Prepared by: Approved by:

SHANE C. BRIONES JONI NEIL B. CAPUCAO, DIT


FACULTY/CHAIRMAN, REVIEW COMMITTEE DEAN

You might also like