0% found this document useful (0 votes)
16 views10 pages

DSA Project

Uploaded by

ejr1590
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)
16 views10 pages

DSA Project

Uploaded by

ejr1590
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/ 10

____________________________________________________________

DSA Project:
Queue & Algorithms
Done By Sara S. Ali, Karar Abood,
And Zubaida Manhal
Supervised By Dr. Dunia H. Hameed

Data Structure Course | Fall Semester


2nd Year | BMIC-UoITC
09.12.2024

—-----------------------------------------------------------

1
Introduction to Queues

In computer science, a queue is a collection of


entities that are maintained in a sequence and can be
modified by the addition of entities at one end of the
sequence and the removal of entities from the other end of
the sequence. By convention, the end of the sequence at
which elements are added is called the back, tail, or rear
of the queue, and the end at which elements are removed is
called the head or front of the queue, analogously to the
words used when people line up to wait for goods or
services.

The operations of a queue make it a first-in-first-out


(FIFO) data structure. In a FIFO data structure, the first
element added to the queue will be the first one to be
removed. This is equivalent to the requirement that once a
new element is added, all elements that were added before
have to be removed before the new element can be removed. A
queue is an example of a linear data structure, or more
abstractly a sequential collection. Queues are common in
computer programs, where they are implemented as data
structures coupled with access routines, as an abstract data
structure or in object-oriented languages as classes.

2
Table of Contents
Introduction to Queues
1. Algorithm “Insert”
1. Pseudocode
2. Python Implementation
2. Algorithm “Delete”
1. Pseudocode
2. Python Implementation
3. Algorithm “Display”
1. Pseudocode
2. Python Implementation
4. Algorithm “Counting Even Numbers”
1. Pseudocode
2. Python Implementation
5. Algorithm “Finding Negative Values”
2. Pseudocode
2. Python Implementation

3
1. Algorithm “Insert”
1. Pseudocode
[Given Queue (Q) of size (N), Rear (R) and Front (F) are two
pointers variables. This algorithm is to insert a new value
(X) to the queue]
I. [Check overflow] If (R >= N) print "overflow message",
exit.
II. [Increment R] R ← R+1.
III. [insert a new value] Q[R] ← X.
IV. [Check Front] If (F=0) Set F←1.
V. [Finished] exit.

2. Python Implementation

4
2. Algorithm “Delete”
1. Pseudocode
[Given Queue (Q) of size (N), Rear (R) and Front (F) are two
pointers variables. This
algorithm is to delete an element from the queue and store
it in a variable called (T)]
I. [Check underflow]
If (F<= 0) print "underflow message", return.
II. [Delete element]
T ← Q[F].
III. [Is Queue Empty?]
if (R=F) Set R=F=0.
else F←F+1.
IV. [Finished] return T.

2. Python Implementation

5
3. Algorithm “Display”
1. Pseudocode
I. [Check if the Queue is Empty]
If F >= R, print "Queue is empty", return.
II. [Display the Elements]
For i ← F to R-1 do:
print Q[i]
III. [Finished]

2. Python Implementation

6
4. Algorithm “Counting Even Numbers”
1. Pseudocode

Counting Even Numbers


[Given Queue (Q) of size (N), Rear (R) and Front (F) are two
pointers variables. This algorithm is to empty the queue and
count every even element by incrementing a variable called
E, this algorithm returns an integer that is the last value
of E.]

I. [Initialize E]
E ← 0
II. [check underflow]
If F<=0 print underflow message and exit
III. If Q[F]%2==0 set E ← E+1
F ← F+1
else
F ← F+1
IV. Is Queue Empty?
If R=F
set R=F=0 and return E
else Repeat Step 2
V. [Finished] Return E

7
2. Python Implementation

8
5. Algorithm “Finding Negative Values”
2. Pseudocode

Finding Negative Values


[Given Queue (Q) of size (N), Rear (R) and Front (F) are two
pointers variables. This algorithm is to empty the queue and
push every negative element into a stack called (S), this
algorithm returns an array representing the stack of all
negative values.]

VI. [check underflow]


If F<=0 print underflow message and exit
VII. If Q[F]<0
S.push(Q[F])
F ← F+1
else
F ← F+1
VIII. Is Queue Empty?
If R=F
set R=F=0 and return E
else Repeat Step 2
IX. [Finished] Return S

9
2. Python Implementation

10

You might also like