Lecture 7:  Linked Lists

 

conceptual picture:

        a list with 3 items, pointed to by a variable L

 

 

 

 


·       a linked list consists of nodes

·       each node contains

o      data

o      a pointer to the next node, or null

·       each node is an object, so implement them by defining a Listnode class

public class Listnode {

    // fields – can make these public instead of private

    private Object data;

    private Listnode next;

    // constructors

    public Listnode(Object ob) { data = ob; next=null;}

    public Listnode(Object ob, Listnode n){ … };

    // methods

public Object getData() {return data; }

    public Listnode getNext() {return next;}

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

    public void setNext(Listnode n) {next = n;}

}

example:  create a list with 2 nodes:  "ant" and "bat"

 

next

 

bat

 

ant

 

L

 

data

 
goal:

 

 

 

 

alternative algorithms:

1.      create Listnode with "ant" pointed to by L

create Listnode with "bat" pointed to by tmp

create link from 1st node to second node

Listnode L = new Listnode("ant");

Listnode tmp = new Listnode("bat");

L.setNext(tmp);

 

2.    create Listnode with "bat" pointed to by L

create & link 1st node

Listnode L = new Listnode("bat");

L = new Listnode("ant",L);

 

3.    do it all in one statement

Listnode L = new Listnode("ant",

new Listnode("bat"));

 

You try

 

L

 
assume you have:

ant

 

bat

 
 

 

 


1.      write code to change 1st node's data from "ant" to "fly"

 

L.setData("fly");

 

2.    to change 2nd node's data from "bat" to "cat"

 

L.getNext().setData("cat");

 

3.    to add a new node with "rat" at the front of the list

 

L = new Listnode("rat",L);

 

 

4.    to add a new node with "rat" between the 1st and 2nd nodes

 

tmp = new Listnode("rat",L.getNext());

L.setNext(tmp);

 

L.setNext(new Listnode("rat",L.getNext()));

 

 

Listnode assignment statements:

 


Listnode L1 = new Listnode("hi");

 

 


L2

 
Listnode L2 = L1;

 

·       L2 has same value as L1

·       L2 points to same Listnode as L1  ("aliasing")

·       Listnode is not copied – L2.setData("no");  // changes L1

·       L2 doesn't point to L1;  L1 = null; //doesn't change L2

 

Linked Lists - review:

·      

tmp

 

cat

 

L

 
Listnode:

·       Linked list:

ant

 

bat

 

cat

 
 

 


·       code not in the Listnode class would use

L.getData();  L.getNext();

L.getNext().getData();

L.getNext().getNext();

 

Next:

·       how to add/remove a node

·       how to implement the LinkedList class

·       compare LinkedList and ArrayList classes

 

In general, to access each node in a linked list

 

·       do not use L.getNext().getNext().getNext();

·       use a "tmp" pointer to traverse the list until @ end

 

 

 

 


Listnode tmp = L;

while(tmp != null) {

    System.out.println(tmp.getData());

    tmp = tmp.getNext();

}

Remove the node after the node pointed to by tmp:

 

 

 

 


In English,

        set "next" field of tmp equal to tmp.getNext().getNext()

         tmp2 = tmp.getNext().getNext();

         tmp.setNext(tmp2);

    or

tmp.setNext(tmp.getNext().getNext());

 

·       time complexity:  O(1)

·       problem:  can't use this code to remove the 1st item in L

o      code to remove node pointed to by tmp is O(N)

 

Add a node after the node pointed to by tmp:

        create a new Listnode with "xx" and tmp.getNext()

set "next" field of tmp equal to new Listnode

    tmp.setNext(new Listnode("xx",tmp.getNext());

·       time complexity:  O(1)

·       problem:  can't use this code to add a 1st item in L

 

Solution: use a "header node"

Using a header node

Goal:  avoid special case code when adding or removing 1st node

Approach:  keep a special "dummy" node at front of list

 

 

 

L

 
 

 


if tmp points to dummy node,

        remove node after node pointed to by tmp

removes 1st item in the list

        add node after node pointed to by tmp

                adds a 1st item in the list

·       code that initializes the list must create the header node

Listnode L = new Listnode(null);

·       other operations must know List starts after header node

o      size() must not count header node

o      "contains" must start at L.getNext() instead of L

 

From now on, we'll assume a header node, unless otherwise specified