Unit-2 (Stack and Queue)
Unit-2 (Stack and Queue)
• The abstract data type is special kind of data type, whose behavior is defined
by a set of values and set of operations. The keyword “Abstract” is used as we
can use these data types, we can perform different operations. But how those
operations are working that is totally hidden from the user. The ADT is made of
with primitive data types, but operation logics are hidden.
• A real-world stack allows operations at one end only. For example, we can
place or remove a card or plate from the top of the stack only. Likewise, Stack
ADT allows all data operations at one end only. At any given time, we can only
access the top element of a stack.
• This feature makes it LIFO data structure. LIFO stands for Last-in-first-out.
Here, 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
•A stack can be implemented by means of Array, Structure, Pointer, and Linked
List. Stack can either be a fixed size one or it may have a sense of dynamic
resizing. Here, we are going to implement stack using arrays, which makes it a
fixed size stack implementation.
peek() − get the top data element of the stack, without removing it.
The time complexities for push() and pop() functions are O(1) because we
always have to insert or remove the data from the top of the stack, which is a
one step process.
Application’s Of Stack
• Recursion
• Indirect Recursion
Eg:-
int a()
{
b();
}
int b()
{
a();
}
Tower Of Hanoi
• Tower of Hanoi basically comes from a Chinese or Japanese source
• It’s a simple game concept which can be applied in stacks. The concept is
that the bigger value should not be kept upon the smaller value
Movement
of Discs
1. AC
2. AB
3. CB
4. AC
5. BA
6. BC
7. AC
Expression Parsing
• The way to write arithmetic expression is known as a notation. An arithmetic
expression can be written in three different but equivalent notations, i.e.,
without changing the essence or output of an expression.
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
• Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are
used in-between operands. It is easy for us humans to read, write, and speak
in infix notation but the same does not go well with computing devices. An
algorithm to process infix notation could be difficult and costly in terms of
time and space consumption.
• Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written
ahead of operands. For example, +ab. This is equivalent to its infix notation a
+ b. Prefix notation is also known as Polish Notation.
Postfix Notation and Algorithm
• This notation style is known as Reversed Polish Notation. In this
notation style, the operator is postfixed to the operands i.e., the
operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
Infix to Postfix Conversion Algorithm
We use a stack
1 - scan the expression from left to right
2 - When an operand is read, output it
3 - When an operator is read – Pop until the top of the stack has an element
of lower precedence – Then push it
4 - When ) is found, pop until we find the matching (
5 - ( has the lowest precedence when in the stack
6 - but has the highest precedence when in the input
7 - When we reach the end of input, pop until the stack is empty
Infix to Postfix Conversion Example
Example 1 --3+4*5/6
1. 2. 3.
3+4*5/6 3+4*5/6 3+4*5/6
• Stack: • Stack:
• Output: • Output: 3
4. 5. 6.
3+4*5/6 3+4*5/6 3+4*5/6
• Stack: + • Stack: + • Stack: + *
• Output: 3 • Output: 3 4 • Output: 3 4
Infix to Postfix Conversion Example
7. 8. 9.
3+4*5/6 3+4*5/6 3+4*5/6
• Stack: + * • Stack: + • Stack: + /
• Output: 3 4 5 • Output: 3 4 5 * • Output: 3 4 5 *
10. 11. 12.
3+4*5/6 3+4*5/6 3+4*5/6
• Stack: + / • Stack: + • Stack:
• Output: 3 4 5 * 6 • Output: 3 4 5 * 6 • Output: 3 4 5 * 6
/ /+
Done!
Infix to Postfix Conversion Example 1
Example 1 : ((A-(B+C))*D)^(E+F)
Infix to Prefix Conversion Algorithm
1. Reverse the infix expression. And follow the steps of infix to post fix
conversion algorithm .
2. After getting postfix expression , reverse it and this is the prefix expression
of the infix expression.
Infix to Prefix Conversion Example
(A + B) * (C + D) *+AB+CD AB+CD+*
A postfix expression can be evaluated using the Stack data structure. To evaluate a
postfix expression using Stack data structure we can use the following steps...
1. Read all the symbols one by one from left to right in the given Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop
operations and store the two popped oparands in two different variables
(operand1 and operand2). Then perform reading symbol operation using
operand1 and operand2 and push result back on to the Stack.
4. Finally! perform a pop operation and display the popped value as final result.
Evaluation of expression Example
Links for conversion of Expressions
1. Infix to postfix
https://www.youtube.com/watch?v=vq-nUF0G4fI
ADT Queue
Types of Queues:
Simple Queue
Circular Queue
Priority Queue
Operations on each types of queues:
Algorithms and their analysis
ADT(Abstract Data Type)
• Insert A
Example
Non of Simple
Primitive Queue
Data Structure
• Insert B
Example
Non of Simple
Primitive Queue
Data Structure
• Insert C
Example
Non of Simple
Primitive Queue
Data Structure
• Insert D
Example
Non of Simple
Primitive Queue
Data Structure
• Delete A
Example
Non of Simple
Primitive Queue
Data Structure
• Insert E
Example
Non of Simple
Primitive Queue
Data Structure
• Insert F
Example
Non of Simple
Primitive Queue
Data Structure
• Delete B
Example
Non of Simple
Primitive Queue
Data Structure
• Insert G
Input Restricted:
When we use only one end for inserting an element and both the
ends for deleting an element.
Classification of Dequeue
Output Restricted
When we use only one end for deleting an element and both ends
for insertion of an element.
Methods to implement Dequeue
Performance:
Performance:
– insert takes O(1) time since – insert takes O(n) time
we can insert the item at since we have to find the
the beginning or end of the place where to insert the
sequence item
– remove take O(n) time since – remove and take O(1)
we have to traverse the time, since the smallest
entire sequence to find the key is at the beginning
smallest key
www.paruluniversity.ac.in