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

of 19/19
Linked structures

Post on 20-Dec-2015

217 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Linked structures
  • Slide 2
  • 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
  • Slide 3
  • Linked-lists overview Linked Structures p. 3/19 ABCDEFG vs. ACB
  • Slide 4
  • 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
  • Slide 5
  • The code (2) public class LinearNode { private T element; private LinearNode 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
  • Slide 6
  • 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
  • Slide 7
  • 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
  • Slide 8
  • 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
  • Slide 9
  • Arrays vs. linked lists (summary) FeatureArraysLinked 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. XX Linked Structures p. 9/19
  • Slide 10
  • 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. 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
  • Slide 11
  • 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
  • Slide 12
  • 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
  • Slide 13
  • Java's built-in java.util.Stack class Consider the javadoc for Javas 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
  • Slide 14
  • 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(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 14/19
  • Slide 15
  • 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(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 15/19
  • Slide 16
  • 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(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 16/19
  • Slide 17
  • 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(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 17/19
  • Slide 18
  • Arrays vs. linked lists (continued) OperationBig-Oh of Stack Implementation Big-Oh of Linked-List Implementation push pop peek isEmpty Linked Structures p. 18/19
  • Slide 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