Transcript Document
Linear
Lists
Chapter 3 – Part 2
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
1
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.
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
2
Destroy List
algorithm destroyList (ref pList <head pointer>)
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 (pListcount not zero)
1 dltPtr = pListhead
2 pListhead = dltPtrlink
3 pListcount = pListcount – 1
4 release (dltPtr)
No data left in list
2 release(pList)
3 retun null pointer
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
3
Linear List Applications
Append Linked Lists
Figure 3-19
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
4
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.
Figure 3-20
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
5
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
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
6
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.
Figure 3-21
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
7
•
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 pLocdata.key
AND pLoclink not equal to startAddress)
Figure 3-22
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
8
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”.
Figure 3-23
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
9
Complex Linked List Structure
Doubly Linked List – Head node Structure
node
shared structure
back <pointer>
fore <pointer>
variant structure
metadata
count <integer>
pos
<pointer>
rear <pointer>
user data
key
<key type>
...
end node
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
10
Doubly Linked List -Insertion
Figure 3-24
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
11
Doubly Linked List -Insertion
algorithm insertDbl (val pList <node pointer>, val dataIn <dataType>)
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 <integer> 0: failed– dynamic memory overflow
1: successful
2: failed- dublicate key presented
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
12
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 pNewuserData = dataIn
3 pNewback = pPre
4 pNewfore = pPrefore
Test for insert into null list or at end of list
5 if (pPrefore null) –Inserting at end of list, set rear pointer
1 pListmetadata.rear = pNew
6 else – Inserting in middle of list, point successor to new
1 pSuccback = pNew
7 pPrefore = pNew
8 pListmetadata.count = pListmetadata.count +1
9 return 1
Dublicate data. Key already exist.
4 return 2
end insertDbl
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
13
Doubly Linked List -Deletion
Figure 3-25
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
14
Doubly Linked List -Deletion
algorithm deleteDbl ( val pList <node pointer>,
val pDlt <node pointer>)
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
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
15
Doubly Linked List -Deletion
1 if (pDlt null)
1 abort
2 pListmetadata.count =pListmetadata.count – 1
3 pPred = pDltback
4 pSucc = pDltfore
5 pPredfore = pSucc
6 if (pSucc not null)
1 pSuccback = pPred
7 recycled pDlt
8 return 2
end deleteDbl
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
16
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.
Figure 3-26
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
17
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.
Ceng-112 Data Structures I
Serap ATAY, Ph. D.
18