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

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

Jan 18, 2018

Download

Documents

Damian Copeland

Linear Interface
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.

CSCI 62Data Structures

Dr. Joshua StoughOctober 7, 2008

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

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.

Linear Interface

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

AbstractLinear

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

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.

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.

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.

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.

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.

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.

Recursive QuickSort

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

Iterative QuickSort CallFrame

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

Iterative QuickSort

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

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.

Stack copy answer

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

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.

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.

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.

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.

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.

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.

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.

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.

Stack vs Queues• Stacks depth-first,

• Queues breadth-first.