Stacks First in, last out processing You can only see the next available item on the stack, the newest one in the stack Examples: your inbox at work a pile of dishes in the sink Push Add a new item to the top of the stack What are the possible errors involved? Pop Remove the top item from the stack What are the possible errors involved? Peek Look at the top item on the stack. What are the possible errors involved? As a Linked List: Keep a pointer to the last node of the list Push: Add a new node to the end of the list Pop : move the ptrLast back one and delete the last node As an Array: Keep the index value of the most recently added item Push: increment the current index value Pop : decrement the current index value WriteBackwards string wordFwd = char[10]; string wordBwd = char[10]; char chr = 0; for ( int i=0; i<10; i++ ) { push(wordFwd[i]); } for ( int i=0; i<10; i++ ) { chr = pop(); cout << chr; } Infix Notation Operators are placed between operands - the way we normally see them 2 * 5 + 7 postfix string: 2 5 * 7 + opertator stack: Postfix Notation Operators are placed after the operands 257*+ Infix to Postfix The pseudo-code for converting is: while (there are more items) { read next item into input if ( input is identifier or constant ) append the popped operand to postfix string else { switch ( input ) { case '(': push input onto operator stack case ')': while ( top of operator stack != '(' ) { pop the operator stack append the popped operand to postfix string } pop the operator stack default: while ( input has less or equal priority to top of operator stack ) { pop the operator stack append the popped operand to postfix string } push input onto operator stack } } } Evaluating Postfix Expressions This requires two stacks - operand and operator The pseudo-code is: while (there are more items) { read next item into input if ( input is identifier or constant ) push onto operand stack else if ( there are more than two operands on operand stack ) { pop top two operands apply operator push the result onto operand stack } else push onto operator stack } 12 25 7 * + 25*7+ operand stack : 17 operator stack: