linked listsanhtt/slidesss/dataalgo/dataalgo-lists.pdf · 3 linked list za linked list is an...
TRANSCRIPT
![Page 1: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/1.jpg)
Linked ListsTruong Tuan Anh
CSE-HCMUT
![Page 2: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/2.jpg)
2
Outline
Basic conceptsList Operations
![Page 3: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/3.jpg)
3
Linked List
A linked list is an ordered collection of data in which each element contains the location of the next element.
list //Linked Implementation of List
head <pointer>
count <integer> //number of elements (optional)
end list
![Page 4: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/4.jpg)
4
Linked List: Implementing in C++
![Page 5: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/5.jpg)
5
Nodes
An element in a linked list is called a nodeA node in a linked list is a structure that has at least two fields:
The dataThe address of the next node
![Page 6: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/6.jpg)
6
Nodes
![Page 7: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/7.jpg)
7
Nodes: Implementing in C++
![Page 8: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/8.jpg)
8
Nodes: Implementing in C++
![Page 9: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/9.jpg)
9
Nodes: Example
![Page 10: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/10.jpg)
10
Nodes: Example
![Page 11: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/11.jpg)
11
Operations
![Page 12: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/12.jpg)
12
Linked List Operations
Create an empty linked listInsert a node into a linked listDelete a node from a linked listSearch a node from a linked listSort a linked list
![Page 13: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/13.jpg)
13
Create an Empty Linked List
![Page 14: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/14.jpg)
14
Create an Empty Linked List
![Page 15: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/15.jpg)
15
Insert a Node into an Empty Linked List
1. Allocate memory for the new node and set up data2. Locate the pointer p in the list, which will point to the
new node:1. If the new node becomes the first element in the
List: p is list.head2. Otherwise: p is pPre->link, where pPre points
to the predecessor of the new node3. Point the new node to its successor4. Point the pointer p to the new node
![Page 16: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/16.jpg)
16
Insert a Node into an Empty Linked List
![Page 17: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/17.jpg)
17
Insert a Node at the Beginning of a List
![Page 18: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/18.jpg)
18
Insert a Node in the Middle of a List
![Page 19: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/19.jpg)
19
Insert a Node at the End of a List
![Page 20: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/20.jpg)
20
Insert a Node into a List
Insertion is successful when allocation memory for the new node is successful
There is no difference between insertion at the beginning of the list and insertion into an empty list
pNew->link = list.Headlist.head = pNew
There is no difference between insertion in the middle and insertion at the end of the list
pNew->link = pPre->linkpPre->link = pNew
![Page 21: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/21.jpg)
21
Insert a Node into a List
![Page 22: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/22.jpg)
22
Deletion
![Page 23: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/23.jpg)
23
Delete a node from a linked list
1. Locate the pointer p in the list which points to the node to be deleted (pLoc will hold the node to be deleted)
1. If that node is the first element in the List: p is list.head2. Otherwise: p is pPre->link, where pPre points to
the predecessor of the node to be deleted2. p points to the successor of the node to be
deleted3. Recycle the memory of the deleted node
![Page 24: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/24.jpg)
24
Delete First Node
![Page 25: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/25.jpg)
25
General Deletion Case
![Page 26: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/26.jpg)
26
Delete a Node from a Linked List
Removal is successful when the node to be deleted is foundThere is no difference between deleting the node from the beginning of the list and deleting the only node in the list
list.head = pLoc->linkrecycle(pLoc)
There is no difference between deleting a node from the middle and deleting a node from the end of the list
pPre->link = pLoc->linkrecycle(pLoc)
![Page 27: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/27.jpg)
27
Delete a Node from a Linked List
![Page 28: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/28.jpg)
28
Destroy a Linked List
Delete all nodes in the list→ save memory
How?
![Page 29: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/29.jpg)
29
Destroy a Linked List
![Page 30: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/30.jpg)
30
Searching
![Page 31: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/31.jpg)
31
Searching in a Linked List
Sequence Search has to be used for the linked list
Function Search of List ADT:<ErrorCode> Search (val target
<dataType>,ref pPre <pointer >, ref pLoc <pointer>)
Searches a node and returns a pointer to it if found
![Page 32: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/32.jpg)
32
Searching in a Linked List
![Page 33: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/33.jpg)
33
Searching in a Linked List
![Page 34: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/34.jpg)
34
Other Lists
Doubly Linked List
Circularly Linked List
![Page 35: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element](https://reader035.vdocuments.site/reader035/viewer/2022070923/5fbbbe96ed166578b6306b5b/html5/thumbnails/35.jpg)
35
Takeaways
Basic conceptsList operations