You are on page 1of 35

Gambella University

Department Of Computer Science


Data Structure and Algorithms Course
Unit 4 -: Stacks and Queues
Instructor: Takele A. (MSc. In CoSc.)
4.1 Stacks
Definition Of stack
 A stack is a list with the restriction.
 Insertions and deletions can only be performed at the top of the list.
 The other end is called bottom.
 Stacks are less flexible, but are more efficient and easy to implement.
 Stacks are known as LIFO (Last In, First Out) lists.
 The last element inserted will be the first to be retrieved. To understand
the concept of stack, see this diagram.
In stack, the element which is placed (inserted or added) last, is accessed first.
In stack terminology, insertion operation is called PUSH operation and
removal operation is called POP operation.
Stack Representation
The following diagram depicts a stack and its operations .
Basic Operations
Stack operations may involve initializing the stack, using it and then de-
initializing it.
Apart from these basic objects, a stack is used for the following two primary
operations.
 push() − Pushing (storing) an element on the stack.
 pop() − Removing (accessing) an element from the stack.
 When data is pushed onto stack.
o To use a stack efficiently, we need to check the status of stack as well.
o For the same purpose, the following functionality is added to stacks.
 peek() − get the top data element of the stack, without removing it.
 isFull() − check if stack is full.
 isEmpty() − check if stack is empty.

At all times, we maintain a pointer to the last pushed data on the stack. As this
pointer always represents the top of the stack, hence named top.
The top pointer provides top value of the stack without actually removing it.
Linked List Implementation of Stack

 A stack data structure can be implemented by using a linked list data


structure.

 The stack implemented using a linked list can work for a large number of
values.

 That means, stack implemented using the linked list works for the variable
size of data.

 So, there is no need to fix the size, at the beginning of the implementation.
Applications of Stack
Data structure has some important applications in different aspects. These are
Like:-
a) Evaluation of Algebraic Expression
b) Infix and Post fix (RPN)
c) Function calls
a) Evaluation of Algebraic Expression
Stack data structure is used for evaluating the given expression. For example,
consider the following expression
5 * ( 6 + 2 ) - 12 / 4
Since parenthesis has the highest precedence among the arithmetic
operators, ( 6 +2 ) = 8 will be evaluated first. Now, the expression becomes
5 * 8 - 12 / 4
* and / have equal precedence and their associativity is from left-to-right. So,
start evaluating the expression from left-to-right.
5 * 8 = 40 and 12 / 4 = 3
Now, the expression becomes
40 - 3
And the value returned after the subtraction operation is 37.
 This means that after the end of evaluation 37 is stored on the stack.
b) Infix and Post fix (RPN)
 Infix notation: The expression of the form a op b. When an operator is in-
between every pair of operands. Example: 2+3, 4-1, (3 + 4) × 5 – 6, etc.
 Postfix notation: The expression of the form a b op. When an operator is
followed for every pair of operands. Example: 2 3+, 4 2 *, etc.
Infix and Postfix reverse-polish notation (RPN):
Normally, mathematics is written using what we call in-fix notation:

(3 + 4) × 5 – 6
The operator is placed between to operands.
One weakness: parentheses are required
(3 + 4) × 5 – 6 = 29
3+4 × 5–6 = 17
3 + 4 × (5 – 6) = –1
(3 + 4) × (5 – 6) = –7
RPN conti…..

Examples of postfix:
3 4 5 × + 6 –
3 20 + 6 –
23 6 –
17
3 4 5 6 – × +
3 4 –1 × +
3 –4 +
–1
RPN conti…..

Benefits:
– No ambiguity and no brackets are required
– It is the same process used by a computer to perform
computations:
• operands must be loaded into registers before operations can
be performed on them.
– Reverse-Polish can be processed using stacks.
RPN conti…..

The easiest way to parse reverse-Polish notation is to use an


operand stack:
 operands are processed by pushing them onto the stack.
 when processing an operator we need to:
• pop the last two items off the operand stack.
• perform the operation, and
• push the result back onto the stack.
RPN conti…..

Evaluate the following reverse-Polish expression using a stack:


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

Push 1 onto the stack


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

1
RPN conti…..

Push 2 onto the stack


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

Push 3 onto the stack


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
RPN conti…..

Pop 3 and 2 and push 2 + 3 = 5 5


1
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

Push 4 onto the stack


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
4
5
1
RPN conti…..

Push 5 onto the stack 5


1 2 3 + 4 5 6 × – 7 × + – 8 9 × + 4
5
1

Push 6 onto the stack 6


5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
4
5
1
RPN conti…..
30
Pop 6 and 5 and push 5 × 6 = 30
4
1 2 3 + 4 5 6 × – 7 × + – 8 9 × + 5
1

