Stacks CS240 Dick Steflik Stacks Last In First

  • Slides: 8
Download presentation
Stacks CS-240 Dick Steflik

Stacks CS-240 Dick Steflik

Stacks • Last In, First Out operation - LIFO • As items are added

Stacks • Last In, First Out operation - LIFO • As items are added they are chronologically ordered, items are removed in reverse chronological order (newest first)

Applications • • • reversing the items in a list returning from a function/subroutine

Applications • • • reversing the items in a list returning from a function/subroutine passing arguments to a function evaluation of postfix expressions converting infix expressions to postfix localization of parameters and variables in a function

Overflow and Underflow • Stack overflow is the condition that arises when you attempt

Overflow and Underflow • Stack overflow is the condition that arises when you attempt adding (pushing) an item onto an already full stack. – To avoid always check is. Full( ) before adding • Stack Underflow is the condition that arises when you attempt to remove (pop) an item from an already empty stack – To avoid always check is. Empty before popping

ADT methods • constructor - create and initialize a stack object • copy constructor

ADT methods • constructor - create and initialize a stack object • copy constructor - create a stack object that is a duplicate of another existing stack object (this method needed to insure correct value semantics) • assignment operator - assign the value of an existing stack object (a) to another existing stack object (b) so that the result is that b is a duplicate of a (this method needed to insure correct value semantics) • destructor - destroy a stack object by returning any of its dynamic storage back to the heap and setting its static elements to NULL or zero

ADT Methods • push - add an item (as the most recent) • pop

ADT Methods • push - add an item (as the most recent) • pop - delete the most recently added item • pull - return the value of the most recently added item then delete the item • peek - return the value of the most recently added item • is. Full - return false/true depending if the stack is full; true if it is, false otherwise • is. Empty - return false/true depending if the stack is empty; true if it is, false otherwise

Private data strategies • use an array to hold items and use an int

Private data strategies • use an array to hold items and use an int as an index for the array to indicate where the next item is to go • same as above, but use a dynamic array • use a struct to define a node and add nodes dynamically as they are needed; use one static pointer to a node at point at most recently added item(top of stack)

Static Implementation int stack[MAXSIZE]; int top; void empty(void) { top = 0; }; void

Static Implementation int stack[MAXSIZE]; int top; void empty(void) { top = 0; }; void push(int v) { if (top >= MAXSIZE) stackfull(); else stack[top++] = v; }; int pop(void) { if (top < 0 ) stackempty(); else return (stack[--top]; };