Stacks Chapter 4 Outline Introduction Stack Operations Stack

  • Slides: 13
Download presentation
Stacks (Chapter 4)

Stacks (Chapter 4)

Outline • Introduction • Stack Operations • Stack Implementation • Implementation of Push and

Outline • Introduction • Stack Operations • Stack Implementation • Implementation of Push and Pop operations • Applications Recursive Programming Evaluation of Expressions • ADT for stacks

3 A stack is an ordered list with the restriction that elements are added

3 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. Top Bottom A B C 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 Operations B C D B A C (i) Insertion or addition of elements

Stack Operations B C D B A C (i) Insertion or addition of elements known as Push. D The two operations which stack data structure supports are A (ii) Deletion or removal of elements known as Pop. A B C

Stack Implementation • A common and a basic method of implementing stacks is to

Stack Implementation • A common and a basic method of implementing stacks is to make use of another fundamental data structure eg. , arrays. While arrays are sequential data structures the other alternative of employing linked data structures 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.

Algorithm 4. 1 Implementation of push operation on a stack procedure PUSH(STACK, n, top,

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

Algorithm 4. 2 Implementation of pop operation on a stack procedure POP(STACK, top, item)

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

8 Applications Recursive Programming During the execution of a recursive program, to keep track

8 Applications 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 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.

9 In a stack implementation of a recursive call, all the local variables of

9 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. 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.

10 Evaluation of Expressions Infix expression <Operand> <Operator> <Operand> Postfix expression <Operand> < Operand

10 Evaluation of Expressions Infix expression <Operand> <Operator> <Operand> Postfix expression <Operand> < Operand > <Operator> Prefix expression <Operator> <Operand> < Operator >

11 Evaluation of Post-fix expressions Algorithm 4. 3 : Procedure to evaluate a post

11 Evaluation of Post-fix expressions Algorithm 4. 3 : Procedure to evaluate a post fix expression E. Procedure EVAL_POSTFIX(E) X = get_next_character (E); /* get the next character of expression E */ case x of : x is an operand: Push x into stack S; : x is an operator: : x = “$”: end case end EVAL-POSTFIX. Pop out required number of operands from the stack S, evaluate the operator and push the result into the stack S; Pop out the result from stack S;

12 Example 1 Consider a stack DEVICE[1: 3] of peripheral devices. The insertion of

12 Example 1 Consider a stack DEVICE[1: 3] of peripheral devices. The insertion of the four items PEN, PLOTTER, JOY STICK and PRINTER into DEVICES and deletion. Example 2 Consider infix expression a + b*c – d Compute the equivalent postfix expression Example 3 Consider infix expression (a * b – f * h) ↑ d Compute the equivalent prefix expression

ADT for Stacks Data objects: A finite set of elements of the same type

ADT for Stacks Data objects: A finite set of elements of the same type 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)