1 data structures for media linked lists. 2 review: abstract data types review: abstract data types...

42
1 Data Structures for Data Structures for Media Media Linked Lists Linked Lists

Post on 21-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

11

Data Structures for MediaData Structures for MediaLinked ListsLinked Lists

Page 2: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

22

Review: Abstract Data TypesReview: Abstract Data Types Review: Pointers and ReferencesReview: Pointers and References Singly Linked ListSingly Linked List Circular ListsCircular Lists Doubly Linked ListsDoubly Linked Lists ApplicationsApplications

OutlineOutline

Page 3: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

33

We want to know whether two cups can hold the same We want to know whether two cups can hold the same amount of wateramount of water

What to do?What to do? Calculate the volume by mathematical formulasCalculate the volume by mathematical formulas Fill cup 1 by water, pour the water to cup 2, overflow? vice Fill cup 1 by water, pour the water to cup 2, overflow? vice

versaversa ……

We only care about the result, not how to get the resultWe only care about the result, not how to get the result Abstract Data Type for cups!Abstract Data Type for cups!

Abstract Data TypeAbstract Data Type

Page 4: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

44

Abstract Data TypesAbstract Data Types

Some advanced data types are very useful.Some advanced data types are very useful.

People tend to create modules that group together the data types and People tend to create modules that group together the data types and the functions for handling these data types. (~ Modular Design)the functions for handling these data types. (~ Modular Design)

They form very useful toolkits for programmers.They form very useful toolkits for programmers.

/*matrix.h*/

public __gc class Matrix {void PrintMatrix(..);Matrix AddMatrix(.);…};

When one search for a “tool”, When one search for a “tool”, he looks at he looks at how / what*how / what* the the tools can do. i.e. He looks at the tools can do. i.e. He looks at the abstract data types (ADT). abstract data types (ADT). menu

He needs not know He needs not know how / what*how / what* the tools the tools have been implemented.have been implemented.

Page 5: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

55

e. g. The e. g. The setset ADT: ADT:Value:

A set of elementsCondition: elements are distinct.

Operations for Set *s:

1. void Add(ELEMENT e)postcondition: e will be added to *s

2. void Remove(ELEMENT e)precondition: e exists in *spostcondition: e will be removed from *s

3. int Size()postcondition: the no. of elements in *s will be returned.

• An ADT is a package of the declarations of a data type and the operations that are meaningful to it.

• We encapsulate the data type and the operations and hide them from the user.

• ADTs are implementation independent.

Abstract Data TypeAbstract Data Type

Page 6: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

66

Value:A set of elementsCondition: elements are distinct.

The set ADT:

1. 1. Definition of valuesDefinition of values

involves involves • definitiondefinition • conditioncondition (optional) (optional)

Consists of 2 parts:

Operations for Set *s:1. void Add(ELEMENT e)

postcondition: e will be added to *s2. void Remove(ELEMENT e)

precondition: e exists in *spostcondition: e will be removed from *s

3. int Size( )postcondition: the no. of elements in *s will be returned.

2. 2. Definition of operationsDefinition of operations

each operation involveseach operation involves• headerheader• preconditionprecondition (optional) (optional)• postconditionpostcondition

Abstract Data TypeAbstract Data Type

Page 7: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

77

Phase Description Result

Data AbstractionData AbstractionData abstraction is an important method in program designData abstraction is an important method in program design

Using the right data types => more efficient programUsing the right data types => more efficient programAlgorithms can be designed in terms of the abstract data type. Algorithms can be designed in terms of the abstract data type. We can defer decisions of data types until their uses are fully understooWe can defer decisions of data types until their uses are fully understood.d.

3. Implement the algorithm

Construct a mathematical model + its operations (ADT)

e.g., the model is a set, the problem is to list all elements. This phase is to express the algorithm in pseudocode, while figuring out the picture of the ADT.

Implementation-independent: Not to think whether to use array or linked list etc.. Not to design how to implement the operations.

A rough algorithm

The ADT

Data types and operations

Implement the actual data types and the operations for the ADT

1. Problem Formulation

2. Design the algorithm(Suppose no ADT available yet.)

Page 8: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

88

Value Type: variable contains data of that typeValue Type: variable contains data of that type int A; double B; …int A; double B; …

