Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.

Similar presentations


Presentation on theme: "Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse."— Presentation transcript:

1 Stacks CS-240 Dick Steflik

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

3 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

4 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 isFull( ) 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 isEmpty before popping

5 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

6 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 isFull - return false/true depending if the stack is full; true if it is, false otherwise isEmpty - return false/true depending if the stack is empty; true if it is, false otherwise

7 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 use a struct to define a stack type 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)

8 Static 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];};

9 Define a stack type typedef int item; typedef struct { int top; item data[MAXSIZE]; } stack; void stackInit( stack * id); void push(stack * id, item v); item pop(stack * id) ; int isFull(stack id); int isEmpty(stack id);

10 struct based stack Remember structs are passed by value, so if you pass a struct to a function the function can look at the data but not change it To be able to change it you must pass its address and pass data back through that address (see push on next slide)

11 void push(Stack * id, int v) { int k = id->top; id->data[k] = v ; id->top++; } ; in the main() push would be invoked as: stack a; stackInit(&a); push( &a, 5);

12 with a Macro #ifndef _STACK_H #define _STACK_H #include #define STACK (name) int name[MAXSTACKSIZE]={0} ; name[0]=1; void push(int id[],int v); int pop (int id[]); int isFull(int id[]); int isEmpty(int id[]); #endif // notice the clever way that the 0 th position of the array is used as the top // pointer; this makes everything the stack needs self contained

13 How it’s used #include #include "stack.h" int main() { int i; a = 5; STACK(stack1) ; STACK(stack2) ; push(stack1,5); push(stack1,10); push(stack1,15); push(stack1,20); push(stack1,25); for (i=0 ; i< MAXSTACKSIZE ; i++) printf("V = %d ", stack1[i]); printf("\n"); };

14 Stack Machines A machine where the memory of the computer model takes the form of one or more stacks that are manipulated by the machines instruction set. Could be : –A real physical machine –A virtual machine

15 Real Physical Stack machines Burroughs Large System Computers Rockwell-Collins Atmel MARC4 MCU Saab Erocsson Tandem Computers Pascal and Forth machines/chips

16 Virtual Stack Machines UCSD Pascal p-machine Java Virtual Machine Virtual Execution System (VES) used to execute managed code for Common Intermediate Language for Microsoft.Net (VB, C++ and C#) Forth programming language Adobe PostScript interpreter


Download ppt "Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse."

Similar presentations


Ads by Google