170 likes | 552 Views
Stack. Content. Stack Stack operation Push Pop Stack Representation Array Linked list Applications. Stack.
E N D
Stack Department of CE
Content • Stack • Stack operation • Push • Pop • Stack Representation • Array • Linked list • Applications Department of CE
Stack • A stack is an ordered collection of elements in which insertions and deletions are restricted to one end. The end from which elements are added and/or removed is referred to as top of the stack. Stacks are also referred as “piles” and “push-down lists. The first element placed in the stack will be at the bottom of the stack. The last element placed in the stack will be at the top of the stack. The last element added to stack is the first element to be removed. Hence stacks are referred to as Last-In –First-Out (LIOF) lists. Department of CE
Stack Operation • Push: Places an object on the top of the stack. • Pop: Removes an object from the top of the stack and produces that object. • IsEmpty: Reports whether the stack is empty or not. Department of CE
Stack Operation Department of CE
PUSH push(stack, top, n, item) { if(top=n) then // test whether the stack is full or not { print “stack full”; exit; } else { top=top+1; // top value is incremented by one stack[top]=item; // new item added onto the stack } } Department of CE
POP POP (STACK, top, ITEM) { if( top=0) then { print “stack empty”; exit; } else { ITEM=STACK[top] top=top-1; } } Department of CE
Array representation of stack • Stacks are represented in the computer memory using array structure or linked list structure. The figure shows the representation using an array namely STACK[N] • N is the size of the stack and top is a pointer variable which holds the index value of the topmost element in the stack. • If the value of top is zero or NULL, the stack is said to be empty. If the value of top is N, then the stack is said to be full. Department of CE
Figure N N-1 N-2 N-3 . . . . 6 5 TOP - 4 93 3 58 2 7 1 22 Department of CE
Linked list representation of stack • This type of representation has more advantages than representing stacks using arrays. They are • It is not necessary to specify the number of elements to be stored in a stack during its declaration. • Insertions and deletions can be handled easily and efficiently. • Linked list representation of stacks can grow and shrink in size without wasting the memory space, depending upon the insertion and deletion that occurs in the list. • Multiple stacks can be represented efficiently using a chain for each stack. Department of CE
Application of Stack - Evaluating Postfix Expression (5+9)*2+6*5 • An ordinary arithmetical expression like the above is called infix-expression -- binary operators appear in between their operands. • The order of operations evaluation is determined by the precedence rules and parenthesis. • When an evaluation order is desired that is different from that provided by the precedence, parentheses are used to override precedence rules. Department of CE
Application of Stack - Evaluating Postfix Expression • Expressions can also be represented using postfix notation - where an operator comes after its two operands. • The advantage of postfix notation is that the order of operation evaluation is unique without the need for precedence rules or parenthesis.
Application of Stack - Evaluating Postfix Expression The following algorithm uses a stack to evaluate a postfix expressions. Start with an empty stack for (each item in the expression) { if (the item is a number) Push the number onto the stack else if (the item is an operator){ Pop two operands from the stack Apply the operator to the operands Push the result onto the stack } } Pop the only one number from the stack – that’s the result of the evaluation
Application of Stack - Evaluating Postfix Expression • Example: Consider the postfix expression, 2 10 + 9 6 - /, which is (2 + 10) / (9 - 6) in infix, the result of which is 12 / 3 = 4. • The following is a trace of the postfix evaluation algorithm for the above.
END Department of CE