Reference Type: variable contains the addressReference Type: variable contains the address String * c; Object * d; …String * c; Object * d; …

PointersPointersint y=5;int y=5;int *ypointer;int *ypointer;ypointer=&y;ypointer=&y;

*ypointer=?*ypointer=?

Pointers and ReferencesPointers and References

c=1196 data1196

ypointer y=56000060000

dereferencing

ReferencesReferencesint count = 1;int count = 1;int &cRef = count;int &cRef = count;++cRef;++cRef;

What is the value of count?What is the value of count?

Page 9: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

99

String *text = “hello”;String *text = “hello”;If ( text->EndsWith(“llo”) )If ( text->EndsWith(“llo”) )

Console::WriteLine(“{0} ends with llo”, text);Console::WriteLine(“{0} ends with llo”, text);If ( (*text).EndWith(“llo”) )If ( (*text).EndWith(“llo”) )

Console::WriteLine(“{0} ends with llo”, text);Console::WriteLine(“{0} ends with llo”, text); Remember: Remember: ->-> for pointer; for pointer; .. for dereference for dereference

Call methods using pointersCall methods using pointers

Page 10: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1010

Pass by ValuePass by Valueint SquareByV(int int SquareByV(int

a)a){{

Return a * a;Return a * a;}}int _tmain()int _tmain(){{

int x =2;int x =2;SquareByV(x);SquareByV(x);

}}

Passing AugumentsPassing AugumentsPass by ReferencePass by Referencevoid SquareByP(int *cptr)void SquareByP(int *cptr){{

*cptr *= *cptr;*cptr *= *cptr;}}int _tmain()int _tmain(){{

int y =2;int y =2;SquareByP(&y);SquareByP(&y);

}}

Pass by ReferencePass by Referencevoid SquareByR(int &cRef)void SquareByR(int &cRef){{

cRef *=cRef;cRef *=cRef;}}int _tmain()int _tmain(){{

int z =2;int z =2;SquareByR(z);SquareByR(z);

}}int * p : (int *) p

*p: dereference (the data pointed by p)

*: multiplication

&a: the address of a

A[]: array variable is a pointer

Page 11: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1111

Question:

When to use Call-by-value?When to use Call-by-reference?

For security of data, we apply Call-by-reference only if necessary, e.g.

(1) we really want the called function to change the data

(2) the data is too big, i.e. Instead of copying a bulky data, e.g. a 100 byte record, (Call-by-value) we prefer to copy the memory address only (Call-by-reference)

Passing AugumentsPassing Auguments

Page 12: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1212

A Linear ListA Linear List (or (or a lista list, for short), for short)

is a sequence of n nodes xis a sequence of n nodes x11, x, x22, .., x, .., xnn whose essential structural properties whose essential structural properties involve only the involve only the relative positionsrelative positions between items as they appear between items as they appear in a linin a linee..

A list can be implemented asA list can be implemented as

A list can be sorted or unsorted.A list can be sorted or unsorted.

Arrays: statically allocated or dynamically allocated

Linked Lists: dynamically allocated

ListList

Page 13: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1313

Each item in the list is a Each item in the list is a nodenode

Linear StructureLinear Structure

Node can be stored in memory consecutively /or Node can be stored in memory consecutively /or not (logical order and physical order may be not (logical order and physical order may be different)different)

Singly Linked ListSingly Linked List

data next

a0 a1a2

first

a0 a1 a2 a3 a4 first

Page 14: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1414

// ListNode.h// ListNode.h

#pragma once#pragma once#include “stdlib.h”#include “stdlib.h”#using <mscorlib.dll>#using <mscorlib.dll>using namespace System;using namespace System;namespace LinkedListLibrarynamespace LinkedListLibrary{{

public __gc class ListNodepublic __gc class ListNode{{public:public:

ListNode( int );ListNode( int );ListNode( int, ListNode *);ListNode( int, ListNode *);__property ListNode *get_Next()__property ListNode *get_Next(){{

return next;return next;}}……

private:private:int data;int data;ListNode *next;ListNode *next;

};};}}

Singly Linked ListSingly Linked List// List.h// List.h#pragma once#pragma once#include “ListNode.h”#include “ListNode.h”namespace LinkedListLibrarynamespace LinkedListLibrary{{

