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

ece 242 data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

ECE242 L3: Introduction to Stacks September 14, 2009

ECE 242 Data Structures

Lecture 3

Introduction to Stacks

ECE242 L3: Introduction to Stacks September 14, 2009

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
slide-2
SLIDE 2

ECE242 L3: Introduction to Stacks September 14, 2009

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

ECE242 L3: Introduction to Stacks September 14, 2009

Stack Basics In other words, we “stack” up undo operations.

replace “hello” with “goodbye” add character “!” add word “world” add word “hello”

document edits, with most recent first

slide-3
SLIDE 3

ECE242 L3: Introduction to Stacks September 14, 2009

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

100 200 300 400

Operation lists

100 200 300

undo

100 200

undo

100

undo undo

ECE242 L3: Introduction to Stacks September 14, 2009

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 C B A

Push Pop

slide-4
SLIDE 4

ECE242 L3: Introduction to Stacks September 14, 2009

Last-in First-out (LIFO)

A A B A B C

When we push entries

  • nto the stack and then

pop them out one by one, we will get the entries in reverse order. The last one pushed in is the first one popped

  • ut! (LIFO)

A A B

Push A, B, C Pop C, B, A

ECE242 L3: Introduction to Stacks September 14, 2009

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

slide-5
SLIDE 5

ECE242 L3: Introduction to Stacks September 14, 2009

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.

ECE242 L3: Introduction to Stacks September 14, 2009

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() }

slide-6
SLIDE 6

ECE242 L3: Introduction to Stacks September 14, 2009

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() { ... } }

ECE242 L3: Introduction to Stacks September 14, 2009

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

slide-7
SLIDE 7

ECE242 L3: Introduction to Stacks September 14, 2009

Array Implementation of Stack

°Use an array to implement stack

… C B A n … 4 3 2 1 Max_Size

Operations:

  • push(item)
  • pop( )
  • peek( )
  • isEmpty( )
  • isFull( )

Other implementations are possible

ECE242 L3: Introduction to Stacks September 14, 2009

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

}

slide-8
SLIDE 8

ECE242 L3: Introduction to Stacks September 14, 2009

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; }

ECE242 L3: Introduction to Stacks September 14, 2009

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; }

slide-9
SLIDE 9

ECE242 L3: Introduction to Stacks September 14, 2009

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; }

ECE242 L3: Introduction to Stacks September 14, 2009

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; }

slide-10
SLIDE 10

ECE242 L3: Introduction to Stacks September 14, 2009

ArrayStackPT Peek() Method public Object peek() { if (isEmpty())

throw new IllegalStateException ("Stack is empty");

return stack[top]; }

ECE242 L3: Introduction to Stacks September 14, 2009

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