csci 62 data structures dr. joshua stough october 7, 2008
DESCRIPTION
Linear InterfaceTRANSCRIPT
![Page 1: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/1.jpg)
CSCI 62Data Structures
Dr. Joshua StoughOctober 7, 2008
![Page 2: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/2.jpg)
Today• Linear Structures
– Grow and Shrink in predetermined manner.
– Stacks – Last In – First Out – Queues – First In – First Out
• http://www.cs.williams.edu/JavaStructures/Documentation.html
![Page 3: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/3.jpg)
Linear Interface
![Page 4: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/4.jpg)
AbstractLinear
![Page 5: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/5.jpg)
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
![Page 6: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/6.jpg)
AbstractStack• push()• pop()• peek()• That’s it!
– All the rest is complication that can be abstracted away.
![Page 7: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/7.jpg)
Stack
Stack
topNodeNodeNodeNodeNode
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()
![Page 8: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/8.jpg)
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
![Page 9: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/9.jpg)
StacksExample
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 Stack33
11top
![Page 10: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/10.jpg)
Stacks used to simulate recursion• CallFrame• CallStack to organize the CallFrames
• CallFrame contains– Local variables– Method parameters– Program counter – declaring location
within routine
![Page 11: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/11.jpg)
Recursive QuickSort
![Page 12: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/12.jpg)
Iterative QuickSort CallFrame
![Page 13: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/13.jpg)
Iterative QuickSort
![Page 14: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/14.jpg)
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)
![Page 15: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/15.jpg)
Stack copy answer
![Page 16: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/16.jpg)
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)
![Page 17: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/17.jpg)
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?
![Page 18: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/18.jpg)
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
![Page 19: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/19.jpg)
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.
![Page 20: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/20.jpg)
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
![Page 21: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/21.jpg)
Queue – as Linked List – using a Linked
ListQueue
headNodeNodeNodeNodeNode
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
![Page 22: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/22.jpg)
Queue – as a Vector• Add elements to the back
– Complexity?
• Get elements from the front– Complexity?
• Cost of adding elements?
![Page 23: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/23.jpg)
Queue – as Array• Keep ints for head and count• Use % to keep track of head upon
remove
![Page 24: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008](https://reader036.vdocuments.net/reader036/viewer/2022062413/5a4d1b887f8b9ab0599bda86/html5/thumbnails/24.jpg)
Stack vs Queues• Stacks depth-first,
• Queues breadth-first.