Lecture Week 5 - 1: Stack
Lecture Week 5 - 1: Stack
RECALL: ADT’S
Primitive data types = int, string, char… (support basic functions like add, subtract
…)
User defined data types= user will define data types + user define operations as well.
To simplify the process of solving problems, we combine the data structures with
their operations and we call this “Abstract Datatype.
1. Declaration of data
2. Declaration of operators
When defining ADT’s dont worry about implementation details. They come into the picture only when
we want to use them. Different kinds of ADT’s are suited to different kind of applications.
STACK
A stack is a simple data structure used for storing data.
In a stack , the order in which the data arrives is important.
STACK
A stack is a linear list structure in which insertion and deletion is done at one
end called “TOP”
The last element inserted will be the first one to be deleted.
It is called as LIFO (Last in First Out ) list.
The depth of stack is the number of elements it contains.
When element is inserted in a stack, the concept is called “Push”
When element is removed from a stack , the concept is called “Pop”
Trying to pop out an empty stack is called “underflow” condition
Trying to push an element in full stack is called as “overflow” condition
STACK
STACK OPERATIONS (MAIN)
Push (int data) => insert data into the stack where top is indication. If stack is
full then overflow exception occured
int Pop () => Removes and returns the last inserted element from the stack, if
stack is empty then underflow exception occured
AUXILIARY STACK
OPERATIONS
int Top() => returns the last inserted element without removing it . If stack is
empty error occurred.
int size() => returns the no. of elements stored in stack
bool IsEmpty( ) => indicates whether stack is empty or not. Returned true if
stack is empty else return false.
Bool IsFull( )=> indicates whether stack is full or not. Return true if stack is
full else return false.
STACK
IMPLEMENTATION
USING ARRAYS
IMPLEMENTING STACK USING
ARRAY
STACK
1 2 3 4 5 6 7 8
1
MAXSTK 8
TOP
PUSH()
This procedure pushes an ITEM onto a stack
PUSH(STACK,TOP,MAXSTK,ITEM)
1. IF TOP = MAXSTK, then
Print : Overflow, and Return
2. Set TOP = TOP + 1 [Increase TOP by 1]
3. Set STACK[TOP]= ITEM [Insert ITEM in new TOP
position]
4. Return
POP()
This procedure deletes the top element of stack and assigns it to the variable ITEM
POP(STACK,TOP,ITEM)
1. IF TOP = 0, then
Print : Underflow, and Return
2. Set ITEM = STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP= TOP - 1 [Decreases TOP by 1]
4. Return
LIMITATIONS
– The maximum size of the stack must be defined priori and
cannot be changed
a + b * c --> a + ( b * c )
Here, multiplication operation has precedence over addition, b * c will be evaluated
first.
ASSOCIATIVITY
For example, in expression a + b − c, both + and – have the same
precedence
A. 3 + 5 * 6 – 7 * (8 + 5)=
B. A/B-C+D*E-A*C =
C. 5+ 3 + 4 + 1=
D. (5 + 3) * 10=
E. (b * b – 4 * a * c) / (2 * a) =
POSTFIX NOTATION
[End of if structure]
[End of step 2 loop]
5. Set VALUE equal to the top element on STACK
6. Exit
EXAMPLE
Symbol STACK
P: 5,6,2,+,*,12,4,/,- Scanned
5 5
6 5,6
2 5,6,2
+ 5,8
* 40
12 40,12
4 40,12,4
/ 40,3
- 37
)
POSTFIX EXPRESSION
EVALUATION
P: 5,6,2,+,*,12,4,/,-