0% found this document useful (0 votes)
15 views49 pages

Slot02 03 LinkedLists

aasdwqd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views49 pages

Slot02 03 LinkedLists

aasdwqd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Slot 2&3

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

5- Demo 1: Doubly Linked List  Your work


6- Demo 2: Sorted Doubly Linked List  Your work
7- When linked Lists are used?
8- Managing groups using the Java API

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

a No empty cell is allowed!!! Empty cells


MAXN=15
n = 10

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:

Operation Complexity Expectation


Memory allocation Allocated memory size To optimize memory use,
can be redundant/ lack memory for storing
elements will be allocated
when needed.
Add x to the end O(1)  Good
Add x to the position pos O(n) < O(n)
Search x O(n) < O(n) Linked Data
Structures are
Remove element at the O(n) < O(n) introduced.
position pos

Question: Why linked data structures are needed?


 Answer.
Linked Lists 7
3: Introduction to Linked Data Structures
 Linked Data structure: A data organization in which each
node includes itself data and links to others.
 An example depict memory figure of a linked data structure:

James Linda Jane Tom Jack Paul


42 25 10 22 22 8
Manager Officer student student Designer Kid
1000 3000 8000 200 10000 0 (NULL)
400 1000 3000 8000 200 10000
Memory
address Person
Element Info of an element itself
(name, age, role)
structure
next Reference to the next element

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

 Non linear linked data


structure: Links establish
multiple lines to access
elements  Trees

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.

700 1000 800 400 600


obj
head: 700 obj1 obj2 obj3 obj5 Singly Linked
4
tail: 600 1000 800 400 600 null list (SLL)

700 1000 800 400 600


obj Circularly
head: 700 obj1 obj2 obj3 obj5
4 Singly Linked
tail: 600 1000 800 400 600 700
list (CSLL)

Object data Item head


Item List Item tail
Item next

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

700 1000 800 400 600


head: 700
Item
obj1 obj2 obj3 obj4 obj5
tail: 600 1000 800 400 600 null DLL Object data
null 700 1000 800 400 Item next
Item previous
head: 700 obj1 obj2 obj3 obj4 obj5
tail: 600 CDLL
1000 800 400 600 700
600 700 1000 800 400 List
Item head
Item tail

Linked Lists 12
LLs…: Representing Item/List
Representing an item(element):
Representing a linked list:

Item in DLL Linked List


Item in SLL
Object data Object data Item head
Item next Item next Item tail
Item previous
 A list can be managed using a reference to the beginning item
only but we should use both references to the head and the tail of
the list. The reference to the tail helps the add-last operation a
new item to the end of a list with the best performance (O(1)). If
not, when we want to add a new item to the end of a list, we need
to move from the beginning to the end of the list (O(n)).

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)

Circular (CLL) and Non - circular LL (LL):


View Evaluation
Identifying In LL, the last item of a path has the reference is NULL
the last Ex: for (t=head; t!=null; t= t.next)
item In CLL, the last item of a path has the reference is the path’s
beginning
Ex: for (t=head; t.next!=head; t= t.next)
 CLL cause more complex when it is implemented.

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)

x obj1 obj2 obj3 obj4


null1000 800 400 600 null
p = 700 1000 800 400 600

head: 1000  700


tail: 600

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
}

obj1 obj2 obj3 obj4 x


800 400 600 null  700 null
1000 800 400 600 p = 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;
}

obj1 obj2 obj3 obj4


800 400 600  700 null
1000 800 ref= 400 600

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

List traversal is the process of visiting each node in the list


exactly one time  O(number of nodes)
Sample code:
for (t = head; t!=null; t= t.next){
Process t;
}
We can implement a list traversal in an object, called as
Iterator. This technique will be introduced in the
assignment.

Linked Lists 21
SLL Operations: Sorting

Sorting is not a main operation on a LL.


We can modify a simple sort algorithm on an array to sort a LL.
 Sample code:

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.

Operation Arrays L- lists Sorted L-lists


Add new element to the beginning O(n) O(1) Add to the right
Add new element to the end O(1) O(1) position  Average
cost: n/2
Add new element to a specific position O(n) O(1)
Linear Searching O(n) O(n) Average cost: n/2
Remove an element at a specific position O(n) O(1) O(1)
Sort O(n2) O(n2) The list is sorted

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).

Package Structure Demonstration 1:


Class for an element in a doubly linked list

Interface for traversing elements in the list

Class for a doubly linked list

Program will test the MyLinkedList class

Class for a sorted doubly linked list

Program will test the MySortedLL class


Demonstration 2:

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

pBefore ele pAfter

Linked Lists 33
Demo 1: Class MyLinkedList…

The class Traverser takes duty for


traversing a MyLinkedList object
only. So, it is implemented as a
inner class of the MyLinkedList
class.

The method iterator() of the


MyLinkedList class support outside
a mean for traversing elements of a
DLL list.

Supporting methods to add more


than one elements.
Linked Lists 34
Demo 1: class MyLinkedListTest

Java program for testing


implemented algorithms.

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)

Average complexity of the


add/search operation

Linked Lists 38
Demo 2- Class
MySortedLL…

Linked Lists 39
Demo 2- Class MySortedLL…

This inner class is


the same as those in
the MyLinkedList
class

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

0 0 0 1 1 0 0 0 head1 Col, val Col, val Col, val

1 1 0 0 0 0 0 0 head2 Val, Col


2 head3 Col, val Col, val
3
headi Col, val Col, val Col, val

headn Col, val Col, val Col, val Col, val
9999
Sorted lists based on column

N (9999) rows  N lists

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.

Data Class Characteristics When is it used?


structure
Dynamic Vector Accessing an element using its index. Data group for single
Array Thread safe is supported or multi-thread
All basic operations are implemented. applications

Dynamic ArrayList Accessing an element using its index. Single -thread


Array Thread safe (synchronized technique) application
is NOT supported
All basic operations are implemented.

Double LinkedLi Thread safe (synchronized technique) Single -thread


linked list st is NOT supported. All basic operations application
are implemented.

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)

A sparse table refers to a table that is populated sparsely by


data and most of its cells are empty.
Linked lists allow easy insertion and deletion of
information because such operations have a local impact
on the list.
The advantage of arrays over linked lists is that they allow
random accessing.
Random access: given address, data at that address will
be accessed  very fast.

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

You might also like