Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1 CS212:DATA STRUCTURES Computer Science Department Lecture 5: Linked List linear list Concept 2 Linear list : Each element has a unique successor. Two approaches to implement a linear list Array Inefficient Linked when element needs to be inserted or deleted. list Efficient when element needs to be inserted or deleted. Categories of linear list 3 24-May-17 Computer Science Department Linear list Operations 4 Insertion Deletion Retrieval Traversal 24-May-17 Computer Science Department Linked list 5 An ordered collection of data in which each element contains the location of the next element. Each element contains two part Data be processed Address of the next element 24-May-17 Computer Science Department Linked list-Nodes 6 A node is a structure has at least two fields: Data Address of the next node Data : 6 Address of the next node : 300 Linked list-Nodes 7 A node with one data field number A node with three data fields name id grdPts A structure in a node name Home_Address 24-May-17 phone Computer Science Department Linked List -Head 8 A structure that identifies the list Head points to the first element in the list Sometimes used to store metadata(data about the list) Examples: Counter : to count the number of element in the list Maximum: to store the maximum number in list Head and Node 9 24-May-17 Computer Science Department Data Node Structure 10 dataType key <keyType> field1 <…> field2 <…> … fieldN <…> end dataType 24-May-17 Key is included if the application requires searching by key Computer Science Department Common Operations on a Linked List 11 Linked List operation Details Create a list Insert a node Delete a node Create the head of the list Search list Retrieve node Traverse List Use a sequential search to find an element in the list Add data to a linked list Requires a search algorithm to find the element to be deleted Locate data and retrieve it to the calling module All element are retrieved in sequence Creating a list 12 Parameter: Head structure This algorithm initializes the metadata of the list. Creating a list (Pseudocode) 13 24-May-17 Computer Science Department Insert Node 14 Three steps : 1. Allocate memory for the new node and insert data. 2. Point the new node to its successor. 3. Point the new node’s predecessor to the new node Three cases: 1. Insert into an empty list 2. Insert at beginning 3. Insert at middle 4. Insert at end. Insert into an empty list 15 The list is empty if the head points to null. 24-May-17 Computer Science Department Insert at beginning 16 The node inserted before the first node of the list 24-May-17 Computer Science Department Insert in Middle 17 1.Point new node to its successor 2.Point successor to the new node 24-May-17 Computer Science Department Insert at end 18 1. 2. Point predecessor to the new node. Set new node (link) to null. 24-May-17 Computer Science Department Inset Node Algorithm 19 24-May-17 Computer Science Department Inset Node Algorithm (Cont.) 20 24-May-17 Computer Science Department Delete a node 21 Three steps: 1. Search for the node 2. Change predecessor link filed to point to the deleted node successor. 3. Recycle the node to dynamic memory. Two cases: 1. Delete at the beginning 2. Delete at middle/end 24-May-17 Computer Science Department Delete First Node 22 The link of the head points To node 75. Figure 3.6 General Delete Case 23 The link of the predecessor points to the deleted node successor 24-May-17 Computer Science Department Delete Node Algorithm 24 24-May-17 Computer Science Department Delete Node Algorithm (Cont.) 25 24-May-17 Computer Science Department Search List 26 We need to search the list in order to Insert data Delete data Retrieve data Can’t use binary search algorithm with linked list No 24-May-17 physical relationship between the nodes Computer Science Department Search List 27 24-May-17 Computer Science Department Doubly Linked Lists 28 The linked list is redefined so that each node in the list has two pointers, one to the successor and one to the predecessor. This list is called a doubly linked list . 29 Insert a node at the end of integerdoubly linked list : Same as single linked list Connect previous and next links. Steps: 1. A new node is created, and then its three data members are initialized 1. 2. 3. 2. 3. ( info member to a number, next member to null, and the prev member to value of tail) tail is set to point to the new node The next member of the predecessor is set to point to the new node 30 31 Delete a Node at the end of Integer-doubly linked list : A temporary variable (ex. el) is set to the value in the node. 2. tail is set to its predecessor. 3. The last node is deleted. 4. The next member of the tail node is set to null. 5. Return the copy of the object stored in the removed node. Note : before deleting, check if the list is empty. 1. 32 Node to be deleted Circular Lists 33 A list where nodes form a ring ; each node has a successor. Example where Circular list is used: Several processes are using the same resources for the same amount of time, and each process need a fair share of the resources. Circular Lists: Operations 34 Same as single linked list Last node link is connected to the first. When insert/ delete at the last node Make sure to update the link field of the last node to the first node. Insert a node at the front of CLL 35 Insert a node at the end of CLL