Stack Chapter 4
Outline Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications Recursive Programming Evaluation of Expressions ADT for stacks
Introduction A stack is an ordered list with the restriction that elements are added or deleted from only one end of the list termed top of stack. The other end of the list which lies inactive is termed bottom of stack. The stack data structure obeys the principle of Last In First Out (LIFO). In other words, elements inserted or added into the stack join last and those that joined last are the first to be removed.
Stack operation The two operations which stack data structure supports are Insertion or addition of elements known as Push. Deletion or removal of elements known as Pop.
Stack Implementation A common and a basic method of implementing stacks is to make use of another fundamental data structure, i.e arrays. Arrays are sequential data structures, but we can also employ linked data structures and have been successfully attempted and applied. An array based implementation of stacks is fairly convenient considering the fact that stacks are unidimensional ordered lists and so are arrays which despite their multi-dimensional structure are inherently associated with a one-dimensional consecutive set of memory locations.
Implementing PUSH Let STACK[1:n] be an array implementation of a stack. Top is variable recording the current top of stack position and initialized to 0. Item is the element to be pushed into the stack and n is the maximum capacity of the stack. Algorithm 4.1 Implementation of push operation on a stack procedure PUSH(STACK, n, top, item) if (top = n) then STACK_FULL; else { top = top + 1; STACK[top] = item; /* store item as top element of STACK */ } end PUSH
Implementing POP Empty_STACK : Top=0 Full_STACK : Top = n Algorithm 4.2 : Implementation of pop operation on a stack procedure POP(STACK, top, item) if (top = 0) then STACK_EMPTY; else { item = STACK[top]; top = top - 1; } end POP
Stack ADT Data objects: A finite set of elements of the same type a linear array Operations: Create an empty stack and initialize top of stack CREATE(STACK) Check if stack is empty CHK_STACK_EMPTY(STACK) (Boolean function) Check if stack is full CHK_STACK_FULL(STACK) (Boolean function) Push ITEM into stack STACK PUSH(STACK, ITEM) Pop element from stack STACK and output the element popped in ITEM POP(STACK, ITEM)
Application of Stack Recursive programming During the execution of a recursive program, to keep track of the calls made to itself and to record the status of the parameters at the time of the call, a stack data structure is used Example: factorial function factorial (5)
Stack implementation on recursive prog.
Tail recursion or Tail end recursion is a special case of recursion where a recursive call to the function turns out to be the last action in the calling function. Note that the recursive call needs to be the last executed statement in the function and not necessarily the last statement in the function. In a stack implementation of a recursive call, all the local variables of the function that are to be remembered, are pushed into the stack when the call is made.
Conti... Upon termination of the recursive call, the local variables are popped out and restored to their previous values. Now for tail recursion, since the recursive call turns out to be the last executed statement, there is no need that the local variables must be pushed into a stack for them to be remembered and restored on termination of the recursive call. factorial1(n, accumulator) { if (n == 0) return accumulator; return factorial1(n - 1, n * accumulator); } factorial(n) { return factorial1(n, 1); }
Evaluation of expression Infix expression <Operator> <Operand> <Operator> Postfix expression <Operator> <Operator> <Operand> a+b*c-d abc*+d- Prefix expression <Operand> <Operator> <Operator> -+a*bcd These are important feature of compiler to evaluate and understand the an arithmetic expression. In reality compiler rework the infix expression to produce the follows the Postfix scheme or Prefix scheme above.
Example Postfix Infix : a+b*c-d
Example prefix Infix : (a*b-f*h)^d
Evaluation of Postfix
Q&A