public __gc class Listpublic __gc class List{{public:public:List( String * );List( String * );List();List();//various member functio//various member functio

nsnsprivate:private:

ListNode *first;ListNode *first;String *name;String *name;

}}}}

Page 15: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1515

Operations:Operations: InsertNodeInsertNode: insert a new node into a list: insert a new node into a list RemoveNodeRemoveNode: remove a node from a list: remove a node from a list SearchNodeSearchNode: search a node in a list: search a node in a list CountNodesCountNodes: compute the length of a list: compute the length of a list PrintContentPrintContent: print the content of a list: print the content of a list ……

Singly Linked ListSingly Linked List

Page 16: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1616

Count a linked list:Count a linked list:

Use a pointer p to traverse the list

first ……

p

//List.cpp//List.cppint List::Count()int List::Count(){{

int result=0;int result=0;ListNode *p=first;ListNode *p=first;while (p!=NULL)while (p!=NULL){{

result++;result++;p= p->getNext();p= p->getNext();

}}}}

Page 17: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1717

Print a linked list:Print a linked list:

//List.cpp//List.cppList::Print()List::Print(){{

ListNode *p=first;ListNode *p=first;while (p!=NULL)while (p!=NULL){{

Console::Write(S”{0}”, p->getData().ToString());Console::Write(S”{0}”, p->getData().ToString());p=p->getNext();p=p->getNext();

}}}}

Use a pointer p to traverse the list

first ……

p

Page 18: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1818

Search for a node:Search for a node:

//List.cpp//List.cppListNode* List::Search(int data)ListNode* List::Search(int data){{

ListNode *p=first;ListNode *p=first;while (p!=NULL)while (p!=NULL){{

if (p->getData()==data)if (p->getData()==data)return p;return p;

p=p->getNext();p=p->getNext();}}return NULL;return NULL;

}}

Use a pointer p to traverse the list

If found: return the pointer to the node, otherwise: return NULL.

first ……

p

Page 19: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

1919

Data comes one by one and we want to keep Data comes one by one and we want to keep them sorted, how?them sorted, how? Do search to locate the positionDo search to locate the position Insert the new nodeInsert the new node

Why is this correct?Why is this correct? InvariantInvariant

We will encounter three cases of insert positionWe will encounter three cases of insert position Insert before the first nodeInsert before the first node Insert in the middleInsert in the middle Insert at the end of the listInsert at the end of the list

One important case missing: Empty ListOne important case missing: Empty List

Insert a node:Insert a node:

Void List::InsertNode(ListNode* newnode)Void List::InsertNode(ListNode* newnode){{

if (first==NULL)if (first==NULL)first=newnode;first=newnode;

else if (newnode->getData()<first->getData() )else if (newnode->getData()<first->getData() )……

}}

Page 20: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2020

Case 1: Insert before the first nodeCase 1: Insert before the first node newnode->next=firstnewnode->next=first first=newnodefirst=newnode

Insert a node:Insert a node:

newnode

first ……

newnode

……

before insert after insert

first

Page 21: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2121

Void List::InsertNode(ListNode* newnode)Void List::InsertNode(ListNode* newnode){{

if (first==NULL)if (first==NULL)first=newnode;first=newnode;

else if (newnode->data<first->data)else if (newnode->data<first->data){{

newnode->next=first;newnode->next=first;first=newnode;first=newnode;

}}elseelse……

}}

Insert a node: Case 1Insert a node: Case 1In the following slides, we assume all the class member variables are pu

blic, so that the writings will be more clear

Remember that in real coding, you should use functions

getData(), setData(), getNext(), setNext() to access private class variables

Page 22: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2222

Case 2: Insert in the middleCase 2: Insert in the middle newnode->next=p->nextnewnode->next=p->next p->next=newnodep->next=newnode

Insert a node:Insert a node:

newnode

…… ……

p

newnode

…… ……

p

before insert after insert

Page 23: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2323

Case 3: Insert at the end of the listCase 3: Insert at the end of the list newnode->next=p->nextnewnode->next=p->next p->next=newnodep->next=newnode

Insert a node:Insert a node:

newnode

……

p

newnode

……

p

before insert after insert

Page 24: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2424

