4. Introduction
4
Data structures have different kinds of
storage structures such us Arrays,
Stacks, queues, and priority queues .
Consider Arrays – as a data storage
structure:
1. very useful.
2. easy to insert into, delete from, and search for specific
items.
5. Stack
5
A stack is used to store data such that the last item inserted is the first
item removed. It is used to implement a last-in first-out (LIFO) type
protocol. The stack is a linear data structure in which new items are
added, or existing items are removed from the same end, commonly
referred to as the top of the stack.
Stacks are very common in computer science and are used in many types
of problems. Stacks also occur in our everyday lives. Consider a stack of
trays in a lunchroom. When a tray is removed from the top, the others
shift up. If trays are placed onto the stack, the others are pushed down.
6. Stack
6
A stack is used to store data such that the last item inserted is
the first item removed. It is used to implement a last-in first-out
(LIFO) type protocol. The stack is a linear data structure in which
new items are added, or existing items are removed from the
same end, commonly referred to as the top of the stack.
8. Stack
8
A stack is a data structure that stores a linear collection of items
with access limited to a last-in first-out order. Adding and
removing items is restricted to one end known as the top of the
stack. An empty stack is one containing no items.
• Stack(): Creates a new empty stack.
• isEmpty(): Returns a boolean value indicating if the stack is
empty.
• length (): Returns the number of items in the stack.
9. Stack
9
• pop(): Removes and returns the top item of the stack, if the
stack is not empty. Items cannot be popped from an empty
stack. The next item on the stack becomes the new top item.
• peek(): Returns a reference to the item on top of a non-empty
stack without removing it. Peeking, which cannot be done on
an empty stack, does not modify the stack contents.
• push( item ): Adds the given item to the top of the stack.
10. Push operation
10
The process of putting a new data element onto stack is known
as a Push Operation. Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next
empty space.
Step 4 − Adds data element to the stack location, where top is
pointing.
Step 5 − Returns success.
11. Pop Operation
11
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and
exit.
Step 3 − If the stack is not empty, accesses the data
element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
12. Implementing the Stack in Python
12
Using a Python List
Using a Linked Lis
Using Collections.deque
Using queue.LifoQueue
13. Application of stack
13
Infix notation: the operator comes between the two
operands need to use parentheses to control the
evaluation of the operators.
Postfix notation: the operator comes after its two operands :
no need to use parentheses
Prefix notation: the operator comes before its two
operands : no need to use parentheses
14. Example: a + b
14
Infix notation : a + b
Prefix notation : + a b
Postfix notation: a b +
15. Algorithm Transforming Infix Expression into Postfix Expression
15
Rules for the conversion from infix to postfix expression
Print the operand as they arrive.
If the stack is empty or contains a left parenthesis on top, push the incoming operator on to the
stack.
If the incoming symbol is '(', push it on to the stack.
If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis is found.
If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
If the incoming symbol has lower precedence than the top of the stack, pop and print the top of the
stack. Then test the incoming operator against the new top of the stack.
If the incoming operator has the same precedence with the top of the stack then use the
associativity rules. If the associativity is from left to right then pop and print the top of the stack then
push the incoming operator. If the associativity is from right to left then push the incoming operator.
At the end of the expression, pop and print all the operators of the stack.
16. Example
16
Convert the following infix expression topostfix
using stack:
1. A + (B /C)
2. (( A – ( B + C)) * D) / (E+F)
17. Evaluation of postfix expression using stack.
17
Scan the expression from left to right.
If we encounter any operand in the expression, then we push the operand
in the stack.
When we encounter any operator in the expression, then we pop the
corresponding operands from the stack.
When we finish with the scanning of the expression, the final value
remains in the stack.
18. Algorithm Transforming Infix Expression into Prefix Expression
18
Rules for the conversion of infix to prefix expression:
First, reverse the infix expression given in the problem.
Scan the expression from left to right.
Whenever the operands arrive, print them.
If the operator arrives and the stack is found to be empty, then simply push the operator into the stack.
If the incoming operator has higher precedence than the TOP of the stack, push the incoming operator into
the stack.
If the incoming operator has the same precedence with a TOP of the stack, push the incoming operator into
the stack.
If the incoming operator has lower precedence than the TOP of the stack, pop, and print the top of the stack.
Test the incoming operator against the top of the stack again and pop the operator from the stack till it finds
the operator of a lower precedence or same precedence.
•
19. Algorithm Transforming Infix Expression into Prefix Expression
19
If the incoming operator has the same precedence with the top of the stack and the
incoming operator is ^, then pop the top of the stack till the condition is true. If the
condition is not true, push the ^ operator.
When we reach the end of the expression, pop, and print all the operators from the
top of the stack.
If the operator is ')', then push it into the stack.
If the operator is '(', then pop all the operators from the stack till it finds ) opening
bracket in the stack.
If the top of the stack is ')', push the operator on the stack.
At the end, reverse the output.
20. Example
20
Convert the following infix expression toPrefix
using stack:
1. A + (B /C)
2. (( A – ( B + C)) * D) / (E +F)
21. Evaluation of Prefix Expression using Stack
21
Step 1: Initialize a pointer 'S' pointing to the end of the expression.
Step 2: If the symbol pointed by 'S' is an operand then push it into the stack.
Step 3: If the symbol pointed by 'S' is an operator then pop two operands from the
stack. Perform the operation on these two operands and stores the result into the
stack.
Step 4: Decrement the pointer 'S' by 1 and move to step 2 as long as the symbols left
in the expression.
Step 5: The final result is stored at the top of the stack and return it.
Step 6: End
22. Converting Exercise
22
1 ) Convert these INFIX to PREFIX and POSTFIX :
A / B – C / D
(A + B) ^ 3 – C * D A ^ (B + C)
2)Convert these PREFIX to INFIX and POSTFIX :
+ – / A B C ^ DE
– + D E / X Y
^ + 2 3 – C D
3)Convert these POSTFIX to INFIX and PREFIX :
A B C + –
G H + I J / * A B ^ C D + –
23. Queue
23
The term queue is commonly defined to be a line of people waiting to be
served like those you would encounter at many business establishments.
Each person is served based on their position within the queue. Thus, the
next person to be served is the first in line. As more people arrive, they
enter the queue at the back and wait their turn.
A queue structure is well suited for problems in computer science that
require data to be processed in the order in which it was received. Some
common examples include computer simulations,
CPU process scheduling, and shared printer management.
24. Queue
24
A queue is a specialized list with a limited number of operations in which items
can only be added to one end and removed from the other. A queue is also
known as a first-in, first-out (FIFO) list.
Consider the below pictures, which illustrates an abstract view of a queue. New
items are inserted into a queue at the back while existing items are removed
from the front. Even though the illustration shows the individual items, they
cannot be accessed directly.
25. Queue
25
A queue is a data structure that a linear collection of items in which access is
restricted to a first-in first-out basis. New items are inserted at the back and
existing items are removed from the front. The items are maintained in the
order in which they are added to the structure.
Queue(): Creates a new empty queue, which is a queue containing no
items.
isEmpty(): Returns a boolean value indicating whether the queue is empty.
length (): Returns the number of items currently in the queue.
enqueue( item ): Adds the given item to the back of the queue.
dequeue(): Removes and returns the front item from the queue. An item
cannot be dequeued from an empty queue.
26. Queue
26
A queue is like a line of people waiting for a bank teller. The
queue has a front and a rear.
$ $
Front
Rear
27. Queue
27
New people must enter the queue at the rear. it is usually
called an enqueue operation.
$
$
Front
Rear
28. Queue
28
When an item is taken from the queue, it always comes from the front. it
is usually called a dequeue operation
$ $
Front
Rear
29. Algorithm to insert element (enqueue Operation
29
The following steps should be taken to enqueue (insert) data into a queue
−
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
Step 4 − Add data element to the queue location, where the rear is
pointing.
Step 5 − return success..
30. Algorithm to delete element (dequeue Operation)
30
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is
pointing.
Step 4 − Increment front pointer to point to the next available data
element.
Step 5 − Return success.
31. 31
0 1 2 3 4
Front=-1
Rear=-1
A
0 1 2 3 4
Front=0
Rear=0
1
2
Enqueue A
A B
0 1 2 3 4
Front=0
Rear=1
3
Enqueue B
A B C
0 1 2 3 4
Front=0
Rear=2
4
Enqueue C
B C
0 1 2 3 4
Front=1
Rear=2
5
dequeue
C
0 1 2 3 4
Front=2
Rear=2
6
dequeue
0 1 2 3 4
Front=3
Rear=2
7
dequeue
Queue is empty
Queue is empty
Entry point is called Rear &
Exit point is called Front
32. Disadvantages of linear queue
32
On deletion of an element from existing queue, front pointer is
shifted to next position.
This results into virtual deletion of an element. By doing so
memory space which was occupied by deleted element is wasted
and hence inefficient memory utilization is occur.
33. Overcome disadvantage of linear queue
33
To overcome disadvantage of linear queue, circular queue is
use.
We can solve this problem by joining the front and rear end of a
queue to make the queue as a circular queue .
Circular queue is a linear data structure. It follows FIFO
principle.
In circular queue the last node is connected back to the first node
to make a circle.
36. Implementation
36
There are various ways to implement a queue in Python.
This article covers the implementation of queue using data
structures and modules from Python library.
Queue in Python can be implemented by the following
ways:
list
collections.deque
queue.Queue
37. Priority Queue
37
A priority queue is a more specialized data structure
than a stack or a queue.:
insert in rear,
remove from front.
But items in the priority queue are ordered by a key
value so that ‘the item with the lowest key (in some
implementations the highest key) is always at the front.
38. Priority Queue
38
Priority Queues are abstract data structures
where each data/value in the queue has a certain
priority. For example, In airlines, baggage with
the title “Business” or “First-class” arrives earlier
than the rest.
39. Priority Queue
39
Idea behind the Priority Queue is simple:
• Is a queue But the items are ordered by a key.
• So, once in the queue, your ‘position’ in the queue
may be changed by the arrival of a new item.
Various applications of Priority queue in Computer Science
are: Job Scheduling algorithms, CPU and Disk Scheduling,
managing resources that are shared among different
processes, etc.
40. Key differences between Priority Queue and Queue:
40
In Queue, the oldest element is dequeued first. While, in Priority
Queue, element based on highest priority is dequeued.
When elements are popped out of a priority queue then result
obtained in either sorted in Increasing order or in Decreasing
Order. While, when elements are popped from a simple queue, a
FIFO order of data is obtained in the result.