Download presentation
Presentation is loading. Please wait.
Published byGrace Cobb Modified over 9 years ago
1
Ceng-112 Data Structures ISerap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists
2
Ceng-112 Data Structures ISerap ATAY, Ph. D. 2 Destroy List 1.Deletes any nodes still in the list, releases their memory 2.Releases the head node’s memory. 3.Returns null pointer indicating that the list no longer exist.
3
Ceng-112 Data Structures ISerap ATAY, Ph. D. 3 Destroy List algorithm destroyList (ref pList ) Deletes all data in list and then deletes head structure. PRE pList is a pointer to a valid list head structure POST All data and head structure deleted RETURN null head pointer 1 loop (pList count not zero) 1 dltPtr = pList head 2 pList head = dltPtr link 3 pList count = pList count – 1 4 release (dltPtr) No data left in list 2 release(pList) 3 retun null pointer
4
Ceng-112 Data Structures ISerap ATAY, Ph. D. 4 Figure 3-19 Linear List Applications Append Linked Lists
5
Ceng-112 Data Structures ISerap ATAY, Ph. D. 5 Figure 3-20 Linear List Applications Array of Linked Lists Each linked list represents one row in the array. The nodes in the linked list represents the columns.
6
Ceng-112 Data Structures ISerap ATAY, Ph. D. 6 Complex Linked List Structure Linked list structural variations known as a header node. We have three useful linked list variations: 1.Circularly linked list 2.Doubly linked list 3.Multilinked list
7
Ceng-112 Data Structures ISerap ATAY, Ph. D. 7 Figure 3-21 Complex Linked List Structure Header Nodes A header node structure; Contains the meta data Shares a pointer structure with data nodes A list pointer points the head node. A null list is defined as a list with only a header node.
8
Ceng-112 Data Structures ISerap ATAY, Ph. D. 8 Figure 3-22 Complex Linked List Structure Circularly Linked List The link in the last node points to the firs node. It could also point to the header node. Insertion and deletion are same with singly linked list except that the last node points to the first node. Search problem: we save the starting node’s address and stop when we have circled around. loop (target not equal to pLoc data.key AND pLoc link not equal to startAddress)
9
Ceng-112 Data Structures ISerap ATAY, Ph. D. 9 Figure 3-23 Complex Linked List Structure Doubly Linked List It is a linked list structure in which each node has a pointer to both its successor and its predecessor. A backward pointer to its predecessor. A forward pointer to its successor. Another variation on the doubly linked list is the “doubly linked circularly linked list”.
10
Ceng-112 Data Structures ISerap ATAY, Ph. D. 10 Complex Linked List Structure Doubly Linked List – Head node Structure node shared structure back fore variant structure metadata count pos rear user data key... end node
11
Ceng-112 Data Structures ISerap ATAY, Ph. D. 11 Figure 3-24 Doubly Linked List -Insertion
12
Ceng-112 Data Structures ISerap ATAY, Ph. D. 12 Doubly Linked List -Insertion algorithm insertDbl (val pList, val dataIn ) This algorithm inserts data into a doubly linked list. PRE pList is a pointer to a valid doubly linked list. dataIn contains the data to be inserted. POST The data have been inserted in sequence RETURN 0: failed– dynamic memory overflow 1: successful 2: failed- dublicate key presented
13
Ceng-112 Data Structures ISerap ATAY, Ph. D. 13 Doubly Linked List -Insertion 1 if (full list) 1 return (0) Locate insertion position in list 2 found = searchList(pList, pPre, pSucc, dataIn.key) 3 if (found false) 1 allocate (pNew) 2 pNew userData = dataIn 3 pNew back = pPre 4 pNew fore = pPre fore Test for insert into null list or at end of list 5 if (pPre fore null) –Inserting at end of list, set rear pointer 1 pList metadata.rear = pNew 6 else – Inserting in middle of list, point successor to new 1 pSucc back = pNew 7 pPre fore = pNew 8 pList metadata.count = pList metadata.count +1 9 return 1 Dublicate data. Key already exist. 4 return 2 end insertDbl
14
Ceng-112 Data Structures ISerap ATAY, Ph. D. 14 Figure 3-25 Doubly Linked List -Deletion
15
Ceng-112 Data Structures ISerap ATAY, Ph. D. 15 Doubly Linked List -Deletion algorithm deleteDbl (val pList, val pDlt ) This algorithm deletes a node from a doubly linked list. PRE pList is a pointer to a valid doubly linked list. pDlt is a pointer to the node to be deleted. POST node deleted
16
Ceng-112 Data Structures ISerap ATAY, Ph. D. 16 Doubly Linked List -Deletion 1 if (pDlt null) 1 abort 2 pList metadata.count =pList metadata.count – 1 3 pPred = pDlt back 4 pSucc = pDlt fore 5 pPred fore = pSucc 6 if (pSucc not null) 1 pSucc back = pPred 7 recycled pDlt 8 return 2 end deleteDbl
17
Ceng-112 Data Structures ISerap ATAY, Ph. D. 17 Figure 3-26 Complex Linked List Structure Multilinked List It is a list with two or more logical key sequences. Data can be processed in multible sequence. The data are not replicated.
18
Ceng-112 Data Structures ISerap ATAY, Ph. D. 18 HW-4 Create a doubly linked list structure to process the student information. The student number should be the key info. Follow the all remarks and instructions in your text book pages from 91 to 97 and build the your C codes to satisfy the defined requirements under a menu control such as: –Create link list –Destroy linked list –Add node –Delete node –Search node –Display list (traverse list) Load your HW-4 to FTP site until 05 Apr. 07 at 17:00.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.