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

Slides:



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

Chapter 17 Linked List Saurav Karmakar Spring 2007.
COMP 103 Linked Stack and Linked Queue.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
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.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
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.
CSCS-200 Data Structure and Algorithms Lecture
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
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.
COMP 103 Linked Structures
Linked Structures – Part 2
Data Structure By Amee Trivedi.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
CSCI-255 LinkedList.
Linked lists.
Programming Abstractions
COMP 103 Binary Search Trees.
LINKED LISTS CSCD Linked Lists.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 15 Lists Objectives
Arrays and Linked Lists
Programming Abstractions
By Yogesh Neopaney Assistant Professor Department of Computer Science
Data Structures & Algorithms
Linked List Functions.
Linked Lists.
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Linked lists.
Presentation transcript:

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

2 RECAP-TODAY RECAP  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 ? BAJHC MXP K B A J H C M X P K

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 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 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 { 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 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 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 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 squares /

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 squares rest /

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