Download presentation
Presentation is loading. Please wait.
Published byAlexina Chapman Modified over 9 years ago
1
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures
2
2 RECAP-TODAY RECAP Reviewing collections – is there a better implementation ? TODAY A whole new way to implement Collections: Linked Structures
3
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
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 ? BAJHC MXP K B A J H C M X P K
5
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. B A J H C M X P K
6
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 JMACXP JMA C XP shorthand for "null": end of the list
7
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
8 A Linked Node class: public class LinkedNode { private E value; private LinkedNode next; public LinkedNode(E item, LinkedNode nextNode) { value = item; next = nextNode; } public E get() { return value; } public LinkedNode next() { return next; } public void set(E item) { value = item; } public void setNext(LinkedNode nextNode) { next = nextNode; } } thing
9
9 Using Linked Nodes LinkedNode colours = new LinkedNode (“red”, null); colours.setNext(new LinkedNode (“blue”, null)); colours = new LinkedNode (“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() ); 1 st : green 2 nd : red 3 rd : blue colours “red”“blue”“green”
10
10 Using Linked Nodes Remove the second node: colours.setNext( colours.next().next() ); Copy colours, then remove first node LinkedNode copy = colours; colours = colours.next(); “red” colours “green”“blue” copy No references. Garbage collector will get it eventually
11
11 Using Linked Nodes Making a big list: LinkedNode squares = null; for (int i = 1; i (i*i, squares); This builds the list backwards - Exercise: Build the list working forwards. 49 1 squares /
12
12 Iterating through a linked list LinkedNode rest = squares; while (rest != null){ System.out.printf("%6d \n", rest.get()); rest = rest.next(); } or for (LinkedNode rest=squares; rest!=null; rest=rest.next()){ System.out.printf("%6d \n", rest.get()); } or we could do it with recursion.... 49 1 squares rest /
13
13 T wo ways to print a linked list /** Print the values in the list starting at a node */ public void printList(LinkedNode list){ if (list == null) return; System.out.printf("%s, ", list.get()); printList(list.next()); } or public void printList(LinkedNode list){ for (LinkedNode rest=list; rest!=null; rest=rest.next() ) System.out.printf("%s, ", rest.get()); } “dog”“cat” “cow” list recursive iterative as per previous slide
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.