Linked node problem 3 What set of statements turns this picture:

Slides:



Advertisements
Similar presentations
Chapter 17 Linked List.
Advertisements

CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
slides adapted from Marty Stepp and Hélène Martin
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Building Java Programs Chapter 16
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
UNIT – I Linked Lists.
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
Doubly Linked List Review - We are writing this code
Algorithm for deleting a node from a singly linked list
slides created by Marty Stepp
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Linked Lists.
CSCI 3333 Data Structures Linked Lists.
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Road Map CS Concepts Data Structures Java Language Java Collections
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
Topic 11 Linked Lists -Joel Spolsky
Building Java Programs
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
slides created by Marty Stepp and Ethan Apter
Linked List (Part I) Data structure.
Building Java Programs
Building Java Programs
Lecture 26: Advanced List Implementation
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Lecture 7: Linked List Basics reading: 16.2
More Linked Lists and Loops Based on slides by Ethan Apter
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Programming Abstractions
changing a list There are only two ways to change a linked list:
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
Recall: stacks and queues
Building Java Programs
CSE 143 Lecture 5 References and Linked Nodes
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Road Map CS Concepts Data Structures Java Language Java Collections
Building Java Programs
slides created by Marty Stepp
Recall: stacks and queues
Linked Lists based on slides created by Ethan Apter
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Ordered Structures Wellesley College CS230 Lecture 10
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
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
slides created by Ethan Apter
Linked Lists.
slides created by Marty Stepp
Lecture 6: References and linked nodes reading: 16.1
Topic 11 Linked Lists -Joel Spolsky
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Linked node problem 3 What set of statements turns this picture: Into this? data next 10 data next 20 list1 data next 30 data next 40 list2 data next 10 data next 20 data next 30 list1 data next 40 list2

Linked node problem 3 How many ListNode variables? Which variables change? B C data next 10 data next 20 list1 A E F data next 30 data next 40 list2 D C E data next 10 data next 20 data next 30 list1 data next 40 list2 D

Linked node question Suppose we have a long chain of list nodes: We don't know exactly how long the chain is. How would we print the data values in all the nodes? data next 10 data next 20 data next 990 list ...

Algorithm pseudocode Start at the front of the list. While (there are more nodes to print): Print the current node's data. Go to the next node. How do we walk through the nodes of the list? list = list.next; // is this a good idea? data next 10 data next 20 data next 990 list ...

Linked List vs. Array Similar to array code: Print list values: int[] a = ...; int i = 0; while (i < a.length) { System.out.println(a[i]); i++; } Print list values: ListNode list= ...; ListNode current = list; while (current != null) { System.out.println(current.data); current = current.next; } Description Array Code Linked List Code Go to front of list int i = 0; ListNode current = list; Test for more elements i < size current != null Current value elementData[i] current.data Go to next element i++; // i=i+1 current = current.next;

Before/After Before After data next 10 data next 20 data next 30 front 40 front

changing a list There are only two ways to change a linked list: Change the value of front (modify the front of the list) Change the value of <node>.next (modify middle or end of list to point somewhere else) Implications: To add in the middle, need a reference to the previous node Front is often a special case