Void List::InsertNode(ListNode* newnode)Void List::InsertNode(ListNode* newnode){{

if (first==NULL)if (first==NULL)first=newnode;first=newnode;

else if (newnode->data<first->data)else if (newnode->data<first->data){{

newnode->next=first;newnode->next=first;first=newnode;first=newnode;

}}elseelse{{

ListNode* p=first;ListNode* p=first;while(p->next!=NULL && newnode->datwhile(p->next!=NULL && newnode->dat

a>p->next->data)a>p->next->data)p=p->next;p=p->next;

//p will stop at the last node or at the n//p will stop at the last node or at the nodeode

//after which we should insert the new //after which we should insert the new nodenode

//note that p->next might be NULL here//note that p->next might be NULL herenewnode->next=p->next;newnode->next=p->next;p->next=newnode;p->next=newnode;

}}}}

Insert a node: Complete SolutionInsert a node: Complete Solution

Page 25: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2525

Some data become useless and we want to Some data become useless and we want to remove them, how?remove them, how? Search the useless node by data valueSearch the useless node by data value Remove this nodeRemove this node

We will encounter two casesWe will encounter two cases Removing a node at the beginning of the listRemoving a node at the beginning of the list Removing a node not at the beginning of the listRemoving a node not at the beginning of the list

Remove a node:Remove a node:

Page 26: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2626

Case 1: Remove a node at the beginning of the Case 1: Remove a node at the beginning of the listlist Current status: the node pointed by “first” is unwantedCurrent status: the node pointed by “first” is unwanted The action we need: q=first; first=q->nextThe action we need: q=first; first=q->next

Remove a node:Remove a node:

before remove

after remove

first ……

q

……

q

first

Page 27: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2727

Case 2: Remove a node not at the beginning of the Case 2: Remove a node not at the beginning of the listlistCurrent status: q == p->next and the node pointed by q is Current status: q == p->next and the node pointed by q is

unwantedunwanted

The action we need: p->next=q->nextThe action we need: p->next=q->next

Remove a node:Remove a node:

before remove

after remove

…… ……

p q

…… ……

p q

Page 28: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2828

Void List::RemoveNode(ListNode* q)Void List::RemoveNode(ListNode* q){{

//remove at the beginning of a list//remove at the beginning of a listif (q==first)if (q==first)

first=first->next;first=first->next;else else //remove not at the beginning//remove not at the beginning{{

ListNode* p=first;ListNode* p=first;while(p->next!=q)while(p->next!=q)

p=p->next;p=p->next;p->next=q->next;p->next=q->next;

}}}}

Remove a node: Complete SolutionRemove a node: Complete Solution

Remember:

1. In real coding, you should also consider exceptions

2. Design of Linked List ADT is up to you, you can choose suitable functions to be member functions, e.g. InsertAtFront(), RemoveAtFront(). For each function, you can have your own way to implement it

Page 29: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

2929

So many cases with Insert and Remove So many cases with Insert and Remove operationsoperations

We want simpler implementations!We want simpler implementations!

One way to simplify:One way to simplify: keep an keep an extra nodeextra node at the front of the list at the front of the list

Dummy Header NodeDummy Header Node

……

Dummy Header node

first

Data nodes

The value of these nodes are our data

We don’t care about the value of this node.

Page 30: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3030

One case remaining for insertOne case remaining for insert

Dummy Header NodeDummy Header Node

newnode

…… ……

p

newnode

……

p

before insert after insert

Page 31: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3131

Void List::InsertNode(ListNode* newnode)Void List::InsertNode(ListNode* newnode){{

if (first==NULL)if (first==NULL)first=newnode;first=newnode;

else if (newnode->data<first->data)else if (newnode->data<first->data){{

newnode->next=first;newnode->next=first;first=newnode;first=newnode;

}}elseelse{{

ListNode* p=first;ListNode* p=first;while(p->next!=NULL && newnode->data>p->newhile(p->next!=NULL && newnode->data>p->ne

xt->data)xt->data)p=p->next;p=p->next;

//p will stop at the last node or at the node//p will stop at the last node or at the node//after which we should insert the new node//after which we should insert the new node//note that p->next might be NULL here//note that p->next might be NULL herenewnode->next=p->next;newnode->next=p->next;p->next=newnode;p->next=newnode;

}}}}

Insert a node: With Dummy HeaderInsert a node: With Dummy Header

Page 32: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3232

One case remaining for removeOne case remaining for remove

before remove

after remove

…… ……

p q

…… ……

