Академический Документы
Профессиональный Документы
Культура Документы
Mandaluyong
Lesson 3
Stacks
ITC 211: Data Structure and Algorithm Prepared by: Jaevier Angcao Villanueva, MIT
Stacks
• A stack is a list in which insertion and
deletion take place at the same end
– This end is called top
– The other end is called bottom
• Stacks are known as LIFO (Last In, First
Out) lists.
– The last element inserted will be the first
to be retrieved
• e.g. a stack of Plates, books, boxes etc.
Insertion and deletion on stack
Operation On
Stack
• Creating a stack
• Checking stack---- either empty or full
• Insert (PUSH) an element in the stack
• Delete (POP) an element from the stack
• Access the top element
• Display the elements of stack
Push and Pop
• Primary operations: Push and Pop
• Push
– Add an element to the top of the stack.
• Pop
– Remove the element at the top of the stack.
Stack-Related Terms
• Top
– A pointer that points the top element in the stack.
• Stack Underflow
– When there is no element in the stack, the status of stack
is known as stack underflow.
• Stack Overflow
– When the stack contains equal number of elements as per
its capacity and no more elements can be added, the
status of stack is known as stack overflow
Stack Implementation
• Implementation can be done in two ways
– Static implementation
– Dynamic Implementation
• Static Implementation
– Stacks have fixed size, and are implemented as arrays
– It is also inefficient for utilization of memory
• Dynamic Implementation
– Stack grow in size as needed, and implemented as linked lists
– Dynamic Implementation is done through pointers
– The memory is efficiently utilize with Dynamic Implementations
Static Implementation
Elements are stored in contiguous cells of an array.
New elements can be inserted to the top of the list.
Empty
maxlength
Static Implementation
23
1
12
( (A (B C +) *) D /)
/*A+BCD
A*B+C/D
A * (B + C) / D
A * (B + C / D)
Infix Postfix Prefix
A * (B + C) / D ABC+*D/ /*A+BCD
A * (B + C / D) ABCD/+* *A+B/CD
Converting between these notations
The most straightforward method is to start by
inserting all the implicit brackets that show the
order of evaluation e.g.:
( (A * B) + (C / D) ) ( (A B *) (C D /) +) (+ (* A B) (/ C D) )
((A * (B + C) ) / D) ( (A (B C +) *) D /) (/ (* A (+ B C) ) D)
(A * (B + (C / D) ) ) (A (B (C D /) +) *) (* A (+ B (/ C D) ) )
You can use a similar trick to convert to and from parse
trees - each bracketed triplet of an operator and its two
operands (or sub-expressions) corresponds to a node of
the tree. The corresponding parse trees are:
Infix - Postfix
Algorithm : Infix to Postfix Conversion
In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation
is abc*+. The algorithm for the conversion is as follows :
• Scan the Infix string from left to right.
• Initialise an empty stack.
• If the scannned character is an operand, add it to the Postfix string. If the scanned
character is an operator and if the stack is empty Push the character to stack.
• If the scanned character is an Operand and the stack is not empty, compare the
precedence of the character with the element on top of the stack (topStack). If
topStack has higher precedence over the scanned character Pop the stack else
Push the scanned character to stack. Repeat this step as long as stack is not
empty and topStack has precedence over the character.
Example :
Postfix string
Stack
Next character scanned is 'b' which will be placed
in the Postfix string. Next character is '*' which is
an operator. Now, the top element of the stack is
'+' which has lower precedence than '*', so '*'
will be pushed to the stack.
Postfix string
Stack
The next character is 'c' which is placed in the Postfix string. Next
character scanned is '-'. The topmost character in the stack is '*'
which has a higher precedence than '-'. Thus '*' will be popped out
from the stack and added to the Postfix string. Even now the stack
is not empty. Now the topmost element of the stack is '+' which
has equal priority to '-'. So pop the '+' from the stack and add it to
the Postfix string. The '-' will be pushed to the stack.
Postfix string
Stack
Next character is 'd' which is added to Postfix string. Now
all characters have been scanned so we must pop the
remaining elements from the stack and add it to the
Postfix string. At this stage we have only a '-' in the stack.
It is popped out and added to the Postfix string. So, after
all characters are scanned, this is how the stack and
Postfix string will be :
Postfix string
Stack
Infix - Prefix
STEP 1 : Read the given infix expression into string called infix.
STEP 2 : Reverse the infix string and read one character at a time
and perform the following operations :