linked structures. overview overview of linked structures (for lists) design decision: arrays vs....

of 19 /19
Linked structures

Post on 20-Dec-2015

230 views

Category:

Documents


0 download

Embed Size (px)

TRANSCRIPT

Page 1: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Linked structures

Page 2: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

OverviewOverview of linked structures (for lists)Design decision: arrays vs. linked lists Implementing the StackADT with linked listsUsing StackADT: the maze program Java's built-in java.util.Stack class Time complexity of stack operationsComing attractions

Linked Structures p. 2/19

Page 3: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Linked-lists overview

Linked Structures p. 3/19

A B C D E F G

vs.

A CB

Page 4: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

The codepublic class Person { private String name; // could add address, etc. private Person next; public Person (String n) { this.name = n; next = null; } public String getName() { return name; } //plus get+set methods for ``next'‘}

Linked Structures p. 4/19

Page 5: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

The code (2)public class LinearNode<T> { private T element; private LinearNode<T> next; public LinearNode (T elem) { this.element = elem; next = null; } public T getElement() { return element; } //plus get+set methods for ``next'‘}

Linked Structures p. 5/19

Page 6: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Design decision: arrays vs. linked lists Which structure allows quicker access to its

elements?A. an array B. a linked list C. both D. neither

Linked Structures p. 6/19

Page 7: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Design decision: arrays vs. linked lists (2) Which structure uses space more efficiently if the

size of the list stays the same during runtime?A. an array B. a linked list

Linked Structures p. 7/19

Page 8: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Design decision: arrays vs. linked lists (3) Which structure uses space more efficiently if the

size of the list varies significantly during runtime?A. an array B. a linked list

Linked Structures p. 8/19

Page 9: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Arrays vs. linked lists (summary)Feature Arrays Linked Lists

Faster access to elements

X

More efficient use of space if size of list is constant durin g runtime

X

More efficient use of space if size of list varies significantly

X

Can be used to implement StackADT, etc.

X X

Linked Structures p. 9/19

Page 10: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Using linked lists: StackADTConsider the code for the LinkedStack (p. 80). Draw box and arrow diagrams to show, step by

step, what happens when this code is executed to 1. Create a LinkedStack 2. Add a node containing the String ``Lion'' followed

by a second node containing the String ``Tiger'' 3. Pop the top element off of the LinkedStack

Linked Structures p. 10/19

Page 11: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Using linked lists: StackADT (2)Compare the code for the LinkedStack (p. 80)

with the code for the ArrayStack (p. 58). Line by line, what are the differences? How do the differences in the two data

structures explain the differences in the code?

Linked Structures p. 11/19

Page 12: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Using linked lists: StackADT (3)Compare the code for the LinkedStack (p. 80)

with the code for the ArrayStack (p. 58). Line by line, what are the differences? How do the differences in the two data

structures explain the differences in the code?

Linked Structures p. 12/19

Page 13: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in

java.util.Stack class (http://download.oracle.com/javase/7/docs/api/java/util/Stack.html).

What are the design flaws in this class? Explain.

Hint: can it guarantee that the item you pop will always be the last item pushed?

Linked Structures p. 13/19

Page 14: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Time ComplexityConsider the LinkedStack code on pp. 80-85. The

Big-Oh time complexity of the push operation is: A. O(1) B. O(log2 n)

C. O(n) D. O(n2) E. none of the above

Linked Structures p. 14/19

Page 15: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Time Complexity (2)Consider the LinkedStack code on pp. 80-85. The

Big-Oh time complexity of the pop operation is: A. O(1) B. O(log2 n)

C. O(n) D. O(n2) E. none of the above

Linked Structures p. 15/19

Page 16: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Time Complexity (3)Consider the LinkedStack code on pp. 80-85. The

Big-Oh time complexity of the peek operation is: A. O(1) B. O(log2 n)

C. O(n) D. O(n2) E. none of the above

Linked Structures p. 16/19

Page 17: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Time Complexity (4)Consider the LinkedStack code on pp. 80-85. The

Big-Oh time complexity of the isEmpty operation is: A. O(1) B. O(log2 n)

C. O(n) D. O(n2) E. none of the above

Linked Structures p. 17/19

Page 18: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Arrays vs. linked lists (continued)Operation Big-Oh of Stack

ImplementationBig-Oh of Linked-List Implementation

push

pop

peek

isEmpty

Linked Structures p. 18/19

Page 19: Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists

Coming attractionsNext time we'll look at our second Abstract

Datatype, QueueADT, and consider how to implement it with either an array or a linked list.

Homework: read Chapter 5 Queues (or the equivalent in the earlier edition).

Linked Structures p. 19/19