Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Menu

• Linked Lists

• Test (Reminder):
• There will be a 90 minute test
• 15th December 15:00-17:00
• Practice: past tests and exams
available.

1
Where have we been?
Implementing Collections:

• ArrayList: O(n) to add/remove, except at end

• Stack: O(1)

• ArrayQueue: O(1)
• ArraySet: O(n) (cost of searching)

• SortedArraySet O(log(n)) to search (with binary


search)
O(n) to add/remove (cost of inserting)
O(n2) to add n items
O(n log(n)) to initialise with n items.
(with fast sorting)
2
Dynamic Data Structures
• Arrays are very fast to access and create, but…
• we cannot easily adjust the size of them during
execution.
• If our array is too small - our program will not work for
large amounts of data,
• If our array is too big - we waste a lot of space.
• We can make a new larger array, but then we have to
copy all of the elements from the old to the new one.
• and, for maintaining sorted lists, we need to insert into
the body of the array, like so… A B C H J M P X

• but, we can’t do this with an array! K

• as the Array is stored in a contiguous chunk of memory,


• We have to copy items to new positions to create a space.
3
How can we insert fast?
• The right solution is to build the data structure
from small pieces, and add a new piece
whenever we need to make it larger.

• To insert, each item to be in its own chunk, so we


don’t need to move them about to make space.

H P
A J

C X
B K M

• But, how do we keep track of the order?


4
Linked Structures
• Put each value in an object with a field for a link
to the next
H M
A
J

P
B C

X
K

• Insert by changing the links!


5
Linked List Structures
• Each value is in a “Node”
• ;
• Each Node contains a field with the next Node, or null

H
M
J P

A C
X

• Usually drawn with “pointers” (links)


6
Linked List Structures
• Can be drawn with Nodes inside Nodes:

A C J M P X

• Or with ‘Pointers’:
• Each node contains a reference to the next object

A C J M P X

• Can view a node in two ways:


• an object containing two fields
• the head of a linked list of values
7
So, what are Pointers?

• Pointers in Real Life


• In many ways, telephone numbers provide a
good everyday analogy to pointers.
• To contact someone:
• All you need is their telephone number, you do not
• have to have a copy (clone) of them with you.
• Many different people can all have your number
simultaneously. All you need do is copy the pointer.
• Combinations of pointers allow more
sophisticated structures. For example, trees or
graphs.

8
Memory allocation
• Pointers/references contain an address in the computer’s
memory, where the data can be stored. How is this memory
allocated?
• You’ve been doing it (at least) since Comp102, using new to
create an object will allocate some of the computer’s memory for
your object. This memory is called heap memory:
Object o = new Object(); o Object
Object p = o; p

o is the address of the memory location where the object is


stored. The reference/pointer o can be copied (e.g. p) and used in
many places, but there is only one copy of the ‘actual object’
referred to.

• Memory used for dynamic storage must be recycled after use:


• The garbage collector automatically frees up any memory chunks that
no longer have anything pointing/referring to them.
• This frees you from having to worry about explicitly freeing memory. 9
A Linked Node class:
public class LinkedNode <E>{
private E value;
private LinkedNode<E> next;

public LinkedNode(E item, LinkedNode<E> nextNode){


    value = item;
    next = nextNode;
}

public E get() { return value; }

public LinkedNode<E> next() {  return next; }

public void set(E item) {


value = item; 
}
public void setNext(LinkedNode<E> nextNode) { 
next = nextNode; 
}

10
Adding Nodes
LinkedNode<String> colours = new LinkedNode<String>(“red”,
null);
colours.setNext(new LinkedNode<String>(“blue”, null));
colours = new LinkedNode<String>(“green”, colours);

“green”

colours
“red” “blue”

System.out.format(“1st: %s\n”, colours.get() );


System.out.format(“2nd: %s\n”, colours.next().get() );
System.out.format(“3rd: %s\n”, colours.next().next().get() );
11
Node Deletion
• Remove the second node:
Garbage
colours.setNext(colours.next().next()); Collector

copy “green” “red”

colours
“blue”

• Copy colours, then remove first node

LinkedNode<String> copy = colours;


colours = colours.next();
12
Iterating over a linked list
LinkedNode<Integer> squares = null;

for (int i = 1; i < 1000; i++)


squares= new LinkedNode<Integer>( i*i, squares);

LinkedNode<Integer> rest = squares;


while(rest != null){
System.out.format("%6d \n", rest.get());
 rest = rest.next();
}
or

for (LinkedNode<Integer> rest=squares; rest!=null;


rest=rest.next()){
      System.out.format("%6d \n", rest.get());
}
13
Exercise:
• Write a method to return the value in the LAST node of a
list:

public class LinkedNode <E>{


private E value;
private LinkedNode<E> next;
:
/** Returns the value in the last node in the list from this node */
public E lastValue(){
if (next==null) return value;
else return next.lastValue();
}

14

You might also like