COMP 103 Linked Structures

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
COMP 110 Introduction to Programming Mr. Joshua Stough.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Writing a Good Program 8. Elementary Data Structure.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
List Interface and Linked List Mrs. Furman March 25, 2010.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Linked List, Stacks Queues
Linked Structures – Part 2
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
Anatomy of a linked list
5.13 Recursion Recursive functions Functions that call themselves
Week 4 - Friday CS221.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Big-O notation Linked lists
Data Structure Dr. Mohamed Khafagy.
Introduction to Analysing Costs
ENEE150 Discussion 09 Section 0101 Adam Wang.
UNIT-3 LINKED LIST.
Programming Abstractions
Week 15 – Monday CS221.
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 Binary Search Trees.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Linked Lists.
Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Top Ten Words that Almost Rhyme with “Peas”
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Lists.
Chapter 15 Lists Objectives
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Programming Abstractions
CS2013 Lecture 4 John Hurley Cal State LA.
Dynamic Data Structures
Problem Understanding
Chapter 17: Linked Lists.
Linked Lists.
By Yogesh Neopaney Assistant Professor Department of Computer Science
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Linked Lists.
Linked Lists.
CMPT 225 Lecture 5 – linked list.
Problem Understanding
Presentation transcript:

COMP 103 Linked Structures School of Engineering and Computer Science, Victoria University of Wellington

RECAP-TODAY RECAP TODAY Reviewing collections – is there a better implementation? TODAY A whole new way to implement Collections: Linked Structures

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

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

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

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

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!

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

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 2nd: red 3rd: blue    “green” colours “red” “blue”

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”

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

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

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