data structures and algorithms 2 - edx...3 目录页 ming zhang”data structures and algorithms “...
TRANSCRIPT
![Page 1: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/1.jpg)
https://courses.edx.org/courses/PekingX/04830050x/2T2014/
Ming Zhang“ Data Structures and Algorithms “
Data Structures and Algorithms(2)
Instructor: Ming Zhang
Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao
Higher Education Press, 2008.6 (the "Eleventh Five-Year" national planning textbook)
![Page 2: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/2.jpg)
2
目录页
Ming Zhang” Data Structures and Algorithms “
Chapter II Linear Lists
• 2.1 Linear list
• 2.2 Sequential list
• 2.3 Linked list
• 2.4 Comparison of sequential list and
linked list
Linear ListLinear List
a0 a1 a2 … … an-1
a0
a1
an-1
head
tail
{𝑎0, 𝑎1, … , 𝑎𝑛−1}
![Page 3: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/3.jpg)
3
目录页
Ming Zhang” Data Structures and Algorithms “
The Concepts of Linear List
• List for short, is a finite sequence of zero or
more elements, usually represented as k0,k
1,
…,kn-1(n ≥ 1)
– Entries: elements of linear list (can contain multiple
data items, records)
– Index: i is called the "Index" of entry ki
– Length of the list: the number of elements contained
in the list n
– Empty list: a linear list with the length of zero (n = 0)
• Features of Linear list:
– Flexible operations
– Dynamically changed length
2.1 Linear ListLinear List
Chapter II
![Page 4: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/4.jpg)
4
目录页
Ming Zhang” Data Structures and Algorithms “
Linear structure
• Tuple 𝐵 = 𝐾, 𝑅 𝐾 = {𝑎0, 𝑎1, … , 𝑎𝑛−1} 𝑅 = {𝑟}– There is one and only one starting point that has no
previous node and has only one successive node.
– There is one and only one ending point that has only
one previous node and has no successive node.
– The other nodes are called internal nodes that have
only one previous node and also have only one
successive node.
<ai,a
i+1> a
i is previous node of a
i+1 , and a
i+1is the successive
node of ai
2.1 Linear ListLinear List
Chapter II
![Page 5: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/5.jpg)
5
目录页
Ming Zhang” Data Structures and Algorithms “
Linear structure
• Features
Uniformity: Although the data elements of different
linear lists may be diverse, but the data elements of the
same linear list normally have the same data type and
length
Orderliness: each data element has its own position in
the list and their relative positions are linear
Features Linear List
Chapter II
![Page 6: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/6.jpg)
6
目录页
Ming Zhang” Data Structures and Algorithms “
• According to the complexity
- Simple: Linear lists, stacks, queues, hash tables
- Advanced: generalized lists,
multidimensional arrays, files etc.
• Divided by access ways
– Direct access type
– Sequential access type
– Contents Index type
(directory access)
Linear structure
ClassificationLinear List
Chapter II
Linear Structure
Direct access Sequential access Directory access
Vector Entry Dictionary HashTable
Sequential File Generalized Table
QueueStack
Linked List
![Page 7: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/7.jpg)
7
目录页
Ming Zhang” Data Structures and Algorithms “
Linear structure
• Classified by operation (see later)
-Linear List
• All entries are nodes of the same type of linear lists
• No need to limit the form of operation
• Divided into: the sequential list, linked list depending on the
difference of storage
-Stack (LIFO, Last In First Out)
Insert and delete operations are restricted to the same end of the list
-Queue (FIFO, First In First Out)
Insert at one end of the list, while delete at the other end
ClassificationLinear List
Chapter II
![Page 8: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/8.jpg)
8
目录页
Ming Zhang” Data Structures and Algorithms “
2.1 Linear List
•Three aspects
- Logical structure of the linear list
- Storage structure
- Operation of linear list
2.1 Linear ListLinear List
Chapter II
![Page 9: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/9.jpg)
9
目录页
Ming Zhang” Data Structures and Algorithms “
Logical structure of the linear list
• The main properties
– Length
– Head
– Tail
– Current position
2.1 Linear ListLinear List
Chapter II
𝑎𝑖 𝑎𝑖+1……
𝑎0 𝑎𝑛−1Head
Current position Tail
……
![Page 10: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/10.jpg)
10
目录页
Ming Zhang” Data Structures and Algorithms “
Classification (By storage)
• Linear List
– All entries are nodes of the same type of linear lists
– No need to limit the form of operation
– Divided into: the sequential list, linked list
depending on the difference of storage
2.1 Linear ListLinear List
Chapter II
……
……
![Page 11: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/11.jpg)
11
目录页
Ming Zhang” Data Structures and Algorithms “
Storage Structures
• Sequential list
- Store according to index values from small to large in
an adjacent continuous region
- Compact structure, and the storage density is 1
• Linked list
- Single list
- Double linked list
- Circular list
2.1 Linear ListLinear List
Chapter II
……
……
……
……
![Page 12: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/12.jpg)
12
目录页
Ming Zhang” Data Structures and Algorithms “
Classification (By operation)
• Linear List
– No need to limit the form of
operation
• Stack
– At the same end
• Queue
– At both ends
2.1 Linear ListLinear List
Chapter II
![Page 13: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/13.jpg)
13
目录页
Ming Zhang” Data Structures and Algorithms “
Classification (By operation)
• Stack (LIFO, Last In First Out)
– Insert and delete operations are restricted
to the same end of the list
2.1 Linear ListLinear List
Chapter II
k1
...
ki
ki+1
Top
End
Out In
k0
![Page 14: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/14.jpg)
14
目录页
Ming Zhang” Data Structures and Algorithms “
Classification (By operation)
• Queue (FIFO, First In First Out)
– Insert at one end of the list while delete at
the other end
2.1 Linear ListLinear List
Chapter II
• Rear(true pointer)
A B C D
front rear
Insertrear
Deletefront
B C D
rearfront
![Page 15: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/15.jpg)
15
目录页
Ming Zhang” Data Structures and Algorithms “
Operation on linear Lists
• Construct a linear list
• Destruct the linear list
• Insert a new element
• Delete a specific element
• Modify a specific element
• Sort
• Search
• …
2.1 Linear ListLinear List
Chapter II
![Page 16: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/16.jpg)
16
目录页
Ming Zhang” Data Structures and Algorithms “
Class Template of Linear lists
2.1 Linear ListLinear List
Chapter II
template <class T> class List {
void clear(); // clear the linear list
bool isEmpty(); // When it is empty, returns true
bool append(const T value);
// insert the value at the end,length adds by 1
bool insert(const int p, const T value);
// insert the value at position P,length adds by 1
bool delete(const int p);
// delete the value at position p,length decreases by 1
bool getPos(int& p, const T value);
// find the value and returns its position
bool getValue(const int p, T& value);
// return the element’s value at position P
//and assign it to the variable of value
bool setValue(const int p, const T value);
// set value for position P
};
![Page 17: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/17.jpg)
17
目录页
Ming Zhang” Data Structures and Algorithms “
Thinking
• What kind of classification are
there for the linear list?
• In all kinds of names of linear lists
,which are related to storage
structures? Which are related to
operations?
Linear List
Chapter II
![Page 18: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/18.jpg)
18
第二章
张铭《数据结构与算法》
线性表
• 2.1 Linear List
• 2.2 Sequential List
• 2.3 Linked List
• 2.4 Comparison between sequential list and linked list
Chapter II Linear List
a0 a1 a2 … … an-1
Chapter II
![Page 19: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/19.jpg)
19
目录页
Ming Zhang” Data Structures and Algorithms “
2.2 Sequential List
• Also known as the vector, fixed-length one-
dimensional array is used as the storage structure
• Key Features
- Elements are of the same type
- Elements are sequentially stored in contiguous storage
space, and each element has a unique index value
- The type of vector length is constant
• Implemented as Array
• Its elements are easy to read and write, you can
specify the location by using its subscript
- Once the starting position is got, all the data elements of
the list can be random accessed
2.2 Sequential ListLinear List
Chapter II
![Page 20: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/20.jpg)
20
目录页
Zhang Ming ”Data Structures and Algorithms “
2.2 Sequential List
• 元素地址计算如下所示:– L𝑜𝑐 𝑘𝑖 = 𝐿𝑜𝑐 𝑘0 + 𝑐 × 𝑖, 𝑐 = sizeof(𝐸𝐿𝐸𝑀)
2.2 Sequential ListLinear List
Chapter II
Logical Address(Subscript)
Data elements
Store Address Data elements
0 k0
Loc(k0) k
0
1 k1
Loc(k0)+c k
1
… … … …
i ki
Loc(k0)+i*c k
i
… …
n-1 kn-1
Loc(k0)+(n-1)*c k
n-1
The formula to calculate the elements of location is shown as below:
![Page 21: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/21.jpg)
21
目录页
Zhang Ming ”Data Structures and Algorithms “
class arrList : public List<T> { // sequential list,vector
private: // value types and value space of linear list
T * aList ; // private variables,instance of storage for sequential list
int maxSize; // private variables ,maximum length of the sequential list
int curLen; // private variables ,current length of the sequential list
int position; // private variables ,current operation location
public:
arrList(const int size) { // construct a new list,set its length to the maximum
maxSize = size; aList = new T[maxSize];
curLen = position = 0;
}
~arrList() { // destructor function used to eliminate the instance
delete [] aList;
}
Sequence List’s Class Definition
Linear ListLinear List
Chapter II
![Page 22: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/22.jpg)
22
目录页
Zhang Ming” Data Structures and Algorithms “
Chapter II
Linear List
Sequence List’s Class Definition
void clear() { // delete the content,becoming an empty list
delete [] aList; curLen = position = 0;
aList = new T[maxSize];
}
int length(); // returns the current length
bool append(const T value); // append element v at end
bool insert(const int p, const T value); // insert an element at P
bool delete(const int p); // delete the element at P
bool setValue(const int p, const T value); // set the value of an element
bool getValue(const int p, T& value); // return the value of an element
bool getPos(int &p, const T value); // seek for an element
};
2.2 Sequential List
![Page 23: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/23.jpg)
23
目录页
Zhang Ming ”Data Structures and Algorithms “
Operations in Sequential List
• Key discussions
– Insert element operation
•bool insert(const int p, const T value);
– Delete element operation
•bool delete(const int p);
• Others (Think by yourselves)
2.2 Sequential ListLinear List
Chapter II
![Page 24: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/24.jpg)
24
目录页
Zhang Ming” Data Structures and Algorithms “
Chapter II
Linear List
Diagram for the insertion of sequential list
2.2 Sequential List
curr
k0
k1
k2
k0
k1
k2
curr
k4
k3
k5
XX
k4
k3
… …
k5
![Page 25: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/25.jpg)
25
目录页
Zhang Ming ”Data Structures and Algorithms “
Insertion of sequential list// set the element type as T, aList is the array to store Sequential list,// maxSize is its maximum length;// p is the insert location of the new element ,return true if succeeds,// otherwise return false
template <class T> bool arrList<T> :: insert (const int p, const T value) {
int i;
if (curLen >= maxSize) { // check if the SL is overflow
cout << "The list is overflow"<< endl; return false;
}
if (p < 0 || p > curLen) { // check if the position to insert is valid
cout << "Insertion point is illegal"<< endl; return false;
}
for (i = curLen; i > p; i--)
aList[i] = aList[i-1]; // move right from the end curLen -1 of the
list until p
aList[p] = value; // insert a new element at p
curLen++; // adds the current length of the list by 1
return true;
}
2.2 Sequential ListLinear List
Chapter II
![Page 26: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/26.jpg)
26
目录页
Zhang Ming ”Data Structures and Algorithms “
Diagram for sequential list’s delete operation
• 2.2 Sequential List
2.2 Sequential ListLinear List
Chapter II
curr
k0
k1
k2
k0
k1
k2
curr
k4
k3
k5
k4
k3
k5
k6
k6
… …
![Page 27: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/27.jpg)
27
目录页
Zhang Ming ”Data Structures and Algorithms “
Delete operation in sequential list// set the type of the element as T;aLis is the array to store sequential list
// and p is the position of elements to delete
// returns true when delete succeed,otherwise returns false
template <class T> // the type of the elements of SL is T
bool arrList<T> :: delete(const int p) {
int i;
if (curLen <= 0 ) { // Check if the SL is empty
cout << " No element to delete \n"<< endl;
return false ;
}
if (p < 0 || p > curLen-1) { // Check if the position is valid
cout << "deletion is illegal\n"<< endl;
return false ;
}
for (i = p; i < curLen-1; i++)
aList[i] = aList[i+1]; // [p, currLen) every element move left
curLen--; // the current length of the list decreases by 1
return true;
}
2.2 Sequential ListLinear List
Chapter II
![Page 28: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/28.jpg)
28
目录页
张铭《数据结构与算法》
Algorithm analysis of insert and
delete operations in sequential list
• The movement of elements in the list
– Insert: move 𝑛 − 𝑖
– Delete: move 𝑛 − 𝑖 − 1
• The probability values to insert or delete in
position i are respectively 𝑝𝑖 𝑎𝑛𝑑 𝑝𝑖′
– The average move time for insert operation is
𝑀𝑖 = 𝑖=0𝑛 𝑛 − 𝑖 𝑝𝑖
– The average move time of delete operation is
𝑀𝑑 = 𝑖=0𝑛−1 𝑛 − 𝑖 − 1 𝑝𝑖′
2.2 Sequential ListLinear List
Chapter II
![Page 29: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/29.jpg)
29
目录页
张铭《数据结构与算法》
Algorithm Analysis
• If the probability to insert or delete in every location in
SL is the same, namely 𝑝𝑖 =1
𝑛+1, 𝑝𝑖
′ =1
𝑛
2.2 Sequential ListLinear List
Chapter II
2)1(2
)1(
1
)1(
)(1
1)(
1
1
0 00
n
n
nn
n
nn
inn
inn
Mn
i
n
i
n
i
i
2
11
2
)1(
)(1
)1(1
2
0 00
nn
n
n
ninn
inn
Mn
i
n
i
n
i
d
Time cost
is O(𝑛)
![Page 30: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/30.jpg)
30
目录页
Zhang Ming ”Data Structures and Algorithms “
Thinking
• What should you think about when
doing insert or delete operations in
sequential list?
• What advantages and disadvantages
does sequential list have?
2.2 Sequential ListLinear List
Chapter II
![Page 31: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/31.jpg)
31
目录页
张铭《数据结构与算法》
Chapter II Linear List
• 2.1 Linear List
• 2.2 Sequential List
• 2.3 Linked List
• 2.4 Comparison between
sequential list and linked list
2.3 Linked ListLinear List
Chapter II
a0
a1
an-1
head
tail
![Page 32: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/32.jpg)
32
目录页
张铭《数据结构与算法》
Linked List
• Link its storage nodes through pointers
.
• Storage nodes are consisted of two
parts
– Data field + pointer field(successor
address)
2.3 Linked ListLinear List
Chapter II
data next
![Page 33: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/33.jpg)
33
目录页
张铭《数据结构与算法》
2.3 Linked List
• Classification(according to linked
ways and the number of points)
– Single linked list
– Double linked list
– Circular linked list
2.3 Linked ListLinear List
Chapter II
a0
a1
an-1
head
tail
a0
an-1
tail
head
head
tail
a0
a1
an-1
![Page 34: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/34.jpg)
34
目录页
张铭《数据结构与算法》
Single linked list
• Simple single linked list
– The whole single linked list: head
– The first node: head
– The judge of empty list:
head == NULL
– The current node a1:curr
2.3 Linked ListLinear List
Chapter II
a0
a1
an-1
head
tailcurr
![Page 35: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/35.jpg)
35
目录页
张铭《数据结构与算法》
Single linked list
• Single linked list with head node
– The whole single linked list : head
– The first node : head->next,head ≠ NULL
– The judge of empty list:• head->next == NULL
– The current node a1:fence->next (curr
implied)
2.3 Linked ListLinear List
Chapter II
a0
a1
an-1
head
tailfence curr
![Page 36: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/36.jpg)
36
目录页
张铭《数据结构与算法》
Node type of the single
linked list template <class T> class Link {
public:
T data; // to protect content of the node elements
Link<T> * next; // the pointer which points to successor point
Link(const T info, const Link<T>* nextValue =NULL) {
data = info;
next = nextValue;
}
Link(const Link<T>* nextValue) {
next = nextValue;
}
};
2.3 Linked ListLinear List
Chapter II
![Page 37: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/37.jpg)
37
目录页
张铭《数据结构与算法》
Class definition of single list
template <class T> class lnkList : public List<T> {
private:
Link<T> * head, *tail; // head and tail pointer of the single list
Link<T> *setPos(const int p); // the pointer of the pth element
public:
lnkList(int s); // constructed function
~lnkList(); // destructor
bool isEmpty(); // judge whether the link is empty
void clear(); // clear the link’s storage and it becomes an empty list
int length(); // returns the current length of the sequential list
bool append(cosnt T value); // add an element value at the end,// the length of the list added by 1
bool insert(cosnt int p, cosnt T value); // insert an element at p
bool delete(cosnt int p); // delete the element at p,// the length of the list decreased by 1
bool getValue(cosnt int p, T& value); // get the value of the element at p
bool getPos(int &p, const T value); // seek for element with value T
}
2.3 Linked ListLinear List
Chapter II
![Page 38: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/38.jpg)
38
目录页
张铭《数据结构与算法》
Seek the ith node in the single linked list
// the return value of the function is the found node pointer
template <class T> // the element type of the linked list is P
Link<T> * lnkList <T>:: setPos(int i) {
int count = 0;
if (i == -1) // if i was -1, then locate it to the head
return head;
// circular location, if I was 0 then locate to the first node
Link<T> *p = head->next;
while (p != NULL && count < i) {
p = p-> next;
count++;
};
// points to the ith node,i=0,1,…,when the number of
// the nodes of the list is less than i then return NULL
return p;
}
2.3 Linked ListLinear List
Chapter II
![Page 39: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/39.jpg)
39
目录页
Zhang Ming ”Data Structures and Algorithms “
Insert operation of single linked list
• Create a new node
• New node points to the right node
• The left node points to new node
2.3 Linked ListLinear List
Chapter II
Insert 10 between 23 and 12
20 23 15head
tail
12
20 23 15head
tail
1210
![Page 40: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/40.jpg)
40
目录页
张铭《数据结构与算法》
Insert algorithm of single linked list// insert a new node as the ith node
template <class T>
// element type of the linked list is T
bool lnkList<T> :: insert(const int i, const T value) {
Link<T> *p, *q;
if ((p = setPos(i -1)) == NULL) { // p is the previous node of the ith node
cout << " illegal insert position"<< endl;
return false;
}
q = new Link<T>(value, p->next);
p->next = q;
if (p == tail) // insert position is at the tail and
// the node inserted becomes the new tail
tail = q;
return true;
}
2.3 Linked ListLinear List
Chapter II
![Page 41: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/41.jpg)
41
目录页
张铭《数据结构与算法》
Delete operation of single linked list
• Delete the node x from linked list
– 1. Assign p to point to the previous node of
element x
– 2. delete the node with element x
– 3. release the space that x occupied
2.3 Linked ListLinear List
Chapter II
![Page 42: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/42.jpg)
42
目录页
张铭《数据结构与算法》
Example of delete operation of single linked list
• 2.3 Linked List
Linear ListLinear List
Chapter II
head
tail
x
p
p = head;
while (p->next!=NULL && p->next->info!= x)
p = p->next;
![Page 43: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/43.jpg)
43
目录页
张铭《数据结构与算法》
Delete the node with value X
• 2.3 Linked List
Linear ListChapter II
xhead
tail
qp
q = p->next;
p->next = q->next;
free(q);
![Page 44: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/44.jpg)
44
目录页
张铭《数据结构与算法》
Delete algorithm of single linked listtemplate <class T> // Element type of the linked list is T
bool lnkList<T>:: delete((const int i) {
Link<T> *p, *q;
// node to delete doesn’t exist, when the given i is bigger than
// the number of the current elements in the list
if ((p = setPos(i-1)) == NULL || p == tail) {
cout << " illegal delete position " << endl;
return false;
}
q = p->next; // q is the real node to delete
if (q == tail) { // if the node to delte is the tail,
// then change the tail pointer
tail = p; p->next = NULL:
}
else //delete node q and change linked pointer
p->next = q->next;
delete q;
return true;
}
2.3 Linked ListLinear List
Chapter II
![Page 45: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/45.jpg)
45
目录页
张铭《数据结构与算法》
Operation analysis of single linked list
• 对一个结点操作,必先找到它,即用一个指针指向它
• 找单链表中任一结点,都必须从第一个点开始
• 单链表的时间复杂度 𝑂 𝑛
– 定位: :𝑂 𝑛
– 插入: 𝑂 𝑛 + 𝑂 1
– 删除:𝑂 𝑛 + 𝑂(1)
2.3 Linked ListLinear List
Chapter II
p = head;
while (not reaching) p = p->next;
To operate on a node you must find it first, which means to get a pointer address
To find any node in single linked list you must begin from the first node
The time complexity
locating:
insert
delete
![Page 46: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/46.jpg)
46
目录页
张铭《数据结构与算法》
Double linked list
• To make up the disadvantages of single linked
list, double linked list appears.
• The next field of single linked list only points to
the previous node , it can not be used to find
the successive node. The same for “single prev”.
• So, we add a pointer that points to the
precursor node of it in the double linked list.
2.3 Linked ListLinear List
Chapter II
a0
an-1
tail
head
![Page 47: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/47.jpg)
47
目录页
张铭《数据结构与算法》
Double linked list and type of its node
template <class T> class Link {
public:
T data; // used to store content of node elements
Link<T> * next; // the pointer points to successor node
Link<T> *prev; // the pointer points to precursor node
Link(const T info, Link<T>* preValue = NULL, Link<T>* nextValue =
NULL) {
// constructor with given value and precursor and successor pointers
data = info;
next = nextValue;
prev = preValue;
}
Link(Link<T>* preValue = NULL, Link<T>* nextValue = NULL) {
// constructor with given value and precursor and successor pointers
next = nextValue;
prev = preValue;
}
}
2.3 Linked ListLinear List
Chapter II
![Page 48: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/48.jpg)
48
目录页
Zhang Ming ”Data Structures and Algorithms “
Insert procedure of double linked list (Be careful with the order)
p
q
q->next=p->next
q->prev=p
p->next=q
q->next->prev=q
new q;①
④②
③
Insert a new node after the node pointed by p
2.3 Linked ListLinear List
Chapter II
![Page 49: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/49.jpg)
49
目录页
Zhang Ming ”Data Structures and Algorithms “
Delete procedure
• If you delete p
immediately
– Do not need to
assign the null
value
2.3 Linked ListLinear List
Chapter II
Delete the node pointed by p
p
p->prev->next=p->next
p->next->prev=p->prev
p->next=NULL
p->prev=NULL
①
②
![Page 50: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/50.jpg)
50
目录页
张铭《数据结构与算法》
Circularly linked list
• Link the head and tail of single linked list and
double linked list, and we created circular lists
• Do not increase other cost, but benefit lots of
operations
– From any node of circular list you can access all
the other nodes
2.3 Linked ListLinear List
Chapter II
head
tail
a0
a1
an-1
![Page 51: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/51.jpg)
51
目录页
张铭《数据结构与算法》
Boundary conditions of
linked list• Treatment of some special points
– Treatment with the head node
– Pointer field of the tail node of a non-circular list
should be kept as NULL
– Tail of a circular list points to its head pointer
• Treatment with linked list
– Special treatment with empty linked list
– When insert or delete nodes, be careful with the
linking process of the related pointers
– The correctness of points moving
• insert
• search or iteration
2.3 Linked ListLinear List
Chapter II
![Page 52: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/52.jpg)
52
目录页
张铭《数据结构与算法》
Thinking
• Think about the single linked list
with head or not.
• The problems you should consider
when deal with linked list.
2.3 Linked ListLinear List
Chapter II
![Page 53: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/53.jpg)
53
目录页
张铭《数据结构与算法》
Chapter II Linear List
• 2.1 Linear List
• 2.2 Sequential List
• 2.3 Linked list
• 2.4 Comparison between
sequential list and linked list
Linear List
Chapter II
![Page 54: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/54.jpg)
54
目录页
张铭《数据结构与算法》
2.4 Comparison of the implementation method of linear list
• Main advantages of sequential lists
– No pointers, and no overhead cost
– Read an element in a sequential list is quite easy and
convenient
• Main advantages of linked list
– No need to know the list length list before construction
– The length of the linked list can be dynamically changed
– Support frequent insert and delete operations
• To sum up
– Sequential list is the best choice for storing static data
– Linked list is a good choice for storing dynamic data
2.4 Comparison of linear list’s achieving waysLinear List
Chapter II
![Page 55: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/55.jpg)
55
目录页
张铭《数据结构与算法》
Comparison between sequential list and linked list
• Sequential list
– Time cost of insert and delete operation is O(n),search of
ith element can be done in constant time.
– You must apply for continuous storage space with fixed
length previously
– If the whole array is full there will be no structural storage
cost
• Linked list
– Time cost of insert and delete operation is O(1),but the
cost for finding the ith element is O(n)
– Uses pointers for storage, you need to assign storage space
dynamically to the new elements per demand
– Every element has overhead storage cost
2.4 Comparison of linear list’s achieving waysLinear List
Chapter II
![Page 56: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/56.jpg)
56
目录页
张铭《数据结构与算法》
Storage density of Sequential list and linked list
n means the current number of elements in linear list
P means the size of the storage space of the pointer(usually 4 bytes)
E means the size of the storage space of the data element
D means the maximum number of linked list elements that
can be stored in array
• Space requirement
– Space requirement of sequential list is DE
– Space requirement of linked list is n(P + E)
• The critical value of n,namely n > DE / (P+E)
– The bigger the n, the higher the space efficiency of
sequential list
– If P = E,then the critical value is n = D / 2
2.4 Comparison of linear list’s achieving waysLinear List
Chapter II
![Page 57: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/57.jpg)
57
目录页
张铭《数据结构与算法》
The choice in different situations
• Situations sequential list not fit
– Insert or delete operations are frequent
– The maximum length of the linear list is also a
vital consideration
• Situations linked list not fit
– When read operation is more frequen than
insert or delete operations
– When the storage cost of the pointer is
relatively big compared to the occupied space
of the attributes of a node , think carefully.
2.4 Comparison of linear list’s achieving waysLinear List
Chapter II
![Page 58: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/58.jpg)
58
目录页
张铭《数据结构与算法》
Choice between Sequential list and linked list
• Sequential list
– The number of nodes can be estimated
– The nodes are relatively stable
(insert and delete operation are not frequent )
– n > DE / (P + E)
• Linked list
– The number of nodes cannot be estimated
– The node are dynamic
(insert and delete operation are frequent )
– n < DE / (P + E)
2.4 Comparison of linear list’s achieving waysLinear List
Chapter II
![Page 59: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/59.jpg)
59
目录页
张铭《数据结构与算法》
Thinking
• Choose between sequential list
and linked list.
– Dynamic change of nodes
– Storage density
Linear ListChapter II
Chapter II
![Page 60: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/60.jpg)
60
目录页
张铭《数据结构与算法》
Thinking: expression of polynomial with one variable
• Polynomial with one variable: Pn(x) = p
0+p
1x+p
2x
2+…+p
nx
n
• Linear list expression:P = (p0, p
1, p
2, …, p
n)
• Sequential list expression:only save the coefficient(the ith element save X’s
coefficient)
the situation when the data is sparse: p(x) = 1+2x10000
+4x40000
• Linked list expression: node structure
2.4 Comparison of linear list’s achieving waysLinear List
Chapter II
coef. index
0 -1 1 0 2 10000 4 40000
p0 p1 p2 … … pn
![Page 61: Data Structures and Algorithms 2 - edX...3 目录页 Ming Zhang”Data Structures and Algorithms “ The Concepts of Linear List • List for short, is a finite sequence of zero or](https://reader030.vdocuments.site/reader030/viewer/2022040509/5e50da524489c722746a2582/html5/thumbnails/61.jpg)
Ming Zhang“ Data Structures and Algorithms “
Data Structures and Algorithms
Thanks
the National Elaborate Course (Only available for IPs in China)
http://www.jpk.pku.edu.cn/pkujpk/course/sjjg/
Ming Zhang, Tengjiao Wang and Haiyan Zhao
Higher Education Press, 2008.6 (awarded as the "Eleventh Five-Year" national planning textbook)