ece 242 data structures
play

ECE 242 Data Structures Lecture 3 Introduction to Stacks - PDF document

ECE 242 Data Structures Lecture 3 Introduction to Stacks September 14, 2009 ECE242 L3: Introduction to Stacks Overview Techniques for storing large amounts of data Stacks lots of examples in everyday life Easy to implement in


  1. ECE 242 Data Structures Lecture 3 Introduction to Stacks September 14, 2009 ECE242 L3: Introduction to Stacks Overview ° Techniques for storing large amounts of data • Stacks – lots of examples in everyday life • Easy to implement in Java • Lots of fun applications ° Approaches for implementing stacks • Array based implementation ° Use of interfaces in defining stack use • Independent of stack implementation • Defines all the important methods September 14, 2009 ECE242 L3: Introduction to Stacks

  2. Undo operations ° A common feature of word/image processors is the “undo” feature. ° How are our edits stored? • A structure is holding the information • The structure is accessed in a specific pattern ° Why is it that we can recall our last edit so quickly? • The information is stored somewhere in an ordered fashion • We need to be able to access it September 14, 2009 ECE242 L3: Introduction to Stacks Stack Basics In other words, we “stack” up undo operations. document edits, replace “hello” with most recent first with “goodbye” add character “!” add word “world” add word “hello” September 14, 2009 ECE242 L3: Introduction to Stacks

  3. Example ° In an Excel file, input your data in row • 100, 200, 300, 400 ° Find something wrong, use undo, return to previous state ° Information placed in stack, then taken out Operation lists undo undo undo undo 400 300 300 200 200 200 100 100 100 100 September 14, 2009 ECE242 L3: Introduction to Stacks What Is Stack ° Stack is an abstract data type ° Adding an entry on the top (push) ° Deleting an entry from the top (pop) ° Simple, but useful in computer engineering ° Note that you can’t add items to the middle of the stack Push Pop C B A September 14, 2009 ECE242 L3: Introduction to Stacks

  4. Last-in First-out (LIFO) Push A, B, C The last one C pushed in is the B B first one popped A A A out! (LIFO) When we push entries onto the stack and then B pop them out one by one, A A we will get the entries in reverse order. Pop C, B, A September 14, 2009 ECE242 L3: Introduction to Stacks Applications of Stack ° Reverse a string ° Check nesting structure ° Undo operation in many editor tools. ° Brackets balancing in compiler ° Stack frame of procedure call ° Hardware stack in CPU September 14, 2009 ECE242 L3: Introduction to Stacks

  5. Abstract Data Types ° Stack • Operating on top only • Operations: Push(in), Pop(out) ° Queue • Operating on both ends • Operations: EnQueue(in), DeQueue(out) ° List • More general than Stack and Queue, it is a sequence of items. You can insert an item and delete an item anywhere. September 14, 2009 ECE242 L3: Introduction to Stacks Stack Implementation We can abstract away the edit operations, all we care about is storing edits in LIFO order. interface Stack { void push(Object x) Object pop() Object peek() boolean isEmpty() int size() } September 14, 2009 ECE242 L3: Introduction to Stacks

  6. An Array-based Stack class ArrayStack implements Stack { private Object[] data = null; private int top = 0; ArrayStack(int size) { data = new Object[size]; } public void push(Object x) { ... } public Object pop() { ... } public Object peek() { ... } public boolean isEmpty() { ... } public boolean isFull() { ... } public int size() { ... } } September 14, 2009 ECE242 L3: Introduction to Stacks Abstract Data Type ° We use Object data type instead of int or double or String or other data type ° Use an array in stack, which stores all items popped in. • Object Stack[ ]; ° Must be able to store Strings, Wrapper classes, or any other type of Object September 14, 2009 ECE242 L3: Introduction to Stacks

  7. Array Implementation of Stack ° Use an array to implement stack Operations: Max_Size n … … • push(item) 4 3 2 C • pop( ) 1 B 0 A • peek( ) • isEmpty( ) Other implementations are possible • isFull( ) September 14, 2009 ECE242 L3: Introduction to Stacks Implementation for Stack public class ArrayStackPT { private final static int DEFAULT_CAPACITY = 100; // suppose the default capacity for this stack is 100. private Object stack[ ]; // The array that holds the items private int top; // Index of top item on the stack } September 14, 2009 ECE242 L3: Introduction to Stacks

  8. ArrayStackPT Constructor // Creates a stack with the default capacity public ArrayStackPT () { this(DEFAULT_CAPACITY); } // Creates a stack with a user-specified capacity public ArrayStackPT (int capacity) { if (capacity < 1) throw new IllegalArgumentException ("Capacity must be > 0" ); stack = new Object[capacity]; top = -1; } September 14, 2009 ECE242 L3: Introduction to Stacks ArrayStackPT---Empty or Full public int size() { return top + 1; // return the number of elements } // check whether the stack is empty public boolean isEmpty() { return top == -1; } // check whether the stack is full public boolean isFull() { return size() == stack.length; } September 14, 2009 ECE242 L3: Introduction to Stacks

  9. ArrayStackPT Push() Method public void push(Object item) { if (item == null) throw new IllegalArgumentException ("Item is null"); if (isFull()) throw new IllegalStateException ("Stack is full"); top = top + 1; stack[top] = item; } September 14, 2009 ECE242 L3: Introduction to Stacks ArrayStackPT Pop() Method public Object pop( ) { if (isEmpty()) throw new IllegalStateException ("Stack is empty"); Object topItem = stack[top]; stack[top] = null; top = top - 1; return topItem; } September 14, 2009 ECE242 L3: Introduction to Stacks

  10. ArrayStackPT Peek() Method public Object peek() { if (isEmpty()) throw new IllegalStateException ("Stack is empty"); return stack[top]; } September 14, 2009 ECE242 L3: Introduction to Stacks Summary ° Stacks are an important data structure • Many computers from the 1960s were based solely on stacks ° Stacks can be used for many applications ° Interfaces and implementations play a large role in efficient use of stacks ° Reading: L+C 3.1-3.2, 3.4-3.5, 3.7-3.8 September 14, 2009 ECE242 L3: Introduction to Stacks

Recommend


More recommend