chapter 3 – part 2

18
Ceng-112 Data Structures I Serap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists

Upload: ozzie

Post on 12-Jan-2016

26 views

Category:

Documents


3 download

DESCRIPTION

Linear Lists. Chapter 3 – Part 2. Deletes any nodes still in the list, releases their memory Releases the head node’s memory. Returns null pointer indicating that the list no longer exist. Destroy List. algorithm destroyList (ref pList ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 1

Chapter 3 – Part 2

LinearLists

Page 2: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap 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.

Page 3: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 3

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

Page 4: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 4

Figure 3-19

Linear List ApplicationsAppend Linked Lists

Page 5: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 5

Figure 3-20

Linear List ApplicationsArray of Linked Lists

Each linked list represents one row in the array.

The nodes in the linked list represents the columns.

Page 6: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap 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

Page 7: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 7

Figure 3-21

Complex Linked List StructureHeader Nodes

A header node structure;• Contains the meta data• Shares a pointer structure with data nodesA list pointer points the head node.A null list is defined as a list with only a header node.

Page 8: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 8

Figure 3-22

Complex Linked List StructureCircularly 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)

Page 9: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 9

Figure 3-23

Complex Linked List StructureDoubly 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”.

Page 10: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 10

Complex Linked List StructureDoubly Linked List – Head node Structure

nodeshared structure

back <pointer>fore <pointer>

variant structuremetadata

count <integer>pos <pointer>rear <pointer>

user datakey <key type>...

end node

Page 11: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 11

Figure 3-24

Doubly Linked List -Insertion

Page 12: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 12

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

Page 13: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 13

Doubly Linked List -Insertion1 if (full list)

1 return (0)Locate insertion position in list2 found = searchList(pList, pPre, pSucc, dataIn.key)3 if (found false)

1 allocate (pNew)2 pNewuserData = dataIn3 pNewback = pPre4 pNewfore = pPreforeTest for insert into null list or at end of list5 if (pPrefore null) –Inserting at end of list, set rear pointer

1 pListmetadata.rear = pNew6 else – Inserting in middle of list, point successor to new

1 pSuccback = pNew7 pPrefore = pNew8 pListmetadata.count = pListmetadata.count +19 return 1

Dublicate data. Key already exist.4 return 2end insertDbl

Page 14: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 14

Figure 3-25

Doubly Linked List -Deletion

Page 15: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 15

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

Page 16: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 16

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

Page 17: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 17

Figure 3-26

Complex Linked List StructureMultilinked List

• It is a list with two or more logical key sequences.

• Data can be processed in multible sequence.

• The data are not replicated.

Page 18: Chapter 3  – Part 2

Ceng-112 Data Structures I Serap ATAY, Ph. D. 18

HW-4Create 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.