CSE 143 Lecture 6 Linked Lists slides created by Ethan Apter

Slides:



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

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Preliminaries Attendance sheets –I remembered! HW1 due tonight –progress report Stage 1 due on Friday –progress report.
No homework this week Stage 2 starts next week. Code review Team with N members is assigned N submissions to review Discuss submissions within team Everyone.
CPSC 231 Organizing Files for Performance (D.H.) 1 LEARNING OBJECTIVES Data compression. Reclaiming space in files. Compaction. Searching. Sorting, Keysorting.
Lists We’ve seen an array-based list implementation, the ArrayList.
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Stacks, Queues, and Deques
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
2014-T2 Lecture 19 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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
The ArrayList Data Structure Standard Arrays at High Speed!
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Lecture 6 of Computer Science II
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
slides created by Marty Stepp and Ethan Apter
slides created by Ethan Apter
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
More Linked Lists and Loops Based on slides by Ethan Apter
slides created by Ethan Apter
slides adapted from Marty Stepp and Hélène Martin
slides created by Ethan Apter
Introduction to C++ Linear Linked Lists
CSE 143 Lecture 5 References and Linked Nodes
CSC 143 Java Linked Lists.
slides created by Marty Stepp
Linked Lists based on slides created by Ethan Apter
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
slides created by Ethan Apter
Complexity Based on slides by Ethan Apter
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
slides adapted from Marty Stepp
slides created by Ethan Apter and Marty Stepp
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
slides created by Ethan Apter
Lecture 6: References and linked nodes reading: 16.1
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 6 Linked Lists slides created by Ethan Apter

2 Array-Based List Review Array-based lists are what we’ve studied so far –ArrayIntList, ArrayList, SortedIntList all use arrays Arrays use a contiguous block of memory This means all elements are adjacent to each other

3 Advantages and Disadvantages Advantages of array-based lists –random access: can get any element in the entire array quickly kind of like jumping to any scene on a DVD (no fast-forwarding required) Disadvantages of array-based lists –can’t insert/remove elements at the front/middle easily have to shift the other elements –can’t resize array easily have to create a new, bigger array

4 Linked Lists A linked list is a type of list But instead of a contiguous block of memory, like this: Linked list elements are scattered throughout memory: But now the elements are unordered. How do linked lists keep track of everything?

5 Linked Lists Each element must have a reference to the next element: Now, so long as we keep track of the first element (the front), we can keep track of all the elements front back

6 Linked Lists These references to the next element mean that linked lists have sequential access This means that to get to elements in the middle of the list, we must first start at the front and follow all the links until we get to the middle: –kind of like fast-forwarding on a VHS tape –so getting elements from the middle/back is slow Linked lists also do some things well: –linked lists can insert elements quickly (no “shifting” needed) –linked lists can always add more elements (no set capacity) So there are tradeoffs between array lists and linked lists

7 List Nodes List node: an element in a linked list –an individual list nodes is very simple, but multiple list nodes can be used to build complex structures Each list node contains: –a piece of data –a reference to the next list node We typically draw list nodes like this: data next 18

8 ListNode Code for a simple ListNode class containing int s as data: public class ListNode { public int data; public ListNode next; } ListNode is poorly encapsulated (it has public fields) –but that’s ok for now. We’ll talk about it more, later.

9 ListNode ListNode is a recursive data structure This means a ListNode is defined in terms of itself A ListNode contains: –data –a reference to a ListNode A ListNode does not contain another ListNode –Instead, it contains a reference to another ListNode

10 Building a Small Linked List Let’s make a short linked list containing 3, 7, and 12 First, we need a reference to the front of the linked list: ListNode front; The variable front is not a ListNode –it is just a variable that can refer to a ListNode We will draw front like this: front ? We’ll replace the ? with an actual ListNode soon

11 Building a Small Linked List To make an actual ListNode, we must call new : front = new ListNode(); This constructs a new node and makes front refer to it: data next front 0 null Notice that Java automatically initialized data and next to their zero-equivalents –ints: 0 –objects: null (means “no object”)

