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

19
Linked structures

Post on 20-Dec-2015

235 views

Category:

Documents


0 download

TRANSCRIPT

Linked structures

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

Linked-lists overview

Linked Structures p. 3/19

A B C D E F G

vs.

A CB

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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