List ADT NodeBased Implementation - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

List ADT NodeBased Implementation

Description:

Elements of array are ... Drawback! Not great for position-based list ... A major drawback to using an array to implement the List ADT is the fact that ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 25
Provided by: bal1
Category:

less

Transcript and Presenter's Notes

Title: List ADT NodeBased Implementation


1
List ADTNode-Based Implementation
  • IC312
  • A Linear Data Type

2
Node-Based Changes
  • Elements of array are juxtaposed, by definition,
    in memory
  • Allows us to use indices to identify elements
  • We dont know placement
  • of nodes in memory

0 1 2 3
3
Node-Based Changes
  • Metal Picture of Memory (array-based)
  • Mental Picture of Memory (node-based)

Tail
Head
Tail
Head
4
Node-Based List
  • We need to make sense of the mess

Tail
Head
5
Nodes More than just our data
  • Hopefully you see importance of knowing location
    of next element in list
  • Each node has to keep that information
  • List knows where it starts and ends
  • Each node knows next step in between

6
Node
  • class NodeltGenericgt
  • //public methods
  • private Generic data
  • private NodeltGenericgt next

7
Singly-Linked List
  • Name comes from links between nodes
  • Each node has a single link
  • Each node points to next node in list
  • Last node points to

8
Drawback!
  • Not great for position-based list
  • We know where the next node is, but what about
    node x ?
  • Other problems?

9
One Way
  • With singly-linked lists can only move one way
  • To get to second to last, must start at beginning

10
Adding to Middle of List
  • We also have issues adding to middle of list

?
BAD!
?
Ø
?
?
GOOD!
Ø
11
Losing Track of Elements
  • We cant lose track of elements
  • We only know where an element is because node
    before keeps track of it
  • Must copy location before erasing it
  • Similar with deleting

12
Deleting
  • Make previous node skip one in list
  • Now nothing points to deleted node
  • Java garbage collection will get it

13
Benefits?
  • There are drawbacks to node-based list
  • What are some benefits?
  • List has no maximum size
  • Inserting and removing elements is done in
    constant time (unless position is important)
  • Dont have to shift elements
  • Still, we have that one-way problem

14
How to Solve One-Way Problem?
  • Use array-based list
  • Have links that go both ways

15
Doubly-Linked List
  • Links go both ways
  • Node needs next and previous
  • Allows you to travel forward and backward through
    list

Ø
Ø
Head
Tail
16
Doubly Linked Node
  • class NodeltGenericgt
  • //public methods
  • private Generic data
  • private NodeltGenericgt next
  • private NodeltGenericgt previous

17
Adding to Middle of List
Ø
Ø
18
Removing
  • Make your previous next be your next
  • Make your nexts previous be your previous

19
Interface
public interface ListltGenericgt int size()
boolean isEmpty() Generic get(int
index) Generic set(int index, Generic
entry) boolean add(Generic entry)
boolean add(int index, Generic entry)
Generic remove(int index)
20
Implementations
  • int size()
  • boolean isEmpty()

21
Implementations
  • Generic get(int index)
  • Generic set(int index, Generic entry)

22
Implementations
  • boolean add(Generic entry)
  • boolean add(int index, Generic entry)

23
Implementation
  • Generic remove(int index)

24
Homework
  • Due Monday, 10 September
  • Read section 2.5 (Casting and Generics)
  • Read sections 3.2 and 3.3 (Linked Lists)
  •  
  • Dynamic Array Expansion.
  • A major drawback to using an array to implement
    the List ADT is the fact that the size of the
    array is fixed, thereby imposing an arbitrary
    upper limit on the number of elements one can
    store in the list.  One way to overcome this
    limitation is with dynamic array expansion.  The
    idea is this when the array becomes full, create
    a new array that is larger than the original
    array, copy the contents of the original array
    into the new array, and reset the reference
    variable that was point to the original array so
    that it now points to the new array.  In
    practice, each new array it typically twice the
    size of the old array.
  •  
  • On the course web page under In Class Examples
    there is a copy of the array-based list
    implementation.  Your job is to modify the code
    and implement dynamic array expansion.  You can
    use either the Object version, or the generic
    version of List.  In addition, write a class that
    will test your updated List class to verify that
    it does indeed increase capacity as necessary. 
    Remember, there are two add methods in the List
    ADT and therefore two places in the code where
    the list can become full.
  •  
  • Deliverables  A hard copy of your updated List
    class and a screen shot of your test programs
    execution.
Write a Comment
User Comments (0)
About PowerShow.com