Stack

You might also like

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

Data Structures and Algorithms

Linear Data Structure Stack


1. Introduction of Stack 2. Operations on Stack a. Push operation b. Pop operation c. isEmpty operation d. Peek operation (also referred as stackTop) 3. Representing Stack in C 4. Implementation of Operations 5. Definitions of Infix, Prefix and Postfix 6. Algorithm for Evaluating Postfix expression 7. Algorithm to convert an Infix expression to Postfix expression

Introduction of Stack As an abstract data type, the stack is a container (data structure) of nodes (items) and has two basic operations: push and pop. Push adds a given node to the top of the stack leaving previous nodes below. Pop removes and returns the current top node of the stack. A frequently used metaphor is the idea of a stack of plates in a spring loaded cafeteria stack. In such a stack, only the top plate is visible and accessible to the user, all other plates remain hidden. As new plates are added (pushed), each new plate becomes the top of the stack, and hides each plate below. As plates are removed (popped) from the stack, they can be used, and second plate becomes the top of the stack. Two important principles are illustrated by this metaphor, the Last-In First-Out principle is one. The second is that the contents of the stack are hidden. Only the top plate is visible, so to see what is on the third plate, the first and second plates will have to be removed. Definition [Stack] A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack. Operations on Stack In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation peek (or stacktop) can return the current top node of the stack without removing it from the stack. We can also define a function that checks whether the stack is empty or not. Representing Stacks in C The following C code represents the Stack. #define SIZE 10 typedef struct { int ptr; int items[SIZE]; }Stack; void initStack(Stack &s){ s->ptr = -1; }
Page 1 of 4 Prepared by: Manoj Shakya

Data Structures and Algorithms

Implementation of Operations Implementing various operations including pop and push.


int isEmpty(STACK *s){ if(s->ptr == -1) return 1; else return 0; } void push(STACK *s, int value){ if(s->ptr == 49){ printf("Data overflow."); exit(1); } else{ (s->ptr)++; s->items[s->ptr] = value; } } int pop(STACK *s){ if(isEmpty(s)==1){ printf("Data Underflow"); exit(1); } else{ int v = s->items[s->ptr]; (s->ptr)--; return v; } } int stackTop(STACK *s) { if(isEmpty(s)==1){ printf("Underflow"); exit(1); } else{ int v = s->items[s->ptr]; return v; } } void printStack(STACK *s){ int i; for(i=0;i<=s->ptr;i++) { printf("\n%d",s->items[i]); } }

Infix, Prefix, and Postfix Expressions Consider the sum of A and B. We think of applying the operator + to the operands A and B and write the sum as A + B. This particular representation is called infix. There are two alternative notations for expressing the sum of A and B using the symbols A, B, and +. These are

Page 2 of 4 Prepared by: Manoj Shakya

Data Structures and Algorithms

+AB prefix AB+ postfix The prefixes pre-, post- and in- refer to the relative position of the operator with respect to the two operands. In prefix notation the operator precedes the two operands, in postfix notation the operator follows the two operands, and in infix notation the operator is between the two operands. Examples Infix a + b a+b*c Prefix +ab +a*bc Postfix ab+ abc*+

You should note the advantage of prefix and postfix: the need for precedence rules and parentheses are eliminated. Algorithm for Evaluating Postfix Expression The following algorithm evaluates the postfix expression. Algorithm [ Evaluating a Postfix Expression ] stack = new empty stack; /* scan the input string reading one element at a time into symbol */ while(not end of string){ symbol = getNextCharacter(); If(symbol is an operand){ push(stack, symbol) }else{ // symbol is an operator operand2 = pop(stack); operand1 = pop(stack); value = calculate(operand1, symbol, operand2); push(stack, value); } } return (pop(stack)); Demonstration: Let us now consider an example. Suppose that we are asked to evaluate the following postfix expression 6 2 + 5 9 * +. symbol 6 2 + 5 9 * + operand2 operand1 value 6 6, 8 8, 8, 8, 53 stack 2 5 5, 9 45

9 45

5 8

45 53

Finally we need to pop the data from the stack and that is the answer (53).

Page 3 of 4 Prepared by: Manoj Shakya

Data Structures and Algorithms

Algorithm for Converting Infix into Postfix Expression The following algorithm converts the infix expression into postfix expression. Algorithm [Converting Infix to Postfix Expression ] stack = new empty stack; while(not end of string){ symbol = getNextCharacter(); if(symbol is an operand){ concatenate(postfix, symbol); } else { while(!isempty(stack) && precedence(peek(stack),symbol)){ top_symbol = pop(stack); concatenate(postfix, top_symbol); } Push(stack, symbol); } } while(!isempty(stack)){ top_symbol = pop(stack); concatenate(postfix, top_symbol); } Demonstration: Suppose we want to convert 2*3/(2-1)+5*(4-1) into Prefix expression.

symbol 2 * 3 / ( 2 1 ) + 5 * ( 4 1 )

stack * * / /( /( /(/(/ + + +* +*( +*( +*(+*(+*

Postfix 2 2 23 23* 23* 23*2 23*2 23*21 23*2123*21-/ 23*21-/5 23*21-/5 23*21-/5 23*21-/54 23*21-/54 23*21-/541 23*21-/54123*21-/541-*+

So, the Postfix Expression is 23*21-/541-*+.

Page 4 of 4 Prepared by: Manoj Shakya

You might also like