stacks - STACK A stack is a collection of items into which...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
STACK A stack is a collection of items into which new items are inserted and from which items are deleted at one end (called the top of the stack). Different implementations are possible; although the concept of a stack is unique. Example : Trays in the cafeteria. Two primary operations: 1. push : adds a new item on top of a stack. 2. pop : removes the item on the top of a stack Stack is also known as push-down list 1 top
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
LIFO (Last In First Out): order of addition and deletion of items from a stack. A stack is a dynamic structure. It changes as elements are added to and removed from it. 2 Insert: A B C D E F Delete: F E D F E D C B A top C B A top
Background image of page 2
Implementation of a stack as an array #define maxstack 100 struct stack{ int items[maxstack]; int top; }; int isEmpty(struct stack s){ return (s.top < 0); } int isFull(struct stack s){ return (s.top >= maxstack-1); } To put a new item on stack: void push (struct stack *s, int x){ if (s->top >= maxstack-1) printf(“The stack is full.\n”); else { s->top = s->top +1; s->items[s->top] = x; } 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} To pop an item from the stack: int pop (struct stack *s){ int x; if (s->top < 0) printf(“Stack is empty.\n”); else{ x = s->items[s->top]; s->top = s->top –1; return x; } } To indicate which item is at top of stack (without disturbing the stack) int returntop (struct stack *s){ int x; if (s->top < 0) printf(“Stack is empty.\n”); else{ x = s->items[s->top]; return x; }} 4
Background image of page 4
Main program int main() { struct stack S; int c, i; S.top = -1; while ((c=getchar() )!='\n') push(&S, c); while (!isEmpty(S)) printf("%c", pop(&S)); printf("\n"); } 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Use of stack Evaluation of arithmetic expressions Notation can be infix, postfix or prefix. Infix: operator is between operands A + B Postfix : operator follows operands AB+ Prefix : operator precedes operands +AB Operators in a postfix expression are in correct evaluation order. Postfix Expressions a + b * c Infix form (precedence of * is higher than of +) a + (b * c) a + ( b c * ) convert the multiplication 6
Background image of page 6
a (b c * ) + convert the addition a b c * + Postfix form Example 2: ( A + B ) * C Infix form ( A B + ) * C Convert the addition (A B + ) C * Convert multiplication A B + C * Postfix form No need of parenthesis anywhere a + (( b * c ) / d ) a + ( ( b c * ) /d ) (precedence of * and / are same and they are left associative) a + ( b c * d / ) a b c * d / + 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
More examples Infix Postfix (a + b) * (c – d) a b + c d - * a – b / (c + d * e) a b c d e * + / - ((a + b) * c – (d – e))/(f + g) a b + c * d e - - f g + / Order of precedence for 5 binary operators: power (^) multiplication (*) and division (/) addition (+) and subtraction (-) The association is assumed to be left to right except in the case of power where the association is assumed from right to left. i.e. a + b + c
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 26

stacks - STACK A stack is a collection of items into which...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online