12 Building a Small Linked List In this first node, we want to store a 3 and have it point to a new node. We can use dot notation for this: front.data = 3; front.next = new ListNode(); And now our list looks like this: data next data next front 3 0 null front.datafront.next

13 Building a Small Linked List In the second node, we want to store a 7 and have it point to a new node: front.next.data = 7; front.next.next = new ListNode(); And now our list looks like this: data next data next data next front null front.next.datafront.next.next

14 Building a Small Linked List In the last node, we want to store a 12 and terminate our list: front.next.next.data = 12; front.next.next.next = null; And now our completed list looks like this: data next data next data next front null front.next.next.datafront.next.next.next

15 Building a Small Linked List It wasn’t strictly necessary to set the last field to null Java had already done it, since null is a zero-equivalent But it’s ok to be explicit about this kind of thing Also, we normally draw null as a diagonal line: data next data next data next front

16 Improving ListNode Let’s add some constructors to our ListNode class: public class ListNode { public int data; public ListNode next; public ListNode() { this(0, null); } public ListNode(int data) { this(data, null); } public ListNode(int data, ListNode next) { this.data = data; this.next = next; } } Notice we still have one “main” constructor that is called by the other two

17 Better Linked List Code Our new ListNode constructors allows us to build our list containing 3, 7, and 12 in just one line of code: ListNode front = new ListNode(3, new ListNode(7, new ListNode(12))); This is a huge improvement –but it’s still tedious and error-prone There is a better way! We can use loops on linked lists...but we won’t for a little longer –working with list nodes is challenging, so let’s get more practice

18 Basic Linked List Questions Suppose you have two variables of type ListNode named p and q. Consider the following situation: data next data next p 2 4 data next data next q 3 9 How many variables of type ListNode are there? How many ListNode objects are there?

19 Basic Linked List Questions How many variables of type ListNode are there? –6, circled in green data next data next p 2 4 data next data next q 3 9

20 Basic Linked List Questions How many ListNode objects are there? –4, circled in green data next data next p 2 4 data next data next q 3 9

21 Before/After Problems Consider the same situation as before: data next data next p 2 4 data next data next q 3 9 How would you transform it to the following picture? data next data next data next p data next q 9

22 Before/After Problems Which variables need to change in order to arrive at the solution? data next data next p 2 4 data next data next q 3 9

23 Before/After Problems Which variables/links need to change in order to arrive at the solution? –3, colored green data next data next p 2 4 data next data next q 3 9

24 Before/After Problems But the order we change the links is also important In the final situation, q should point to the ListNode containing the 9. But if we do that first: data next data next p 2 4 data next data next q we permanently lose the ListNode containing the 3

25 Before/After Problems So how do we actually solve it? data next data next p 2 4 data next data next q 3 9 Change this one first. It points to null, so we can’t lose anything by changing it

26 Before/After Problems Modifying p.next.next : data next data next p 2 4 data next data next q 3 9 Code p.next.next = q;

27 Before/After Problems What’s next? data next data next p 2 4 data next data next q 3 9 The ListNode referred to by q is now saved, so we can now safely change what q refers to

28 Before/After Problems Modifying q : data next data next p 2 4 data next data next q 3 9 Updated code p.next.next = q; q = q.next;

29 Before/After Problems What’s next? data next data next p 2 4 data next data next q 3 9 The ListNode containing the 3 should refer to null. It is now safe to change this link.

30 Before/After Problems Modifying p.next.next.next : data next data next p 2 4 data next data next q 3 9 Code p.next.next = q; q = q.next; p.next.next.next = null;

31 Before/After Problems We’re all done (even though it looks weird) data next data next p 2 4 data next data next q 3 9 Code p.next.next = q; q = q.next; p.next.next.next = null;

32 Final Thoughts Working with linked lists can be hard So draw lots of pictures! jGRASP’s debugger can also be helpful –but remember: you won’t have jGRASP on the exams –and linked lists are definitely on the exams Sometimes, solving one of these problems requires a temporary variable: ListNode temp = p; This creates a ListNode variable. It does not create a new ListNode object (no call on new ).