Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

5 Stacks and Queues

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Stacks and

Queues
in Python
WMSU

Reference: geeksforgeeks
What is a Stacks?
• Stack is a linear data
structure which follows a
particular order in which
the operations are
performed.
• The order may be
LIFO(Last In First Out).
• At a logical level, a stack is ordered group of homogenous
items or elements.
• The removal of existing items and the addition of new items
can take place only at the top of the stack.
STACK OPERATIONS
• Push: Adds an item in the stack. If the stack is full, then it
is said to be an Overflow condition.
• Pop: Removes an item from the stack. The items are
popped in the reversed order in which they are pushed.
If the stack is empty, then it is said to be an Underflow
condition.
• Peek or Top: Returns top element of stack.
• isEmpty: Returns true if stack is empty, else false.
Stack Example # 1
• Reversing a word

A L G O R I T H M

M H T I R O G L A
Stack Sample # 2
• Undo
Created
Adjust
Keypress
Margin”
“Table”
“Y”

Adjust Margin”

Created “Table”
Stack Sample # 3
• Backtracking
What is a Queues?
• A queue is similar to a
stack in that is an
ordered collection of
objects.
• The order is different
though, it is First-In/First-
Out (FIFO).
QUEUE OPERATIONS
• Enqueue - Put an entry to the queue
• Dequeue – Remove an entry from the queue
Queue Sample # 1
Queue Sample # 2
Python Stack and Queues implementation
1. list
2. collections.deque
3. queue.LifoQueue
list
collections.deque
List vs Deque implementation

list - built upon blocks of contiguous memory


(similar with array)
Best used for indexing into the list or looking for an element into the list.
List vs Deque implementation

In List, if the block of contiguous memory is full, then it will need to


get another block, which can take much longer than a normal
.append():
List vs Deque implementation

deque- is built upon a doubly linked list.

Not great for indexing since you have to walk through each node of the list.
..or we can create our own classes for Stack

False
1
2
..or we can create our own classes for Queue

False
3
2
ARITHMETIC EXPRESSION
EVALUATION
An important application of stacks is in parsing. For example, a compiler must
parse arithmetic expressions written using infix notation:

Infix 4 + 5 * 6

Postfix 4 5 6 * +

Prefix + 4 * 5 6
Infix to postfix
1. Read in the tokens one at a time
2. If a token is an integer, write it into the output. If a token is an operator, push it to the stack,
if the stack is empty.
3. If the stack is not empty, you pop entries with higher or equal priority and only then you
push that token to the stack.
4. If a token is a left parentheses '(', push it to the stack
5. If a token is a right parentheses ')', you pop entries until you meet '('.
1st
*/
6. When you finish reading the string, you pop up all tokens which are left there.
2nd
+-
7. Arithmetic precedence is in increasing order: '+', '-', '*', '/';
Hierarchy of priority 1. A + B * C
1st
*/
2nd
+- Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Hierarchy of priority 2. A * B + C
1st
*/
2nd
+- Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Hierarchy of priority 3. A / B * C – D + E
1st
*/
2nd
+- Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Hierarchy of priority 4. ( A + B ) * ( D / E )
1st
*/
2nd
+- Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Hierarchy of priority 5. ( ( A + B ) * ( C + E ) )
1st
*/
2nd
+- Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Hierarchy of priority 6. (AX+(B*C))
1st
*/
2nd
+- Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Hierarchy of priority 7. ( ( A X + ( B * C Y ) ) / ( D / E ) )
1st
*/
Stack:
2nd
+-
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output:
Infix to prefix
1. Reverse the infix expression i.e. A+B*C will become C*B+A. Note while
reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
2. Obtain the “nearly” postfix expression of the modified expression i.e CB*A+.
3. Reverse the postfix expression.
Hierarchy of priority 1. A + B * C R:
1st
*/
Stack:
2nd
+-
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output: R:
Hierarchy of priority 2. ( A + B ) * ( D / E )
1st
*/
R:
2nd
+-
Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output: R:
Hierarchy of priority 3. ( A X + ( B * C ) )
1st
*/
R:
2nd
+-
Stack:
Remember:
• No 2 operator of
the same priority
can stay together
in the stack
column.
• The top of stack
must be should be
of higher priority.
Else, pop.

Output: R:
ANY QUESTIONS??

You might also like