Slot02 03 LinkedLists
Slot02 03 LinkedLists
Linked Lists
Linked Lists 1
Learning Outcomes
LO1: Describe the list data structure and its’ different way of
implementations. Implement the singly linked list.
LO1.1 Demonstrate the list data structure with basic operations: insert,
remove, edit, search, sort,...
LO1.2 Explain two ways of implementing a list: using arrays and using linked
lists.
LO1.3 Write programs to implement the singly linked list with basic
operations: add (last, first, before, after, at position), search, edit, remove,
reverse, sort... )
LO1.4 Understand the doubly linked-list data structure with basic operations.
LO1.5 Write code snippets which manipulate operations on doubly linked list,
and use diagrams to illustrate the effect.
LO1.6 Describe the circularly linked-list data structure with some basic
operations like add, remove.
LO1.7 Explain why the Java code library provides the ArrayList and
LinkedList classes.
Linked Lists 2
Contents
1- Basic operations on a group
2- Drawbacks of arrays
3- Introduction to Linked Data Structures
4- Linked Lists (LLs):
Definition
Common used LLs: Singly / Doubly/ Circular Linked Lists
Representing an item/ a LL.
Comparing
Common operations on SLLs
Linked Lists 3
1- Basic operations on a group
Group is a collection of data objects which usually have the same
structure. In a group, there may exist relations between elements
such as ordering relations or connections between elements.
Relations must be described in data.
- Physically, there are some ways to store a groups (list/ tree/…).
So, a group is an abstract data type and they are declared as
interfaces in languages (interface Collection, List,… in Java).
Basic operations on group include:
Adding new element
Searching an element
Removing an element
Updating an element Search + re-assign new value
Traversing all elements
Sorting elements using a pre-defined order.
Linked Lists 4
2: Drawbacks of Arrays
Array is a group of elements (nodes)
which belong to the same data type
They are stored in contiguous memory cells
Each element can be accessed using its positional index.
Array is the simplest data structure for a group. Positional
indices describe an ordering relations between elements.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
9 3 1 6 7 8 8 5 6 4
Linked Lists 5
Memory for elements
must be allocated in
advance.
Drawbacks
of Arrays…
Linked Lists 6
Drawbacks of Arrays …
Summary of Arrays and Expectations:
Linked Lists 8
3: Intro. to Linked Data Structures
Types of Linked Data Structures:
Linear data structure/ List: Structure having only one line to access
all elements Array, linked lists.
Linear Linked data structures/ Linked List: Links establish only one
line to access elements linked lists
obj
obj1 obj2 obj3 obj5
4
1000 800 400 600 null
Linked Lists 9
4: Linked Lists (LLs)
Linked list: A group of nodes, each node contains its data and links to
others. Links form a linear path to access elements.
A link describes a previous-next relation between two elements.
Linked list is the simplest linked data structure.
Linked Lists 10
LLs…
Linked list: A group of nodes, each node contains its data and
links to others. Links form a linear path to access elements.
A link describes a previous-next relation between two elements.
Linked list is the simplest linked data structure.
Linear accessing path may form a circle. It means that the
ending element links to the beginning.
Common used linked lists:
Singly linked lists (SLL)
Circular linked lists (CSLL)
Doubly linked lists (DLL)
Circular doubly linked lists (CDLL)
Linked Lists 11
LLs: Common used LLs
700 1000 800 400 600
head: 700 obj1 obj2 obj3 obj4 obj5 Item
tail: 600 SLL
1000 800 400 600 null Object data
Item next
head: 700 obj1 obj2 obj3 obj4 obj5 CSLL
tail: 600 1000 800 400 600 700
Linked Lists 12
LLs…: Representing Item/List
Representing an item(element):
Representing a linked list:
Linked Lists 13
LLs…: Comparing
View Evaluation
SLL
Memory In SLL, only one reference is added to each item
and In DLL, two references are added to each item
DLL: Memory cost of DLL is greater than SLL.
Flexibility SLL has only one path (one accessing direction)
Ex: for (t=head; t!=null; t= t.next)
DLL has two paths DLL is more flexible than SLL.
Ex: for (t=head; t!=null; t= t.next)
Ex: for (t=tail; t!=null; t= t.previous)
Linked Lists 14
LLs…: Operations
Operations on DLL and CLL:
They are similar to operations on SLL
Pay attentions:
Update the reference previous in DLL
In CLLs, the tail item links to the head item.
So, only basic operations on SLL are introduced including:
Add the data x to the beginning / the end of a SLL
Add data x to the position after the reference ref of a SLL
Searching the first existence of the data x in a SLL
Remove the first existence of the data x in a SLL
Traversing items in a list (this operation will be introduced in the
assignment)
Sorting a SLL
Linked Lists 15
SLL Operations: Adding
Add the data x to the beginning of a SLL
Item p = new Item(x); x
if (head==null) head=tail = p; null
p = 700
else {
p.next = head; head: null 700
head = p; Complexity: tail: null 700
} O(1)
Linked Lists 16
SLL Operations: Adding…
Add the data x to the end of a SLL
Item p = new Item(x); x
if (head==null) head=tail = p; null
else { p = 700
tail.next = p; Complexity: head: null 700
tail = p; O(1) tail: null 700
}
head: 1000
tail: 600 700
Linked Lists 17
SLL Operations: Adding…
Add the data x to the position after the reference ref of a SLL
if (ref==tail) Add x to the end of the list;
else {
Item p = new Item(x); Complexity:
p.next = ref.next; O(1)
ref.next = p;
}
head: 1000
x
tail: 600
null 600
p = 700
Linked Lists 18
SLL Operations: Searching
Search the first existence of data x in the SLL
If (head==null) return null; Linear search O(n)
for (Item t = head; t!=null; t = t.next)
Search x in an array:
if (t.data==x) return t; for (i=0; i<n; i++)
return null; if (a[i]==x) return i;
return -1;
x= 8
t= 1000 800 400 600
head: 1000 7 6 5 8 30
800 400 600 700 null
tail: 700
1000 800 400 600 700
Linked Lists 19
SLL Operations: Removing
Remove the first existence of data x in the SLL
If (head==null) return null; x= 8
Item tAfter = null, tDel = head;
tAfter= null 1000 800 400
// Determine the reference which can be removed tDel= 1000 800 400 600
While (tDel!=null && tDel.data!=x) {
1000 800 400 600 700
tAfter=tDel;
Linear search
tDel = tDel.next; O(n) 7 6 5 8 30
} 800 400 600 700 null
// If removed data existed 700
If (tDel != null) {
if (tDel==head) { // remove head
head = head.next; head: 1000
if (head==null) tail= null; // if the list is empty tail: 700
}
else {
tAfter.next = tDel.next;
tDel.next=null; // CUT tDel from the list
if (tDel==tail) tail= tAfter; // update tail
}
} Linked Lists 20
SLL Operations: Traversing
Linked Lists 21
SLL Operations: Sorting
Linked Lists 22
SLL Operations: Reversing
head: 1000 obj1 obj2 obj3 obj4
tail: 600 800 400 600 null
1000 800 400 600
P3=null P2 P1
obj1 obj2 obj3 obj4
800 0 400 600 null
1000 800 400 600
P3 P2 P1
obj1 obj2 obj3 obj4
0 1000 600 null
1000 800 400 600
P3 P2 P1
obj1 obj2 obj3 obj4
0 1000 800 null
1000 800 400 600
P3 P2 P1
head: 600 obj1 obj2 obj3 obj4
tail: 1000 0 1000 800 400
1000 800 400 600
Linked Lists 23
LLs…: Operations- Summary
Complexities: Linked list vs Array
In LLs, Add and remove operations are more efficient.
Search and sort operations are not improved because linear search is still used.
How to improve?
Sorted LL can improve search operations but add operations decrease their
performance to maintain order of elements.
Given sorted list: 1 3 5 7 9 11 13 15
Add 10 to the list: t t t t t. Add 10 after the reference t.
Linked Lists 24
End of the slot 2. Paused
Linked Lists 25
5: Demonstration 1: Doubly Linked List
- Doubly Linked lists have two linear paths for accessing elements. So, they are
more flexible than singly linked lists.
- Computers today have a great memory DLL is chosen to be implemented.
- In the java.util package of Java, DLL is chosen to be implemented also (the
LinkedList class).
Linked Lists 26
Demo. 1: Class LL_Element
Item in a DLL
Object data
Item next
Item previous
Linked Lists 27
Demo 1: Interface MyIterator
- At a time, only one element is processed. Before the data of an element
is accessed (the method next()), we need to test whether an element
remains in the list or not(the method hasNext()).
-In libraries of programming languages such as Java, C#, Python,…, an
interface, named Iterator is declared for traversing elements of a
collection/group.
Linked Lists 28
Demo 1: Class MyLinkedList
Linked List
Item head
Item tail
Linked Lists 29
Demo 1: Class MyLinkedList…
newEle head
data data
next next
previous previous
tail newEle
data data
next next
previous previous
Linked Lists 30
Demo 1: Class MyLinkedList…
p pAfter
newEle
pBefore p
newEle
Linked Lists 31
Demo 1: Class MyLinkedList…
head newHead
Linked Lists 32
Demo 1: Class MyLinkedList…
newTail tail
Linked Lists 33
Demo 1: Class MyLinkedList…
Linked Lists 35
Demo 1: class MyLinkedListTest
Linked Lists 36
6: Demo 2-class MySortedLL
Linked Lists 37
Demo 2- class
MySortedLL…
Add to Number of
position steps
1 1
2 2
3 3
4 4
… …
n n
Sum n(n+1)/2
Average (n(n+1)/
2)/n
O(n/2)
Linked Lists 38
Demo 2- Class
MySortedLL…
Linked Lists 39
Demo 2- Class MySortedLL…
Linked Lists 40
Demo 2- Class MySortedLL_Test
Linked Lists 41
7- Where a linked list should be used?
(1) From advantages of LLs, a LL should be used when:
• Number of items is not known in advance Memory can not
be allocated in advance.
• Add and remove operations are frequently used.
(2) LL is suitable to store a sparse table/ sparse matrix
A sparse table is a large-size 2D array in which number of
valuable values is very small
0 1 2 3 4 … … 9999
0 0 0 1 1 0 0 0 0 1s are valuable but 0s.
Almost of values in the
1 1 0 0 0 0 0 1 0
table is 0.
2 0s need not to be
3 stored.
Linked list is
…
suitable.
9999
Linked Lists 42
Where a linked list should be used?...
Using LLs instead of a sparse matrix:
0 1 2 3 4 … 9999
head0 Col, val Col, val Col, val Col, val
Linked Lists 43
8: Managing groups using the java API
In the java.util package, basic classes for a collection:
Dynamic arrays: Initial, 10 elements are allocated, when arrays are full, user program will be paused
and memory manager allocates double –size memory (10 20, 20 40, …). Steps: (1) Allocating
new double-size memory block (2) Copy old array to new array (3) De-allocate old array.
Linked Lists 44
Summary
A linked structure is a collection of nodes storing data and links
to other nodes.
A linked list is a data structure composed of nodes, each node
holding some information and a reference to another node in the
list.
In a singly linked list, each node has a link only to its successor in
this sequence.
A list is called as circular list when nodes form a ring.
LL Advantages: Insert, remove operations are performed
efficiently.
LL Disadvantages: Search operation is not performed effectively
because of sequential scanning.
Linked Lists 45
Summary (continued)
Linked Lists 46
Summary
LO1.1 Demonstrate the list data structure with basic operations:
insert, remove, edit, search, sort,...
LO1.2 Explain two ways of implementing a list: using arrays and
using linked lists.
LO1.3 Write programs to implement the singly linked list with
basic operations: add (last, first, before, after, at position),
search, edit, remove, reverse, sort... )
LO1.4 Understand the doubly linked-list data structure with basic
operations.
LO1.5 Write code snippets which manipulate operations on
doubly linked list, and use diagrams to illustrate the effect.
LO1.6 Describe the circularly linked-list data structure with some
basic operations like add, remove.
LO1.7 Explain why the Java code library provides the ArrayList
and LinkedList classes
Linked Lists 47
Ôn tập
Viết vào vở theo mẫu sau:
1- Cấu trúc dữ liệu liên kết là gì?
Là một nhóm phần tử trong đó mỗi phân tử có liên kết với các
phần tử khác. Slide tham khảo: 8
2-Tại sao lại cần cấu trúc liên kết?
3- Cấu trúc liên kết tuyến tính là gì?
4- Cấu trúc liên kết phi tuyến là gì?
5- Danh sách liên kết là gì?
6- Danh sách liên kết vòng là gì?
7- Làm sao mô tả một nút trong danh sách liên kết. Cho một thí dụ.
8- Tại sao một DSLK nên được quản lý bằng 2 tham chiếu head và tail?
9- Hãy so sánh độ phức tạp thời gian giữa DSLK và mảng trên các tác
vụ thêm/ tìm/ xoá phần tử.
10- Để cải tiến hiệu quả của tác vụ tìm kiếm trên DSLK, DSLK nên
được tổ chức như thế nào và hiệu quả ra sao?
11- Khi nào DSLK được khuyên dùng?
Linked Lists 48
Ôn tập
12- Bảng thưa là gì? Mô tả bảng thưa như thế nào để tiết kiệm bộ nhớ.
13- Lớp java.util.LinkedList thuộc loại DSLK nào? Có hỗ trợ kỹ thuật an
toàn về luồng hay không?
Given a linked list of positive integers and it is managed using head and
tail references: 5 __ 7 __ 1 __ 6 __ 9 __ 2 __ 4 __ 10 __ 8 __ 3
14- If following codes perform, the value in S is ___
S=10;
for (t=head; t.nexr!=null; t=t.next)
if (t.data%2!=0) S += t.data;
15- If following codes perform, the value in S is ___
S=0;
t = tail;
while (t!=null) {
if ((t.data & (t.data-1))==0) S += t.data;
t = t.previous;
}
Linked Lists 49