Pop 30 and 4 and push 4 – 30 = –26


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–26
5
1
RPN conti…..
7
–26
Push 7 onto the stack 5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × + 1

Pop 7 and –26 and push –26 × 7 = –182


1 2 3 + 4 5 6 × – 7 × + – 8 9 × + –182
5
1
RPN conti…..

Pop –182 and 5 and push –182 + 5 = –177


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–177
1

Pop –177 and 1 and push 1 – (–177) = 178


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

178
RPN conti…..

Push 8 onto the stack


1 2 3 + 4 5 6 × – 7 × + – 8 9 × + 8
178

Push 9 onto the stack 9


1 2 3 + 4 5 6 × – 7 × + – 8 9 × + 8
178
RPN conti…..

Pop 9 and 8 and push 8 × 9 = 72


72
1 2 3 + 4 5 6 × – 7 × + – 8 9 × + 178

Pop 72 and 178 and push 178 + 72 = 250


1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
250
c) Function calls
In function calls:
– you write a function to solve a problem.
– the function may require sub-problems to be solved, hence, it
may call another function.
– once a function is finished, it returns to the function which
called it.
– You will notice that when a function returns, execution and the return
value is passed back to the last function which was called.
– This is again, the last-in—first-out property
Today’s CPUs have hardware specifically designed to facilitate function
calling.
4.2 Queues
Abstract Queue:
An Abstract Queue (Queue ADT) is an abstract data type that emphasizes
specific operations:
 Uses a explicit linear ordering.
 Insertions and removals are performed individually.
 There are no restrictions on objects inserted into (pushed onto) the queue
—that object is designated the back of the queue.
 The object designated as the front of the queue is the object which was in
the queue the longest.
 The remove operation (popping from the queue) removes the current front
of the queue.
Conti…
Also called a first-in–first-out (FIFO) data structure
– Graphically, we may view these operations as follows:
Conti…
• Alternative terms may be used for the four operations on a
queue, including:

 There are two exceptions associated with this abstract data


structure:
– It is an undefined operation to call either pop or front on an
empty queue.
Applications of Queues
• The most common application is in client-server models:
– Multiple clients may be requesting services from one or more servers.

– Some clients may have to wait while the servers are busy.

– Those clients are placed in a queue and serviced in the order of arrival.
• Bank and airport security use queues
• The SSH (Secure Shell) and SFTP (Secure file transfer protocol) are clients

• Most shared computer services are servers:

– Web, file, ftp, database, mail, printers, etc.


Implementations of Queues

We will look at two implementations of queues:


– Singly linked lists
– Circular arrays

Requirements:
– All queue operations must run in O(1) time
Priority Queue,
 A priority queue is a special type of queue in which each element is
associated with a priority value.
 And, elements are served on the basis of their priority. That is, higher
priority elements are served first.
 However, if elements with the same priority occur, they are served
according to their order in the queue.
Assigning Priority Value
Generally, the value of the element itself is considered for assigning the
priority.

For example,
 The element with the highest value is considered the highest
priority element.
 However, in other cases, we can assume the element with the
lowest value as the highest priority element.
 We can also set priorities according to our needs.
Conti…

Removing Highest Priority Element


Difference between Priority Queue and Normal Queue
• In a queue, the first-in-first-out rule is implemented whereas,
• in a priority queue, the values are removed on the basis of priority.
• The element with the highest priority is removed first.
Implementation of Priority Queue
o Priority queue can be implemented using an array, a linked list, a heap data
structure, or a binary search tree.
o Among these data structures, heap data structure provides an efficient
implementation of priority queues.
A comparative analysis of different implementations of priority queue is given below.

Operations peek insert delete

Linked List O(1) O(n) O(1)

Binary Heap O(1) O(log n) O(log n)

Binary Search Tree O(1) O(log n) O(log n)


Priority Queue Operations
• Basic operations of a priority queue are inserting, removing,

and peeking elements.


1. Inserting an Element into the Priority Queue
o Inserting an element into a priority queue (max-heap)
is done by inserting the new element at the end of the queue.
2. Deleting an Element from the Priority Queue
Deleting an element from a priority queue (max-heap)
is done by selecting the element to be deleted.
3. Peeking from the Priority Queue (Find max/min)
Peek operation returns the maximum element from Max Heap or
minimum element from Min Heap without deleting the node.
4. Extract-Max/Min from the Priority Queue
Extract-Max returns the node with maximum value after removing
it from a Max Heap whereas Extract-Min returns the node with
minimum value after removing it from Min Heap.

End of the unit!

You might also like