p q

Dummy Header NodeDummy Header Node

Page 33: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3333

Void List::RemoveNode(ListNode* q)Void List::RemoveNode(ListNode* q){{

//remove at the beginning of a list//remove at the beginning of a listif (q==first)if (q==first)

first=first->next;first=first->next;else else //remove not at the beginning//remove not at the beginning{{

ListNode* p=first;ListNode* p=first;while(p->next!=q)while(p->next!=q)

p=p->next;p=p->next;p->next=q->next;p->next=q->next;

}}}}

Remove a node: With Dummy HeaderRemove a node: With Dummy Header

Page 34: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3434

Suppose we are at the node aSuppose we are at the node a44 and want to reach and want to reach aa11, how?, how?

If one extra link is allowed:If one extra link is allowed:

Circular ListsCircular Lists

a0 a1 a2 a3 a4 first

a0 a1 a2 a3 a4first

Page 35: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3535

Dummy header node can also be Dummy header node can also be added to make the implementation added to make the implementation easiereasier

Circular ListsCircular Lists

a0 a1 a2 a3

first

Dummy Header nodeWe don’t care about the value of this node.

first

Page 36: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3636

Problem with singly linked listProblem with singly linked list When at aWhen at a44, we want to get a, we want to get a33

When deleting node aWhen deleting node a33, we need to know the address of , we need to know the address of node anode a22

When at aWhen at a44, it is difficult to insert between a, it is difficult to insert between a33 and a and a44

If allowed to use more memory spaces, what to If allowed to use more memory spaces, what to do?do?

Doubly Linked ListDoubly Linked List

a0 a1 a2 a3 a4 first

Lnext Rnext

Data

Page 37: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3737

To insert a node after an existing node pointed by pTo insert a node after an existing node pointed by p

Doubly Linked ListDoubly Linked List

p

p

DoublyLinkedList::InsertNode(ListNode *p, ListNode *newnode)

{

newnode->Lnext=p;

newnode->Rnext=p->Rnext;

if(p->Rnext!=NULL) p->Rnext->Lnext=newnode;

p->Rnext=newnode;

}

newnode

newnode

Page 38: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3838

To delete a node, we only need to know a pointer pointing to To delete a node, we only need to know a pointer pointing to the nodethe node

Doubly Linked ListDoubly Linked List

p

p

DoublyLinkedList::RemoveNode(ListNode *p)

{

if(p->Lnext!=NULL) p->Lnext->Rnext=p->Rnext;

if(p->Rnext!=NULL) p->Rnext->Lnext=p->Lnext;

}

Page 39: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

3939

Doubly Linked List with Dummy Doubly Linked List with Dummy HeaderHeader

When emptyWhen empty

Doubly Circular Linked List?Doubly Circular Linked List?

Further ExtensionsFurther Extensions

Dummy Header node

Lnext Dummy Header Rnext

Page 40: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

4040

Linked allocation: Stores data as individual units and link them by pointers.

Advantages of linked allocation: Efficient use of memory

Facilitates data sharingNo need to pre-allocate a maximum size of required memoryNo vacant space left

Easy manipulation To delete or insert an itemTo join 2 lists togetherTo break one list into two lists

Variations Variable number of variable-size listsMulti-dimensional lists

(array of linked lists, linked list of linked lists, etc.)

Simple sequential operations (e.g. searching, updating) are fast

Disadvantages: Take up additional memory space for the links Accessing random parts of the list is slow. (need to walk sequentially)

Advantages / Disadvantages of Linked ListAdvantages / Disadvantages of Linked List

Page 41: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

4141

ApplicationsApplicationsRepresenting Convex PolygonsRepresenting Convex Polygons

A polygon is convex if it contains all the line segments connecting any pair of its points.

Polygon: A closed plane figure with n sides.

first

first

Page 42: 1 Data Structures for Media Linked Lists. 2 Review: Abstract Data Types Review: Abstract Data Types Review: Pointers and References Review: Pointers and

4242

Review: Abstract Data TypesReview: Abstract Data Types Review: Pointers and ReferencesReview: Pointers and References Singly Linked ListSingly Linked List Circular ListsCircular Lists Doubly Linked ListsDoubly Linked Lists Dummy HeaderDummy Header Applications: Representing PolygonsApplications: Representing Polygons

SummarySummary