{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–10. 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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 = (a+b)+c = ab+c+ a^b^c = a^(b^c) = abc^^ 8
Image of page 8
Evaluating a Postfix Expression Each operator in a postfix string refers to the previous two operands .
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern