Linked Lists based on slides created by Ethan Apter

Slides:



Advertisements
Similar presentations
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.
Advertisements

Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
CSE 143 Lecture 6 Linked Lists slides created by Ethan Apter
1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(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.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
Basic Java Syntax Comments Basic data types Operators and assignment.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
slides adapted from Marty Stepp and Hélène Martin
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Lists Rem Collier Room A1.02
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.
Jeff Edmonds York University
CSE 374 Programming Concepts & Tools
UNIT-3 LINKED LIST.
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 node problem 3 What set of statements turns this picture:
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked node problem 3 What set of statements turns this picture:
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
Stacks, Queues, and Deques
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
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
More Linked Lists and Loops Based on slides by Ethan Apter
CC 215 Data Structures Pointers (review and examples)
slides created by Ethan Apter
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 More ArrayIntList:
slides created by Ethan Apter
Recall: stacks and queues
Introduction to C++ Linear Linked Lists
CSE 143 Lecture 5 References and Linked Nodes
Java Programming Language
CSC 143 Java Linked Lists.
slides created by Marty Stepp
CSE 143 Lecture 2 ArrayIntList, binary search
CS2013 Lecture 7 John Hurley Cal State LA.
slides created by Ethan Apter
slides created by Ethan Apter
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 4 Implementing ArrayIntList reading:
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
CSE 143 Lecture 21 Advanced List Implementation
slides created by Ethan Apter
Lecture 6: References and linked nodes reading: 16.1
Stacks – Cont’d Nour El-Kadri CSI 1101.
slides created by Marty Stepp
Presentation transcript:

Linked Lists based on slides created by Ethan Apter CSE 143 Lecture 6 Linked Lists based on slides created by Ethan Apter

ArrayIntList Example (1) ArrayIntList list1; // declares a variable list1

ArrayIntList Example (2) ArrayIntList list1 = new ArrayIntList(); size list1 elementData An ArrayIntList object

ArrayIntList Class public class ArrayIntList { private int[] elementData; // Declares a variable private int size; // Declares a variable ...

ArrayIntList Example (3) public ArrayIntList(int capacity) { if (capacity < 0) throw new IllegalArgumentException(); elementData = new int[capacity]; size = 0; } 0 1 2 3 4 0 0 0 0 0 size list1 elementData An Array object An ArrayIntList object

ArrayIntList Full Picture ArrayIntList list1 = new ArrayIntList(5); public ArrayIntList(int capacity) { if (capacity < 0) throw new IllegalArgumentException(); elementData = new int[capacity]; size = 0; } 0 1 2 3 4 0 0 0 0 0 size list1 elementData An Array object An ArrayIntList object

ArrayIntList (Abbreviated Picture) ArrayIntList list1 = new ArrayIntList(5); list1.add(6); list1.add(2); list1.add(5); list1.add(3); 0 1 2 3 4 6 2 5 3 0 list1 elementData size

Arrays in ArrayIntList Our ArrayIntList class uses arrays Arrays use a contiguous block of memory, all elements are adjacent to each other 0 1 2 3 4 6 2 5 3 0 list1 elementData size

Advantages and Disadvantages Advantages of array-based lists Disadvantages of array-based lists

Linked Lists A linked list is a type of list But instead of a contiguous block of memory, like this: 0 1 2 3 4 5 6 7 8 9 Linked list elements are scattered throughout memory: 8 9 7 5 6 But now the elements are unordered. How do linked lists keep track of everything?

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

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

ListNode Code for a simple ListNode class containing ints 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.

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 hold a reference to a ListNode We will draw front like this: front ? We’ll replace the ? with a reference to an actual ListNode soon

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”)

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.data front.next

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 3 7 0 null front.next.data front.next.next

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 3 7 12 null front.next.next.data front.next.next.next

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 3 7 12

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

Can we build this in one line? data next data next data next front 3 7 12 ListNode front = new ListNode(); front.data = 3; front.next = new ListNode(); front.next.data = 7; front.next.next = new ListNode(); front.next.next.data = 12; front.next.next.next = null;

Can we build this in one line? data next data next data next front 3 7 12

Can we build this in one line? data next data next data next front 3 7 12 ListNode front = new ListNode(3,new ListNode(7,new ListNode(12)));

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 q 3 9 How many variables of type ListNode are there? How many ListNode objects are there?

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

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

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

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

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 q 3 9

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 q 3 9 ...we permanently lose the ListNode containing the 3

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

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

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

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

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

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

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

Before/After Problems (2) Consider this situation: data next data next p 2 4 q 3 9 How would you transform it to the following picture? data next data next data next p 2 4 9 data next q 3

Before/After Problems (2) data next data next p 2 4 q 3 9 Code

Before/After Problems (2) data next data next p 2 4 q 3 9 Code p.next.next = q.next; q.next = null;

Before/After Problems (3) Consider this situation: data next data next p 2 4 q 3 9 How would you transform it to the following picture? data next data next data next p 2 9 4 data next q 3

Before/After Problems (3) data next data next p 2 4 q 3 9 Code q.next.next = p.next; p.next = q.next; q.next = null;

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).