1. Data Structures and
Algorithms
Week 2: Linked Lists
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
2. Week 2
• Linked Lists
• Doubly Linked Lists
• Circular Linked Lists
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
3. 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
3
4. 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
4
5. 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
6. A current reference
• Don't change list. Make another variable, and
change it.
• 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
7. 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
8. Linked List vs. Array
• Print list values:
ListNode list= ...;
ListNode current = list;
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++;
}
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++; current = current.next;
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
9. 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
10. LinkedIntList class v1
public class LinkedIntList {
private ListNode front;
public LinkedIntList() {
front = null;
}
methods go here
}
front =
LinkedIntList
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
11. Basic Structure
import java.io.*;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
}
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
12
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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
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
...
}
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
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);
}
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
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;
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
20. Implementing add
// 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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
21. The add method
// 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);
}
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
21
22. Implementing remove
// Removes the value at the given
index.
public void remove(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
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
22
23. The remove method
LinkedList<String> list = new LinkedList<String>();
// Use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Output the list
System.out.println("LinkedList:" + list);
// Remove the head using remove()
list.remove("Geeks");
list.remove("20");
// Print the final list
System.out.println("Final LinkedList:" + list);
Output
LinkedList:[Geeks, for, Geeks, 10, 20]
Final LinkedList:[for, Geeks, 10]
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
23
24. Sample code
Implementation using Java
https://beginnersbook.com/2013/12/linkedlist-in-
java-with-example/
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
24
25. Week 2
• Linked Lists
• Doubly Linked Lists
• Circular Linked Lists
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
25
26. Doubly Linked List
Recall that the deletion of an element at the tail is not easy because we
have to find the node before the tail (the last node) by link hopping.
head
next
element
next nextnext
element element element
Baltimore Rome Seattle Toronto
tail
This problem can be easily solved by using the double linked list.
- Ed. 2 and 3.: Chapter 4
- Ed. 4: Chapter 3
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
26
27. A node in a doubly linked list: A compound object that
stores a reference to an element and two references, called
next and prev, to the next and previous nodes, respectively.
Reference to
next node
Reference to an
element
next
Element
Node
Reference to
previous node
prev
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
27
28. For convenience, a doubly linked list has a header node and a
trailer node. They are also called sentinel nodes, indicating
both the ends of a list.
header
Baltimore Rome Seattle
trailer
Difference from singly linked lists:
- each node contains two links.
- two extra nodes: header and trailer, which contain no
elements.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
28
29. Class DLNode
Here is an implementation of nodes for doubly
linked lists in Java:
public class DLNode {
private Object element;
private DLNode next, prev;
public DLNode() {
this( null, null, null );
}
public DLNode( Object e, DLNode p, DLNode n
) {
element = e;
next = n;
prev = p;
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
29
30. void setElement( Object newElem ) {
element = newElem;
}
void setNext( DLNode newNext ) {
next = newNext;
}
void setPrev( DLNode newPrev ) {
prev = newPrev;
}
Object getElement() {
return element;
}
DLNode getNext() {
return next;
}
DLNode getPrev() {
return prev;
}
}
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
30
31. Insertion of an Element at the
Head
Before the insertion:
header
Baltimore Rome Seattle
trailer
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
31
32. DLNode x = new DLNode();
x.setElement(new String(“Toronto”));
(x.element = new String(“Toronto”))
Have a new node:
header
Rome Seattle
trailer
Baltimore
Toronto
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
32
34. After the insertion:
header
Rome Seattle
trailer
BaltimoreToronto
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
34
35. Deleting an Element at the Tail
Before the deletion:
header
Rome Seattle
trailer
BaltimoreToronto
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
35
38. Week 2
• Linked Lists
• Doubly Linked Lists
• Circular Linked Lists
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
38
39. Circular Linked List
• Last node references the first node
• Every node has a successor
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
39
40. Circular Linked List
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
40
41. Dummy Head Nodes
• Dummy head node
• Always present, even when the linked list is empty
• Insertion and deletion algorithms initialize prev to
reference the dummy head node, rather than null
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
41
42. Applications
Applications of linked list in computer science
Implementation of stacks and queues
Implementation of graphs : Adjacency list representation of graphs is most popular which is uses linked
list to store adjacent vertices.
Dynamic memory allocation : We use linked list of free blocks.
Maintaining directory of names
Performing arithmetic operations on long integers
Manipulation of polynomials by storing constants in the node of linked list representing sparse matrices
Applications of linked list in real world
Image viewer – Previous and next images are linked, hence can be accessed by next and previous
button.
Previous and next page in web browser – We can access previous and next url searched in web
browser by pressing back and next button since, they are linked as linked list.
Music Player – Songs in music player are linked to previous and next song. you can play songs
either from starting or ending of the list.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics, Thai-
Nichi Institute of Technology
42
43. Next Week
Stack
Queue
Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
43