CS315 Data Structures

Linked structures
Overview
 Overview of linked structures (for lists)
 Design decision: arrays vs. linked lists
 Implementing the StackADT with linked lists
 Using StackADT: the maze program
 Java's built-in java.util.Stack class
 Time complexity of stack operations
 Coming attractions
Linked Structures p. 2/19
Linked-lists overview
A
B
C
D
E
F
G
vs.
A
B
C
Linked Structures p. 3/19
The code
public 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
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
Can be used to
implement StackADT,
etc.
Linked Lists
X
X
X
Linked Structures p. 9/19
Using linked lists: StackADT
Consider 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.
2.
3.
Create a LinkedStack
Add a node containing the String ``Lion'' followed by
a second node containing the String ``Tiger''
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/u
til/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 Complexity
Consider 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
Implementation
Big-Oh of Linked-List
Implementation
push
pop
peek
isEmpty
Linked Structures p. 18/19
Coming attractions
 Next 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