Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.

Similar presentations


Presentation on theme: "CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008."— Presentation transcript:

1 CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008

2 Today Linear Structures –Grow and Shrink in predetermined manner. –Stacks – Last In – First Out –Queues – First In – First Out http://www.cs.williams.edu/JavaStructur es/Documentation.htmlhttp://www.cs.williams.edu/JavaStructur es/Documentation.html

3 Linear Interface

4 AbstractLinear

5 Stacks Like a stack of paper (or cafeteria trays) –“last-in first-out” (LIFO) can only add to the top - push can only remove from the top - pop Why? –Often used to keep track of execution in a program when returning from a method call, the computer has to remember where it last was

6 AbstractStack push() pop() peek() That’s it! –All the rest is complication that can be abstracted away.

7 Stack top Node Only need access to the top of the stack Everything O(1) How about with Vector? May be implemented as a linked list push – addFirst() pop – removeFirst() peek – getFirst()

8 Stacks Interesting idea –Vector O(1) add time, sometimes O(1) sometimes O(n) If that lack of reliability is offputing, then need list implementation

9 Stacks Example 10 public shuffle() { 11 int ind1 = nextInt(NUM_CARDS); } 1 public static void main (String[] args) { 2 deck = new Deck(); 3 deck.shuffle(); 4 System.out.println (deck); } 20 public int nextInt (int num) { 21 return 0; } Call Stack 3 3 11 top

10 Stacks used to simulate recursion CallFrame CallStack to organize the CallFrames CallFrame contains –Local variables –Method parameters –Program counter – declaring location within routine

11 Recursive QuickSort

12 Iterative QuickSort CallFrame

13 Iterative QuickSort

14 Stack Question Suppose you wish to fill a stack with a copy of another, maintaining the order of elements. Using only Stack operations, describe how this would be done. How many additional stacks are necessary? public static void copy(Stack s, Stack t)

15 Stack copy answer

16 Stack Question Suppose you wish to reverse the order of elements of a stack. Using only Stack operations, describe how this would be done. Assuming you place the result in the original stack, how many additional stacks are necessary? public static void reverse(Stack s)

17 Stack: as List or Vector. What’s required of a Stack? –Add to top, remove from top. As a List –Complexity of push, peek, pop? As a Vector –Complexity of push, peek, pop?

18 Post-Fix Notation 6 4 + (10) 3 2 – 4 + (5) 1 2 3 4 5 6 + + + + + (21) Quick: Backus-Naur Form E := L | E E O O := + | - | / | * | ^ | % L := some number

19 Solving Postfix Notation using a Stack The expression to evaluate is either a L or a E. Push all the tokens onto the stack: Take the top: –If L, then we know the value. –If O, Get two more E off the stack. Perform the appropriate operation and return that value.

20 Queues Standing in line –“first-in first-out” (FIFO) add to the “tail” of the list (back of line) - enqueue remove from the “head” (head of line) - dequeue Why? –often used to keep things in the order that they arrived –processes to be scheduled on the CPU –packets arriving to a router

21 Queue – as Linked List – using a Linked List Queue head Node Only have access to the head and tail of the queue May be implemented as a linked list -add to tail -remove from head -SinglyLinked with tail would be fine. tail

22 Queue – as a Vector Add elements to the back –Complexity? Get elements from the front –Complexity? Cost of adding elements?

23 Queue – as Array Keep ints for head and count Use % to keep track of head upon remove

24 Stack vs Queues Stacks depth-first, Queues breadth-first.


Download ppt "CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008."

Similar presentations


Ads by Google