SlideShare a Scribd company logo
CSE 143
Lecture 6
Linked Lists
slides created by Ethan Apter
http://www.cs.washington.edu/143/
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
0 1 2 3 4 5 6 7 8
6 2 5 3 7 1 4 -9 -8
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:
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?
5
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
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 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.
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.data front.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 3 7 0 null
front.next.data front.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 3 7 12 null
front.next.next.data front.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 3 7 12
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 2 4 3
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 3 9
• ...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

More Related Content

Similar to Doubly Circular Linked List – Both next and previous pointers form a circular connection. (20)

PPTX
Lecture ............ 3 - Linked Lists.pptx
SumeetRathi5
 
PDF
Data Structure Lecture 3 Linked Lists.pdf
donotreply20
 
PPTX
Linked list
MahammadAdil
 
PDF
2a-Linked Listsxcxxcxxcxcxcxcxcxcxcxcxx.pdf
NGUYNTHNHQUC2
 
PPT
lecture 02.2.ppt
NathanielAdika
 
PDF
linked lists in data structures
DurgaDeviCbit
 
PPTX
data structures and applications power p
MeghaKulkarni27
 
PDF
AVL TREE.hhfgdgdgdgtyuhiiiouiiiuuggggpdf
akashtest67
 
PPTX
DS_LinkedList.pptx
msohail37
 
PPTX
linked list_MODULE 3.pptx ppt on the linked list
AnuragKumar682871
 
PPTX
RPT_03_A_Linked List presentation for FE
AshishFamt
 
PDF
DS Module 03.pdf
SonaPathak5
 
PPTX
unit 1.pptx
ssuser7922b8
 
PPTX
Linked list
Arbind Mandal
 
PPT
Unit ii(dsc++)
Durga Devi
 
PDF
computer notes - Linked list
ecomputernotes
 
PPTX
Lists, Stacks, and Queues: Abstract Data Types
Hasan Dwi Cahyono
 
PPTX
1.Introduction to Data Structures and Algorithms.pptx
BlueSwede
 
PPT
Data Structures 3
Dr.Umadevi V
 
Lecture ............ 3 - Linked Lists.pptx
SumeetRathi5
 
Data Structure Lecture 3 Linked Lists.pdf
donotreply20
 
Linked list
MahammadAdil
 
2a-Linked Listsxcxxcxxcxcxcxcxcxcxcxcxx.pdf
NGUYNTHNHQUC2
 
lecture 02.2.ppt
NathanielAdika
 
linked lists in data structures
DurgaDeviCbit
 
data structures and applications power p
MeghaKulkarni27
 
AVL TREE.hhfgdgdgdgtyuhiiiouiiiuuggggpdf
akashtest67
 
DS_LinkedList.pptx
msohail37
 
linked list_MODULE 3.pptx ppt on the linked list
AnuragKumar682871
 
RPT_03_A_Linked List presentation for FE
AshishFamt
 
DS Module 03.pdf
SonaPathak5
 
unit 1.pptx
ssuser7922b8
 
Linked list
Arbind Mandal
 
Unit ii(dsc++)
Durga Devi
 
computer notes - Linked list
ecomputernotes
 
Lists, Stacks, and Queues: Abstract Data Types
Hasan Dwi Cahyono
 
1.Introduction to Data Structures and Algorithms.pptx
BlueSwede
 
Data Structures 3
Dr.Umadevi V
 

More from Bhagya775232 (6)

PPTX
Energy Balance and Nutrient Calculation.pptx
Bhagya775232
 
PPTX
Nanotechnology is the field of science and engineering that deals with manipu...
Bhagya775232
 
PPTX
Bubble Sort is one of the most straightforward sorting algorithms
Bhagya775232
 
PPTX
A contactless fingerprint scanner is an advanced Biometric.pptx
Bhagya775232
 
PPT
This PPT offers insights into data structures in Python, including linked lis...
Bhagya775232
 
PPT
Singly Circular Linked List – Last node points to the first node.
Bhagya775232
 
Energy Balance and Nutrient Calculation.pptx
Bhagya775232
 
Nanotechnology is the field of science and engineering that deals with manipu...
Bhagya775232
 
Bubble Sort is one of the most straightforward sorting algorithms
Bhagya775232
 
A contactless fingerprint scanner is an advanced Biometric.pptx
Bhagya775232
 
This PPT offers insights into data structures in Python, including linked lis...
Bhagya775232
 
Singly Circular Linked List – Last node points to the first node.
Bhagya775232
 
Ad

Recently uploaded (20)

PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PDF
Digital water marking system project report
Kamal Acharya
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Design Thinking basics for Engineers.pdf
CMR University
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Distribution reservoir and service storage pptx
dhanashree78
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Digital water marking system project report
Kamal Acharya
 
Ad

Doubly Circular Linked List – Both next and previous pointers form a circular connection.

  • 1. CSE 143 Lecture 6 Linked Lists slides created by Ethan Apter http://www.cs.washington.edu/143/
  • 2. 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 0 1 2 3 4 5 6 7 8 6 2 5 3 7 1 4 -9 -8
  • 3. 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. 4 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?
  • 5. 5 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
  • 6. 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. 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. 8 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.
  • 9. 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. 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. 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. 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.data front.next
  • 13. 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 3 7 0 null front.next.data front.next.next
  • 14. 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 3 7 12 null front.next.next.data front.next.next.next
  • 15. 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 3 7 12
  • 16. 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. 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. 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. 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. 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. 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 2 4 3 data next q 9
  • 22. 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. 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. 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 3 9 • ...we permanently lose the ListNode containing the 3
  • 25. 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. 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. 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. 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. 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. 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. 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. 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