Document Sample

```					                            Linked Lists

many slides taken from Mike Scott, UT Austin

CS 307 Fundamentals of                         1
Computer Science
Recursive Data Structures
 Linked Lists are dynamic data structures
– They grow and shrink one element at a time, normally without some
of the inefficiencies of arrays
 Big O of Array Manipulations
– Access the kth element
– Add or delete an element in the middle of the array while maintaining
relative order
– adding element at the end of array? space avail? no space avail?
– add element at beginning of an array
 If accesses are all at beginning or end of list, a linked
structure offers improvements
 Linked Lists could be used as the underlying storage
container for higher level ADTs (stack, queue)

CS 307 Fundamentals of                                                2
Computer Science
Nodes and Lists
A different way of implementing a list
Each element of a Linked List is a separate
Node object.
Each Node tracks a single piece of data plus
a reference (pointer) to the next
Create a new Node every time we add
something to the List
Remove nodes when item removed from list
and allow garbage collector to reclaim that
memory
CS 307 Fundamentals of                     3
Computer Science
public class Node
{      private Object myData;
private Node myNext;

public Node()
{      myData = null; myNext = null;    }

public Node(Object data, Node next)
{      myData = data;      myNext = next; }

public Object getData()
{      return myData;     }

public Node getNext()
{      return myNext;     }

public void setData(Object data)
{      myData = data;      }

public void setNext(Node next)
{      myNext = next;      }
}
CS 307 Fundamentals of                                     4
Computer Science
One Implementation of a Linked List
The Nodes shown on the previous slide are
– a node refers only to the next node in the
structure
– it is also possible to have doubly linked nodes.
– The node has a reference to the next node in the
structure and the previous node in the structure
as well
How is the end of the list indicated
– myNext = null for last node
– a separate dummy node class / object

CS 307 Fundamentals of                            5
Computer Science
A Simple List Interface
public interface IList
{

Object set(Object item, int pos);

Object get(int pos);

Object remove(int pos);

int size();
}
CS 307 Fundamentals of                              6
Computer Science
private Node myTail;
private int iMySize;

myTail = null;
iMySize = 0;
}
}

myTail    null
CS 307 Fundamentals of                               7
Computer Science
Add Element - List Empty (Before)
null    null     0

Object

item

CS 307 Fundamentals of                       8
Computer Science
Add Element - List Empty (After)
1

Node
Object
myData      myNext
null

CS 307 Fundamentals of                             9
Computer Science
Add Element - List Not Empty (Before)
1

Node
myData  myNext
null

Object                Object
item

CS 307 Fundamentals of                      10
Computer Science
Add Element - List Not Empty (After)
2

Node                      Node
myData  myNext            myData  myNext
null

Object                      Object

CS 307 Fundamentals of                              11
Computer Science

CS 307 Fundamentals of                 12
Computer Science
public void add(Object item, int pos)

CS 307 Fundamentals of                  13
Computer Science
Code for get
public Object get(int pos)

CS 307 Fundamentals of                  14
Computer Science
Code for remove
public Object remove(int pos)

CS 307 Fundamentals of                 15
Computer Science
Are any operations with a Linked List faster
than the version from ArrayList?

Big O? Array version Big O?
Fast performance for removeFront as well?
CS 307 Fundamentals of                     16
Computer Science
Remove Back Method
public Object removeBack()

Big O?
CS 307 Fundamentals of             17
Computer Science
Other Possible Features of
public class DLNode
{      private Object myData;
private DLNode myNext;
private DLNode myPrevious;

}

Circular
Dummy Nodes for first and last node in list

CS 307 Fundamentals of                    18
Computer Science

CS 307 Fundamentals of            19
Computer Science
public void remove(Object item, int pos)

CS 307 Fundamentals of                     20
Computer Science
Code for remove
public Object remove(int pos)

CS 307 Fundamentals of                  21
Computer Science

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 22 posted: 11/29/2011 language: English pages: 21
How are you planning on using Docstoc?