Lecture 05-06 - Linked List
Lecture 05-06 - Linked List
International University
School of Computer Science and Engineering
T UE SD AY , 2 2 O CT O BE R 2 0 24 2
Content
T UE SD AY , 2 2 O CT O BE R 2 0 24 3
Array review
T UE SD AY , 2 2 O CT O BE R 2 0 24 4
Introduction to linked list
T UE SD AY , 2 2 O CT O BE R 2 0 24 5
List Data Structures
T UE SD AY , 2 2 O CT O BE R 2 0 24 6
Operations
T UE SD AY , 2 2 O CT O BE R 2 0 24 7
Operations
T UE SD AY , 2 2 O CT O BE R 2 0 24 8
Self-Referential Structures
T UE SD AY , 2 2 O CT O BE R 2 0 24 9
Self-Referential Structures
Employee
String name;
int age;
DataNode
DataNode
Employee info;
Employee info;
DataNode left;
DataNode next;
DataNode right;
Self-Referential Structures
T UE SD AY , 2 2 O CT O BE R 2 0 24 10
Linked Lists
T UE SD AY , 2 2 O CT O BE R 2 0 24 11
Singly Linked Lists
• A singly linked list is a list whose node includes two data fields: info
and next. The info field is used to store information, and this is
important to the user. The next field is used to link to its successor in
this sequence
• The following image depicts a simple integer linked list.
head tail
T UE SD AY , 2 2 O CT O BE R 2 0 24 13
Link
• A link contains
• Data
• A reference to next link (‘Next’)
T UE SD AY , 2 2 O CT O BE R 2 0 24 14
Link class
T UE SD AY , 2 2 O CT O BE R 2 0 24 15
Relationship, not Position
T UE SD AY , 2 2 O CT O BE R 2 0 24 16
Action on simple linked list
• Insertion
• Deletion
• Searching
T UE SD AY , 2 2 O CT O BE R 2 0 24 17
How would you do that - Insertion
• InsertFirst?
• InsertLast?
T UE SD AY , 2 2 O CT O BE R 2 0 24 18
In Java
T UE SD AY , 2 2 O CT O BE R 2 0 24 19
How would you do that - Deletion
• Delete first?
• Delete last?
T UE SD AY , 2 2 O CT O BE R 2 0 24 20
In Java
T UE SD AY , 2 2 O CT O BE R 2 0 24 21
How would you do that - Deletion
T UE SD AY , 2 2 O CT O BE R 2 0 24 22
How would you do that - Display
T UE SD AY , 2 2 O CT O BE R 2 0 24 23
Practice
• LinkList2App.java
• Complete the functions:
• insertFirst
• find
• delete
T UE SD AY , 2 2 O CT O BE R 2 0 24 24
Circular List
T UE SD AY , 2 2 O CT O BE R 2 0 24 25
Circular Lists - 1
26
• A circular list is when nodes form a ring: The list is finite, and each
node has a successor
Inserting nodes at the front of a circular singly linked list (a) and at its end (b)
Data Structures and Algorithms in Java 2 7/2 3
Circular List application
28
• 1. Round-Robin Scheduling
• One of the most important roles of an operating system is in managing the
many processes that are currently active on a computer, including the
scheduling of those processes on one or more central processing units (CPUs).
• To support the responsiveness of an arbitrary number of concurrent processes,
most operating systems allow processes to effectively share use of the CPUs,
using some form of an algorithm known as round-robin scheduling.
• A process is given a short turn to execute, known as a time slice, but it is
interrupted when the slice ends, even if its job is not yet complete. Each active
process is given its own time slice, taking turns in a cyclic order.
T UE SD AY , 2 2 O CT O BE R 2 0 24 30
Double-Ended Lists
T UE SD AY , 2 2 O CT O BE R 2 0 24 31
Directly insert to last position
T UE SD AY , 2 2 O CT O BE R 2 0 24 32
How about the deletion of last item
T UE SD AY , 2 2 O CT O BE R 2 0 24 33
Simple linked list efficiency
• Insertion and deletion at beginning of the list are very fast: O(1)
• Finding, deleting, or insert item: O(n)
• → is it the same as array (O(n) also)?
• In comparison with array
• Don’t have to shift items to delete or insert.
• Uses exactly as much memory as it needs
• Size can be changed
T UE SD AY , 2 2 O CT O BE R 2 0 24 34
Abstract Data Type (ADT)
T UE SD AY , 2 2 O CT O BE R 2 0 24 35
ADT
T UE SD AY , 2 2 O CT O BE R 2 0 24 36
Implement Stack & Queue
T UE SD AY , 2 2 O CT O BE R 2 0 24 37
Data Types and Abstraction
T UE SD AY , 2 2 O CT O BE R 2 0 24 38
Data Types and Abstraction
T UE SD AY , 2 2 O CT O BE R 2 0 24 39
Data Types and Abstraction and Interface
T UE SD AY , 2 2 O CT O BE R 2 0 24 40
Interface in OOP
T UE SD AY , 2 2 O CT O BE R 2 0 24 41
ADTs as a Design Tool
T UE SD AY , 2 2 O CT O BE R 2 0 24 43
Sorted list
T UE SD AY , 2 2 O CT O BE R 2 0 24 44
How would you do that
• Operations
• Insert
• DeleteSmallest
• DeleteLargest
• Delete(key)
T UE SD AY , 2 2 O CT O BE R 2 0 24 45
Insert data to sorted list
T UE SD AY , 2 2 O CT O BE R 2 0 24 46
Efficiency of sorted list
T UE SD AY , 2 2 O CT O BE R 2 0 24 47
Application
T UE SD AY , 2 2 O CT O BE R 2 0 24 48
Some code
T UE SD AY , 2 2 O CT O BE R 2 0 24 49
Doubly linked lists
T UE SD AY , 2 2 O CT O BE R 2 0 24 50
Introduction
T UE SD AY , 2 2 O CT O BE R 2 0 24 51
Doubly linked list
T UE SD AY , 2 2 O CT O BE R 2 0 24 52
Doubly linked list
T UE SD AY , 2 2 O CT O BE R 2 0 24 53
Operations
• Insert • Delete
• InsertFirst • DeleteFirst
• InsertLast • DeleteLast
• InsertAfter • Delete(key)
• InsertBefore
• Display:
• DisplayForward
• DisplayBackward
T UE SD AY , 2 2 O CT O BE R 2 0 24 54
InsertFirst
T UE SD AY , 2 2 O CT O BE R 2 0 24 55
InsertFirst
T UE SD AY , 2 2 O CT O BE R 2 0 24 56
InsertLast
57
T UE SD AY , 2 2 O CT O BE R 2 0 24 58
Delete an item
T UE SD AY , 2 2 O CT O BE R 2 0 24 59
Delete an item
• DeleteFirst?
• DeleteLast?
T UE SD AY , 2 2 O CT O BE R 2 0 24 60
Application
• Implement deque
• Queue that can insert and delete at either end
• Support bi-direction traversing
T UE SD AY , 2 2 O CT O BE R 2 0 24 61
Iterators
T UE SD AY , 2 2 O CT O BE R 2 0 24 62
Lists in java.util - LinkedList class
boolean add(E o) Appends the specified element to the end of this list.
void addFirst(E o) Inserts the given element at the beginning of this list.
void addLast(E o) Appends the given element to the end of this list.
void clear() Removes all of the elements from this list.
E get(int index) Returns the element at the specified position in this list.
E getFirst() Returns the first element in this list.
E getLast() Returns the last element in this list.
E remove(int index) Removes the element at the specified position in
this list.
E removeFirst()Removes and returns the first element from this list.
E removeLast() Removes and returns the last element from this list.
int size() Returns the number of elements in this list.
Object[] toArray() Returns an array containing all of the elements in this list in the
correct order.
6 3/2 3
Lists in java.util
LinkedList class example
import java.util.*; class Main
class Node { {
String name; public static void main(String [] args)
int age; {
Node() {} LinkedList t = new LinkedList();
6 4/2 3
Lists in java.util - ArrayList class
boolean add(E o) Appends the specified element to the end of this list.
void add(int index, E o) Inserts the given element at the specified pos.
void clear() Removes all of the elements from this list.
E get(int index) Returns the element at the specified position in this list.
E remove(int index) Removes the element at the specified position in this
list.
int size() Returns the number of elements in this list.
void ensureCapacity(int minCapacity) Increases the capacity of this ArrayList
instance, if necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
void trimToSize() Trims the capacity of this ArrayList instance to be the list's
current size.
Object[] toArray() Returns an array containing all of the elements in this list in the
correct order.
6 5/2 3
Summary
T UE SD AY , 2 2 O CT O BE R 2 0 24 66
Summary
T UE SD AY , 2 2 O CT O BE R 2 0 24 68
Summary
• In a sorted linked list, the links are arranged in order of ascending (or
sometimes descending) key value.
• Insertion in a sorted list takes O(N) time because the correct insertion
point must be found. Deletion of the smallest link takes O(1) time.
• In a doubly linked list, each link contains a reference to the previous
link as well as the next link.
• A doubly linked list permits backward traversal and deletion from the
end of the list.
T UE SD AY , 2 2 O CT O BE R 2 0 24 69
Practice
• DoublyLinkedApp.java
• Complete the functions:
• insertFirst
• insertLast
• deleteFirst
• deleteLast
• deleteKey
• displayForward
• displayBackward
T UE SD AY , 2 2 O CT O BE R 2 0 24 70
Vietnam National University of HCMC
International University
School of Computer Science and Engineering
THANK YOU