SlideShare a Scribd company logo
CSE 143
Lecture 10
Linked List Basics
reading: 16.1 - 16.2
slides created by Marty Stepp
http://www.cs.washington.edu/143/
2
References vs. objects
variable = value;
a variable (left side of = ) is an arrow (the base of an arrow)
a value (right side of = ) is an object (a box; what an arrow points at)
• For the list at right:
– a.next = value;
means to adjust where points
– variable = a.next;
means to make variable point at
data next
10
a
data next
20
1
2
1
2
3
Reassigning references
• when you say:
– a.next = b.next;
• you are saying:
– "Make the variable a.next refer to the same value as b.next."
– Or, "Make a.next point to the same place that b.next points."
data next
10
a
data next
20
data next
30
b
data next
40
4
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
990
list
...
data next
20
5
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
990
list
...
data next
20
6
Traversing a list?
• One (bad) way to print every value in the list:
while (list != null) {
System.out.println(list.data);
list = list.next; // move to next node
}
– What's wrong with this approach?
• (It loses the linked list as it prints it!)
data next
10
data next
990
list
...
data next
20
7
A current reference
• Don't change list. Make another variable, and change that.
– A ListNode variable is NOT a ListNode object
ListNode current = list;
• What happens to the picture above when we write:
current = current.next;
data next
10
data next
990
list
...
data next
20
current
8
Traversing a list correctly
• The correct way to print every value in the list:
ListNode current = list;
while (current != null) {
System.out.println(current.data);
current = current.next; // move to next node
}
– Changing current does not damage the list.
data next
10
data next
990
list
...
data next
20
9
Linked list vs. array
• Algorithm to print list values:
ListNode front = ...;
ListNode current = front;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
• Similar to array code:
int[] a = ...;
int i = 0;
while (i < a.length) {
System.out.println(a[i]);
i++;
}
10
A LinkedIntList class
• Let's write a collection class named LinkedIntList.
– Has the same methods as ArrayIntList:
•add, add, get, indexOf, remove, size, toString
– The list is internally implemented as a chain of linked nodes
• The LinkedIntList keeps a reference to its front as a field
•null is the end of the list; a null front signifies an empty list
front
add(value)
add(index, value)
indexOf(value)
remove(index)
size()
toString()
LinkedIntList
ListNode ListNode ListNode
data next
42
data next
-3
data next
17
element 0 element 1 element 2
11
LinkedIntList class v1
public class LinkedIntList {
private ListNode front;
public LinkedIntList() {
front = null;
}
methods go here
}
front =
LinkedIntList
12
Implementing add
// Adds the given value to the end of the list.
public void add(int value) {
...
}
– How do we add a new node to the end of a list?
– Does it matter what the list's contents are before the add?
front =
data next
42
data next
-3
data next
17
element 0 element 1 element 2
13
Adding to an empty list
• Before adding 20: After:
– We must create a new node and attach it to the list.
front = front =
data next
20
element 0
14
The add method, 1st try
// Adds the given value to the end of the list.
public void add(int value) {
if (front == null) {
// adding to an empty list
front = new ListNode(value);
} else {
// adding to the end of an existing list
...
}
}
15
Adding to non-empty list
• Before adding value 20 to end of list:
• After:
front =
data next
42
data next
-3
front =
data next
42
data next
-3
data next
20
element 0 element 1 element 2
element 0 element 1
16
Don't fall off the edge!
• To add/remove from a list, you must modify the next
reference of the node before the place you want to change.
– Where should current be pointing, to add 20 at the end?
– What loop test will stop us at this place in the list?
front =
data next
42
data next
-3
element 0 element 1
17
The add method
// Adds the given value to the end of the list.
public void add(int value) {
if (front == null) {
// adding to an empty list
front = new ListNode(value);
} else {
// adding to the end of an existing list
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
}
18
Implementing get
// Returns value in list at given index.
public int get(int index) {
...
}
– Exercise: Implement the get method.
front =
data next
42
data next
-3
data next
17
element 0 element 1 element 2
19
The get method
// Returns value in list at given index.
// Precondition: 0 <= index < size()
public int get(int index) {
ListNode current = front;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
20
Implementing add (2)
// Inserts the given value at the given index.
public void add(int index, int value) {
...
}
– Exercise: Implement the two-parameter add method.
front =
data next
42
data next
-3
data next
17
element 0 element 1 element 2
21
The add method (2)
// Inserts the given value at the given index.
// Precondition: 0 <= index <= size()
public void add(int index, int value) {
if (index == 0) {
// adding to an empty list
front = new ListNode(value, front);
} else {
// inserting into an existing list
ListNode current = front;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = new ListNode(value,
current.next);
}
}

More Related Content

PDF
Week 2 - Data Structures and Algorithms
PDF
Objective The purpose of this exercise is to create a Linked List d.pdf
PPT
Data structures
PDF
Please and Thank youObjective The purpose of this exercise is to .pdf
PPT
Doubly Circular Linked List – Both next and previous pointers form a circular...
PDF
Implementation The starter code includes List.java. You should not c.pdf
PDF
linklisr
Week 2 - Data Structures and Algorithms
Objective The purpose of this exercise is to create a Linked List d.pdf
Data structures
Please and Thank youObjective The purpose of this exercise is to .pdf
Doubly Circular Linked List – Both next and previous pointers form a circular...
Implementation The starter code includes List.java. You should not c.pdf
linklisr

Similar to 10-linked-list.ppt (20)

PPTX
Linked lists a
PDF
Linked List Objective The purpose of this exercise is to cr.pdf
PPT
PPT
PDF
Objective The purpose of this exercise is to create a Linke.pdf
PDF
Objective The purpose of this exercise is to create a Linke.pdf
PPT
PPT
Topic 14 Linked Lists data type CS upload
DOCX
Lecture 18Dynamic Data Structures and Generics (II).docx
PPT
singly link list project in dsa.....by rohit malav
PPTX
Chapter 4 Linked List introduction lessons.pptx
PDF
please i need help Im writing a program to test the merge sort alg.pdf
PPT
List data structure
PPT
List Data Structure
PDF
Linked lists
PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
Note             Given Code modified as required and required met.pdf
PPT
Jhtp5 20 Datastructures
DOCX
Link list assi
PPT
1 list datastructures
Linked lists a
Linked List Objective The purpose of this exercise is to cr.pdf
Objective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdf
Topic 14 Linked Lists data type CS upload
Lecture 18Dynamic Data Structures and Generics (II).docx
singly link list project in dsa.....by rohit malav
Chapter 4 Linked List introduction lessons.pptx
please i need help Im writing a program to test the merge sort alg.pdf
List data structure
List Data Structure
Linked lists
File LinkedList.java Defines a doubly-l.pdf
Note             Given Code modified as required and required met.pdf
Jhtp5 20 Datastructures
Link list assi
1 list datastructures
Ad

Recently uploaded (20)

PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPT
Quality review (1)_presentation of this 21
PDF
Introduction to the R Programming Language
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
Business Analytics and business intelligence.pdf
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Introduction to Data Science and Data Analysis
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Quality review (1)_presentation of this 21
Introduction to the R Programming Language
.pdf is not working space design for the following data for the following dat...
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
SAP 2 completion done . PRESENTATION.pptx
1_Introduction to advance data techniques.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Business Analytics and business intelligence.pdf
Galatica Smart Energy Infrastructure Startup Pitch Deck
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
Miokarditis (Inflamasi pada Otot Jantung)
Data_Analytics_and_PowerBI_Presentation.pptx
climate analysis of Dhaka ,Banglades.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Introduction to Data Science and Data Analysis
Ad

10-linked-list.ppt

  • 1. CSE 143 Lecture 10 Linked List Basics reading: 16.1 - 16.2 slides created by Marty Stepp http://www.cs.washington.edu/143/
  • 2. 2 References vs. objects variable = value; a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box; what an arrow points at) • For the list at right: – a.next = value; means to adjust where points – variable = a.next; means to make variable point at data next 10 a data next 20 1 2 1 2
  • 3. 3 Reassigning references • when you say: – a.next = b.next; • you are saying: – "Make the variable a.next refer to the same value as b.next." – Or, "Make a.next point to the same place that b.next points." data next 10 a data next 20 data next 30 b data next 40
  • 4. 4 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 990 list ... data next 20
  • 5. 5 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 990 list ... data next 20
  • 6. 6 Traversing a list? • One (bad) way to print every value in the list: while (list != null) { System.out.println(list.data); list = list.next; // move to next node } – What's wrong with this approach? • (It loses the linked list as it prints it!) data next 10 data next 990 list ... data next 20
  • 7. 7 A current reference • Don't change list. Make another variable, and change that. – A ListNode variable is NOT a ListNode object ListNode current = list; • What happens to the picture above when we write: current = current.next; data next 10 data next 990 list ... data next 20 current
  • 8. 8 Traversing a list correctly • The correct way to print every value in the list: ListNode current = list; while (current != null) { System.out.println(current.data); current = current.next; // move to next node } – Changing current does not damage the list. data next 10 data next 990 list ... data next 20
  • 9. 9 Linked list vs. array • Algorithm to print list values: ListNode front = ...; ListNode current = front; while (current != null) { System.out.println(current.data); current = current.next; } • Similar to array code: int[] a = ...; int i = 0; while (i < a.length) { System.out.println(a[i]); i++; }
  • 10. 10 A LinkedIntList class • Let's write a collection class named LinkedIntList. – Has the same methods as ArrayIntList: •add, add, get, indexOf, remove, size, toString – The list is internally implemented as a chain of linked nodes • The LinkedIntList keeps a reference to its front as a field •null is the end of the list; a null front signifies an empty list front add(value) add(index, value) indexOf(value) remove(index) size() toString() LinkedIntList ListNode ListNode ListNode data next 42 data next -3 data next 17 element 0 element 1 element 2
  • 11. 11 LinkedIntList class v1 public class LinkedIntList { private ListNode front; public LinkedIntList() { front = null; } methods go here } front = LinkedIntList
  • 12. 12 Implementing add // Adds the given value to the end of the list. public void add(int value) { ... } – How do we add a new node to the end of a list? – Does it matter what the list's contents are before the add? front = data next 42 data next -3 data next 17 element 0 element 1 element 2
  • 13. 13 Adding to an empty list • Before adding 20: After: – We must create a new node and attach it to the list. front = front = data next 20 element 0
  • 14. 14 The add method, 1st try // Adds the given value to the end of the list. public void add(int value) { if (front == null) { // adding to an empty list front = new ListNode(value); } else { // adding to the end of an existing list ... } }
  • 15. 15 Adding to non-empty list • Before adding value 20 to end of list: • After: front = data next 42 data next -3 front = data next 42 data next -3 data next 20 element 0 element 1 element 2 element 0 element 1
  • 16. 16 Don't fall off the edge! • To add/remove from a list, you must modify the next reference of the node before the place you want to change. – Where should current be pointing, to add 20 at the end? – What loop test will stop us at this place in the list? front = data next 42 data next -3 element 0 element 1
  • 17. 17 The add method // Adds the given value to the end of the list. public void add(int value) { if (front == null) { // adding to an empty list front = new ListNode(value); } else { // adding to the end of an existing list ListNode current = front; while (current.next != null) { current = current.next; } current.next = new ListNode(value); } }
  • 18. 18 Implementing get // Returns value in list at given index. public int get(int index) { ... } – Exercise: Implement the get method. front = data next 42 data next -3 data next 17 element 0 element 1 element 2
  • 19. 19 The get method // Returns value in list at given index. // Precondition: 0 <= index < size() public int get(int index) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current.data; }
  • 20. 20 Implementing add (2) // Inserts the given value at the given index. public void add(int index, int value) { ... } – Exercise: Implement the two-parameter add method. front = data next 42 data next -3 data next 17 element 0 element 1 element 2
  • 21. 21 The add method (2) // Inserts the given value at the given index. // Precondition: 0 <= index <= size() public void add(int index, int value) { if (index == 0) { // adding to an empty list front = new ListNode(value, front); } else { // inserting into an existing list ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = new ListNode(value, current.next); } }