Download presentation
Presentation is loading. Please wait.
1
COMP 103 Linked Structures
School of Engineering and Computer Science, Victoria University of Wellington
2
RECAP-TODAY RECAP TODAY
Reviewing collections – is there a better implementation? TODAY A whole new way to implement Collections: Linked Structures
3
Better Implementations for Collections
We’ve looked at various common collections: lists, stacks, queues, sets, bags, maps. When and how to use them. Implementations of collection, arrays as underlying data structure to ArrayList, ArrayQueue, SortedArrayBag, ArrayPriorityQueue…. Arrays have some inherent limitations for implementing collections: Need contiguous chunks of memory Inserting and removing may mean moving everything up or down Better implementation? Faster insertion and deletion: Linked structures…. Faster lookup: hashing Other kinds of collections: trees and graphs
4
How can we insert faster?
Fast lookup in array ⇒ items must be sorted Arrays stored in contiguous memory. ⇒ inserting new items will be slow You can’t insert fast with an sorted array! To make insert faster, we would need each item to be in its own chunk of memory But, how do we keep track of the order? A B C H J M X P K H P J A X C K B M
5
Linked Structures Put each value in an object with a field for a link to the next Traverse the list by following the links Insert by changing links. No need to shift everything up. Remove by changing links No need to shift things down. H M A J P B C X K
6
Linked List Structures
Can think of this as Nodes inside Nodes: Or as Nodes “pointing” to Nodes: Each node contains a reference to the next node reference = memory location of or “pointer” to object Can view a node in two ways: an object containing two fields the head of a linked list of values J M A C X P J M A C X P shorthand for "null": end of the list
7
Memory allocation What are references/pointers?
A pointer/reference is an address of a chunk of memory, where the data can be stored. How do you get this memory allocated? You’ve been doing it since the start of Comp102, using new: creating an object allocates some chunk memory for the object. new returns the address of the chunk of memory copying the address does not copy the chunk of memory. Memory should be recycled after use: In Java, you don’t have to worry about freeing memory. The garbage collector automatically frees up any memory chunks that no longer have anything pointing/referring to them. In languages without a garbage collector (eg C, C++), you have to do this yourself. OK to ignore in small programs, but requires special care in large programs!
8
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; thing
9
Using Linked Nodes LinkedNode<String> colours = new LinkedNode<String> (“red”, null); colours.setNext(new LinkedNode<String>(“blue”, null)); colours = new LinkedNode<String>(“green”, colours); System.out.printf(“1st: %s ”, colours.get() ); System.out.printf(“2nd: %s ”, colours.next().get() ); System.out.printf(“3rd: %s ”, colours.next().next().get() ); 1st: green nd: red rd: blue “green” colours “red” “blue”
10
Using Linked Nodes Remove the second node:
colours.setNext( colours.next().next() ); Copy colours, then remove first node LinkedNode<String> copy = colours; colours = colours.next(); No references. Garbage collector will get it eventually “red” “green” copy colours “blue”
11
Using Linked Nodes Making a big list: This builds the list backwards -
LinkedNode<Integer> squares = null; for (int i = 1; i < 1000; i++) squares = new LinkedNode<Integer>(i*i, squares); This builds the list backwards - Exercise: Build the list working forwards. 9 4 squares / 1
12
Iterating through a linked list
9 LinkedNode<Integer> rest = squares; while (rest != null){ System.out.printf("%6d \n", rest.get()); rest = rest.next(); } or for (LinkedNode<Integer> rest=squares; rest!=null; rest=rest.next()){ System.out.printf("%6d \n", rest.get()); or we could do it with recursion.... squares 4 rest / 1
13
Two ways to print a linked list
/** Print the values in the list starting at a node */ public void printList(LinkedNode<E> list){ if (list == null) return; System.out.printf("%s, ", list.get()); printList(list.next()); } or for (LinkedNode<E> rest=list; rest!=null; rest=rest.next() ) System.out.printf("%s, ", rest.get()); recursive “cat” list “dog” “cow” iterative as per previous slide
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.