Cơ sở dữ liệu - Chapter 2: List

pdf 72 trang vanle 2870
Bạn đang xem 20 trang mẫu của tài liệu "Cơ sở dữ liệu - Chapter 2: List", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfco_so_du_lieu_chapter_2_list.pdf

Nội dung text: Cơ sở dữ liệu - Chapter 2: List

  1. Chapter 2 – LIST  Linear List Concepts  List ADT  Specifications for List ADT  Implementations of List ADT  Contiguous List  Singly Linked List  Other Linked Lists  Comparison of Implementations of List 1
  2. Linear List Concepts DEFINITION: Linear List is a data structure where each element of it has a unique successor. element 1 element 2 element 3 2
  3. Linear List Concepts (cont.) 3
  4. Linear List Concepts (cont.) General list: • No restrictions on which operation can be used on the list • No restrictions on where data can be inserted/deleted. . Unordered list (random list): Data are not in particular order. . Ordered list: data are arranged according to a key. 4
  5. Linear List Concepts (cont.) Restricted list: • Only some operations can be used on the list. • Data can be inserted/deleted only at the ends of the list. . Queue: FIFO (First-In-First-Out). . Stack: LIFO (Last-In-First-Out). 5
  6. List ADT element 1 element 2 element 3 . . . element n DEFINITION: A list of elements of type T is a finite sequence of elements of T together with the following operations: Basic operations: • Construct a list, leaving it empty. • Insert an element. • Remove an element. • Search an element. • Retrieve an element. • Traverse the list, performing a given operation on each element. 6
  7. List ADT (cont.) Extended operations: • Determine whether the list is empty or not. • Determine whether the list is full or not. • Find the size of the list. • Clear the list to make it empty. • Replace an element with another element. • Merge two ordered list. • Append an unordered list to another. • 7
  8. Insertion  Insert an element at a specified position p in the list Only with General Unordered List.  Insert an element with a given data With General Unordered List: can be made at any position in the list (at the beginning, in the middle, at the end). With General Ordered List: data must be inserted so that the ordering of the list is maintained (searching appropriate position is needed). With Restricted List: depend on it own definition (FIFO or LIFO). 8
  9. Insertion (cont.) Insert an element at a specified position p in the list. 60 1 p-2 p-1 p n Before: 20 30 10 50 40 1 p-2 p-1 p p+1 n+1 After: 20 30 10 60 50 40 Any element formerly at position p and all later have their position numbers increased by 1. 9
  10. Removal, Retrieval  Remove/ Retrieve an element at a specified position p in the list With General Unordered List and General Ordered List.  Remove/ Retrieve an element with a given data With General Unordered List and General Ordered List: Searching is needed in order to locate the data being deleted/ retrieved. 10
  11. Removal Remove an element at a specified position p in the list. 1 p-2 p-1 p p+1 n+1 Before: 20 30 10 60 50 40 1 p-2 p-1 p n After: 20 30 10 50 40 The element at position p is removed from the list, and all subsequent elements have their position numbers decreased by 1 11
  12. Retrieval Retrieve an element at a specified position p in the list. 1 p-2 p-1 p p+1 n+1 Before: 20 30 10 60 50 40 1 p-2 p-1 p p+1 n+1 After: 20 30 10 60 50 40 retrieved data = 60 All elements remain unchanged. 12
  13. Success of Basic Operations  Insertion is successful when the list is not full.  Removal, Retrieval are successful when the list is not empty. 13
  14. Specification for List ADT Create() Traverse (ref Operation ( ref Data ) ) Search (ref DataOut ) // DataOut contains values need to be found in key field, and will reveive all other values in other fields. // For Unsorted List: Insert (val DataIn , val position ) Remove (ref DataOut ,val position ) Retrieve (ref DataOut ,val position ) Replace (val DataIn , ref DataOut , val position ) (Operations are successful when the required position exists). 14
  15. Specification for List ADT (cont.) // For Sorted List: Insert (val DataIn ) Remove (ref DataOut ) // DataOut contains values need to be found in key field, and will reveive all other values in other fields. Retrieve (ref DataOut ) // DataOut contains values need to be found in key field, and will reveive all other values in other fields. (Insertion is successful when the list is not full and the key needs to be inserted does not exist in the list. Removal and Retrieval are successful when the list is not empty and the required key exists in the list). 15
  16. Specification of List ADT (cont.) Samples of Extended methods: isFull() isEmpty() Size() Sort () AppendList (ref ListIn ) // For Unordered Lists. ListIn may be unchanged or become empty. Merge (ref ListIn1 , ref ListIn2 ) // For Ordered Lists. . . . 16
  17. Specification of List ADT (cont.) Samples of variants of similar methods: Create() Create (ref file ) // made a list from content of a file Insert (val DataIn , val position ) InsertHead (val DataIn ) InsertTail (val DataIn ) Replace (val DataIn , ref DataOut , val position ) Replace (val DataIn , ref DataOut ) 17
  18. Specification of List ADT (cont.) Samples of variants of similar methods: Remove (val position ) Remove (ref DataOut , val position ) RemoveHead (val DataOut ) RemoveTail (ref DataOut ) Search (val DataIn , ref ListOut ) // DataIn contains values need to be found in some fields, ListOut will contain all members having that values. . . . 18
  19. Sample of using List ADT #include #include // uses Unordered List ADT. int main() { List listObj; cout > x; listObj.Insert( x, listObj.Size() ); // Insert at the end of the list. } cout << "Elements in the list: \n"; for (i=0; i<10; i++) { listObj.Retrieve(x, i); cout << x << "\t"; } return 0; } 19
  20. Implementations of List ADT  Contiguous implementation: . Automatically Allocated Array with fixed size. . Dynamically Allocated Array with flexible size.  Linked implementations: . Singly Linked List . Circularly Linked List . Doubly Linked List . Multilinked List . Skip List . . . .  Linked List in Array 20
  21. Automatically Allocated Array count n 0 1 2 3 n-2 n-1 max-2 max-1 max data x x x x x x Array with pre-defined maxsize and has n elements. List // Contiguous Implementation of List count // number of used elements (mandatory). data > // (Automatically Allocated Array) End List 21
  22. Dynamically Allocated Array count n maxsize max data 0 1 2 3 n-2 n-1 max-2 max-1 max x x x x x x List // Contiguous Implementation of List count // number of used elements (mandatory). data > // (Dynamically Allocated Array) maxsize End List 22
  23. Contiguous Implementation of List In processing a contiguous list with n elements: • Insert and Remove operate in time approximately proportional to n (require physical shifting). • Clear, Empty, Full, Size, Replace, and Retrieve in constant time. 23
  24. Singly Linked List head data link Singly Linked List List // Linked Implementation of List (for Singly Linked List) head count // number of elements (optional). End List head head count 0 An empty Singly Linked List An empty Singly Linked List having only head. having head and count. 24
  25. Singly Linked List (cont.) data link Node data link Element in the Singly Linked List End Node General DataType: DataType key field1 field2 fieldn DataType may be an atomic or a composite data End DataType 25
  26. Singly Linked List (cont.) • Sample: 26
  27. Singly Linked List (cont.) • Sample: list representing polynomial 27
  28. Create an Empty Linked List Before After head ? head head = NULL List having head head ? head head = NULL count = ? count = 0 count = 0 List having head and count 28
  29. Create an Empty Linked List (cont.) Create() Creates an empty link list Pre none Post An empty linked list has been created. 1. head = NULL 2. Return end Create 29
  30. Insert Node to a Linked List 1. Allocate memory for the new node and set up data. 2. Locate the pointer p in the list, which will point to the new node:  If the new node becomes the first element in the List: p is head. head pNew x  Otherwise: p is pPre->link, where pPre points to the predecessor of the new node. pNew x head pPre 30
  31. Insert Node to a Linked List (cont.) 3. Update pointers: – Point the new node to its successor. – Point the pointer p to the new node. pNew->link = head (1) head X head= pNew (2) (2) pNew (1) X pNew (2) (1) X pNew->link = pPre->link (1) head pPre->link = pNew (2) pPre X 31
  32. Insert Node to a Linked List (cont.)  Insertion is successful when allocation memory for the new node is successful. 32
  33. Insert Node to a Linked List (cont.)  There is no difference between  insertion in the middle (a) and insertion at the end of the list (b) pNew (2) (1) X pNew->link = pPre->link (1) head pPre->link = pNew (2) (a) pPre X pNew x head (b) pPre 33
  34. Insert Node to a Linked List (cont.)  There is no difference between  insertion at the beginning of the list (a) and insertion to an empty list (b). head X (2) (a) pNew (1) X pNew->link = head (1) (b) head head= pNew (2) head pNew pNew 34
  35. Insert Algorithm Insert (val DataIn ) // For ordered list. Inserts a new node in a singly linked list. Pre DataIn contains data to be inserted Post If list is not full, DataIn has been inserted; otherwise, list remains unchanged. Return success or overflow. 35
  36. InsertNode Algorithm (cont.) Insert (val DataIn ) 1. Allocate pNew 2. if (memory overflow) 1. return overflow 3. else 1. pNew->data = DataIn 2. Locate pPre // pPre remains NULL if Insertion at the beginning or to an empty list 3. if (pPre = NULL) // Adding at the beginning or to an empty list 1. pNew->link = head 2. head = pNew 4. else // Adding in the middle or at the end of the list 1. pNew->link = pPre->link 2. pPre->link = pNew 5. return success end Insert 36
  37. Remove Node from a Linked List 1. Locate the pointer p in the list which points to the node to be deleted (pDel will hold the node to be deleted).  If that node is the first element in the List: p is head. head pDel  Otherwise: p is pPre->link, where pPre points to the predecessor of the node to be deleted. head pPre pDel 37
  38. Remove Node from a Linked List (cont.) 2. Update pointers: p points to the successor of the node to be deleted. head = pDel->link X Recycle pDel head pDel pPre->link = pDel->link head Recycle pDel pPre pDel X 3. Recycle the memory of the deleted node. 38
  39. Remove Node from a Linked List (cont.)  Removal is successful when the node to be deleted is found. 39
  40. Remove Node from a Linked List (cont.)  There is no difference between  Removal a node from the middle (a) and removal a node from the end (b) of the list. pPre->link = pDel->link head Recycle pDel pPre pDel (a) X pPre pDel head (b) pPre pDel head X 40
  41. Remove Node from a Linked List (cont.)  There is no difference between  removal the node from the beginning (a) of the list and removal the only-remained node in the list (b). X (a) head pDel (b) head head head = pDel->link pDel pDel Recycle pDel 41
  42. RemoveNode Algorithm Remove (ref DataOut ) Removes a node from a singly linked list. Pre DataOut contains the key need to be removed. Post If the key is found, DataOut will contain the data corresponding to it, and that node has been removed from the list; otherwise, list remains unchanged. Return success or failed. 42
  43. RemoveNode Algorithm (cont.) Remove (ref DataOut ) 1. Allocate pPre, pDel // pPre remains NULL if the node to be deleted is at the beginning of the list or is the only node. 2. if (pDel is not found) 1. return failed 3. else 1. DataOut = pDel->data 2. if (pPre = NULL) // Remove the first node or the only node 1. head = pDel->link 3. else // Remove the node in the middle or at the end of the list 1. pPre->link = pDel->link 4. recycle pDel 5. return success end Remove 43
  44. Search Algorithm for Auxiliary Function in Class  This search algorithm is not a public method of List ADT.  Sequence Search has to be used for the linked list.  This studying for the case: List is ordered accordingly to the key field. 44
  45. Search Algorithm for Auxiliary Function in Class • Public method Search of List ADT: Search (ref DataOut ) Can not return a pointer to a node if found. • Auxiliary function Search of List ADT: Search (val target , ref pPre , ref pLoc ) Searches a node and returns a pointer to it if found. 45
  46. Search Algorithm (cont.) Search (val target , ref pPre , ref pLoc ) Searches a node in a singly linked list and return a pointer to it if found. // For Ordered List Pre target is the key need to be found Post pLoc points to the first node which is equal or greater than key, or is NULL if target is greater than key of the last node in the list. pPre points to the largest node smaller than key, or is NULL if target is smaller than key of the first node. Return found or notFound 47
  47. Search Algorithm (cont.) Search (val target , ref pPre , ref pLoc ) // For Ordered List 1. pPre = NULL 2. pLoc = head 3. loop ( (pLoc is not NULL) AND (target > pLoc ->data.key) ) 1. pPre = pLoc 2. pLoc = pLoc ->link 4. if (pLoc is NULL) 1. return notFound 5. else 1. if (target = pLoc ->data.key) 1. return found 2. else 1. return notFound end Search 48
  48. Retrieve Algorithm Using Search Algorithm to locate the node Retrieving data from that node 49
  49. Retrieve Algorithm (cont.) Retrieve (val target , ref DataOut ) Retrieves data from a singly linked list Pre target is the key its data need to be retrieved Post if target is found, DataOut will receive data Return success or failed Uses Auxiliary function Search of class List ADT. 50
  50. RetrieveNode Algorithm (cont.) Retrieve (val target , ref DataOut ) 1. errorCode = Search (target, pPre, pLoc) 2. if (errorCode = notFound) 1. return failed 3. else 1. DataOut = pLoc->data 2. return success end Retrieve 51
  51. Traverse List Traverse Module controls the loop: Calling a user-supplied operation to process data Traverse (ref Operation ( ref Data ) ) Traverses the list, performing the given operation on each element. Pre Operation is user-supplied. Post The action specified by Operation has been performed on every element in the list, beginning at the first element and doing each in turn. 1. pWalker = head 2. loop (pWalker is not NULL) 1. Operation(pWalker->data) 2. pWalker = pWalker->link end Traverse 52
  52. Traverse List (cont.) User controls the loop: Calling GetNext Algorithm to get the next element in the list. GetNext (val fromWhere , ref DataOut ) Traverses the list, each call returns data of an element in the list. Pre fromWhere is 0 to start at the first element, otherwise, the next element of the current needs to be retrieved. Post According to fromWhere, DataOut contains data of the first element or the next element of the current (if exists) in the list. That element becomes the current. Return success or failed. 53
  53. Traverse List (cont.) User controls the loop: Calling GetNext Algorithm to get the next element in the list. Operation ( ref Data )// the function needs to apply to each element in the list. User controls the loop: 1. errorCode = GetNext(0, DataOut) 2. loop (errorCode = success) 1. Operation(DataOut) 2. errorCode = GetNext(1, DataOut) 54
  54. GetNext Algorithm head current List head current End List • Singly Linked List has additional attribute current pointing to the current element (the last element has just been processed). • All additional attributes must be updated when necessary!. 55
  55. GetNext Algorithm (cont.) GetNext (val fromWhere , ref DataOut ) 1. if (fromWhere is 0) 1. if (head is NULL) 1. return failed 2. else 1. current = head 2. DataOut = current->data 3. return success 2. else 1. if (current->link is NULL) 1. return failed 2. else 1. current = current->link 2. DataOut = current->data 3. return success end GetNext 56
  56. Clear List Algorithm Clear() Removes all elements from a list. Pre none. Post The list is empty. 1. loop (head is not NULL) 1. pDel = head 2. head = head->link 3. recycle pDel 2. return end Clear 57
  57. Comparison of Implementations of List  Contiguous storage is generally preferable . When the entries are individually very small; . When the size of the list is known when the program is written; . When few insertions or deletions need to be made except at the end of the list; and . When random access is important.  Linked storage proves superior . When the entries are large; . When the size of the list is not known in advance; and . When flexibility is needed in inserting, deleting, and rearranging the entries. 58
  58. Doubly Linked List current Doubly Linked List allows going forward and backward. Node data List next current previous End List End Node 59
  59. Doubly Linked List current Doubly Linked List allows going forward and backward. Insert an element in Doubly Linked List 60
  60. Circularly Linked List current data link Node List data current link End List End Node 61
  61. Double Circularly Linked List current Node data List next current previous End List End Node 62
  62. Multilinked List • Sẽ bổ sung các hình minh họa các complex linked list (chỉ hình ảnh minh họa mà thôi) Multilinked List allows traversing in different order. 63
  63. Skip List Skip List improves sequential searching. 64
  64. Choice of variants of Linked List To choose among linked Implementations of List, consider: . Which of the operations will actually be performed on the list and which of these are the most important? . Is there locality of reference? That is, if one entry is accessed, is it likely that it will next be accessed again? . Are the entries processed in sequential order? If so, then it may be worthwhile to maintain the last-used position as part of list. . Is it necessary to move both directions through the list? If so, then doubly linked lists may prove advantageous. 65
  65. Linked List In Array There are two linked lists in array: • One (head) manages used entries. • Another (available) manages empty entries (have been used or not yet) 66
  66. Multilinked List In Array 67
  67. Two one-dimensional arrays of Linked List are used 69
  68. Student no. Course no. Grade Row link Column link 70
  69. Sparce Matrices • Why two arrays of linked lists? • How about two linked lists of linked lists? • How about 3-D sparse matrices? 71
  70. Variants of List are used for Graph and Hash Table, we will see later. 72