linked lists

32
Linked Lists 2014, Fall Pusan National University Ki-Joune Li

Upload: hilel-ramsey

Post on 02-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Linked Lists. 2014, Fall Pusan National University Ki-Joune Li. Problems of Array. Lack of Dynamic Properties Insertion Deletion Moving elements: expensive operation Max. Size of Array Linked List More dynamic data structure than Array No subscript (or index) - PowerPoint PPT Presentation

TRANSCRIPT

Linked Lists

2014, FallPusan National UniversityKi-Joune Li

STEMPNU

2

Problems of Array

Lack of Dynamic Properties Insertion Deletion

Moving elements: expensive operation Max. Size of Array

Linked List More dynamic data structure than Array No subscript (or index)

Only Linear Search is possible

STEMPNU

3

Linked List : Example

전도연 9 전지현 7 김희선 6

이영애 8

Insert ( 선호도 순으로 )

강혜정 5

STEMPNU

4

Linked List : Data Structures

전도연 9

Node

Data

Link to the next node

Class LinkedList { private: Node *first; public: insert(DataType data,Node *q); insert(Node *p); delete(Node *p); Node *search(condition); };

Class LinkedList { private: Node *first; public: insert(DataType data,Node *q); insert(Node *p); delete(Node *p); Node *search(condition); };

Class Node { friend class LinkedList; private: DataType data; Node *next;};

Class Node { friend class LinkedList; private: DataType data; Node *next;};

STEMPNU

5

Linked List : Operations

LinkedList::insert(DataType data,Node *p) { Node *newNode=new Node(data); if(first==NULL) { first=newNode; newNode->next=NULL } else { newNode->next=p->next; p->next=newNode; }};

LinkedList::insert(DataType data,Node *p) { Node *newNode=new Node(data); if(first==NULL) { first=newNode; newNode->next=NULL } else { newNode->next=p->next; p->next=newNode; }};

Insert after pInsert after p

q p: node to delete

LinkedList::delete(Node *p,*q) {// delete node p after q if(q==NULL) first=first->next; else q->next=p->next; delete p; };

LinkedList::delete(Node *p,*q) {// delete node p after q if(q==NULL) first=first->next; else q->next=p->next; delete p; };

Delete nodeDelete node

Node *LinkedList::search(Condition condition) { for(Node *ptr=first;ptr!=NULL; ptr=ptr->next) { if(checkCondition(condition)==TRUE) return ptr; } return NULL;};

Node *LinkedList::search(Condition condition) { for(Node *ptr=first;ptr!=NULL; ptr=ptr->next) { if(checkCondition(condition)==TRUE) return ptr; } return NULL;};

Search with conditionsSearch with conditions

STEMPNU

6

Stacks by Linked List

LinkedList::insertFirst(DataType data) { Node *newNode=new Node(data); newNode->next=temp; first=newNode;};

LinkedList::insertFirst(DataType data) { Node *newNode=new Node(data); newNode->next=temp; first=newNode;};

Push(Linked List): Insert at firstPush(Linked List): Insert at first

Top

Class Stack { private: LinkedList *list; public: push(DataType data); DataType pop();};

Class Stack { private: LinkedList *list; public: push(DataType data); DataType pop();};

DataType LinkedList::deleteFirst() { if(first==NULL) ListEmpty(); DataType tmpData=first->data; Node *tmpNode=first; first=first->next; delete tmpNode; return tmpData;};

DataType LinkedList::deleteFirst() { if(first==NULL) ListEmpty(); DataType tmpData=first->data; Node *tmpNode=first; first=first->next; delete tmpNode; return tmpData;};

Pop: Remove from the firstPop: Remove from the first

Top

Time Complexity: O(1)Time Complexity: O(1)

STEMPNU

Stacks by Linked List

7

Class Stack { private: LinkedList *list; public: push(DataType data); DataType pop();};

Class Stack { private: LinkedList *list; public: push(DataType data); DataType pop();};

Stack::push(DataType data) { list->insertFirst(data);};

Stack::push(DataType data) { list->insertFirst(data);};

PushPush

DataType Stack::pop() { return list->deleteFirst(data);};

DataType Stack::pop() { return list->deleteFirst(data);};

PopPop

STEMPNU

8

Queues by Linked List

LinkedList::insert(DataType data) { Node *newNode=new Node(data); if(first==NULL) { first=newNode; return; } Node *p=first; Node *q; while(p!=NULL) { q=p; p=p->next; } q->next=newNode;};

LinkedList::insert(DataType data) { Node *newNode=new Node(data); if(first==NULL) { first=newNode; return; } Node *p=first; Node *q; while(p!=NULL) { q=p; p=p->next; } q->next=newNode;};

Insert: Insert at lastInsert: Insert at lastClass Queue { private: Node *list; public: insert(DataType data); DataType delete();};

Class Queue { private: Node *list; public: insert(DataType data); DataType delete();};

first

Why not pointer to the last ?Why not pointer to the last ?

Time Complexity: O(n)Time Complexity: O(n)

STEMPNU

9

Circular List

first

O(n) operations to reach to the last node

O(n) operations to reach to the last node

last

CircularList::insert(DataType data) { Node *newNode=new Node(data); if(last==NULL) { last=newNode; last->next=last; return; } newNode->next=last->next; last->next=newNode;};

CircularList::insert(DataType data) { Node *newNode=new Node(data); if(last==NULL) { last=newNode; last->next=last; return; } newNode->next=last->next; last->next=newNode;};

Insert: Insert at lastInsert: Insert at last

DataType CircularList::delete() { if(last==NULL) QueueEmpty(); DataType tmpData=last->data; Node *first=last->next; last->next=first->next; delete first; retun tmpData;};

DataType CircularList::delete() { if(last==NULL) QueueEmpty(); DataType tmpData=last->data; Node *first=last->next; last->next=first->next; delete first; retun tmpData;};

Delete : delete the firstDelete : delete the first

O(1)O(1)

O(1)O(1)

STEMPNU

10

Circular List with Head Node

Head

Empty node with ptr to next

Head

Empty List

CircularList::insert(DataType data) { Node *newNode=new Node(data); newNode->next=head->next; head->next=newNode;};

CircularList::insert(DataType data) { Node *newNode=new Node(data); newNode->next=head->next; head->next=newNode;};

Insert: Insert at first (with head node)Insert: Insert at first (with head node)newNode

STEMPNU

11

Application of List: Polynomials

3 14a = 3 x 14 + 2 x 8 + 3

b = 12 x 14 + 7

2 8 3 N0

12 14 7 N0

Coef Exp

STEMPNU

12

Adding Polynomials

3 14a = 3 x 14 + 2 x 8 + 3

b = 12 x 14 + 7 x 5

2 8 3 N0

12 14 7 N5

c = a + b 15 14 2 8 7 5

3 N0

STEMPNU

13

Erasing Linked List

Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList();};

Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList();};

first

Become garbage

void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; }};

void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; }};

STEMPNU

14

Maintaining Available Node List

LinkedList::insert(DataType data,Node *p) { Node *newNode=new Node(data); if(first==NULL) { first=newNode; newNode->next=NULL } else { newNode->next=p->next; p->next=newNode; }};

LinkedList::insert(DataType data,Node *p) { Node *newNode=new Node(data); if(first==NULL) { first=newNode; newNode->next=NULL } else { newNode->next=p->next; p->next=newNode; }};

Insert after pInsert after p

LinkedList::delete(Node *p,*q) {// delete node p after q if(q==NULL) first=first->next; else q->next=p->next; delete p; };

LinkedList::delete(Node *p,*q) {// delete node p after q if(q==NULL) first=first->next; else q->next=p->next; delete p; };

Delete nodeDelete node

Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList();};

Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList();};

void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; }};

void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; }};

DestructorDestructor

time consuming operations

STEMPNU

15

Available EmptyNode List

Maintaining Available Node List

av

firstClass CircularList {private: Node *first; static Node *av=NULL;public: CircularList(); ~CircularList();};

Class CircularList {private: Node *first; static Node *av=NULL;public: CircularList(); ~CircularList();};

Node *CircularList::getNode() { Node *newNode; if(av==NULL) newNode=new Node(); else { newNode=av; av=av->next; } return newNode;}

Node *CircularList::getNode() { Node *newNode; if(av==NULL) newNode=new Node(); else { newNode=av; av=av->next; } return newNode;}

For add operationFor add operation

void CircularList::~CircularList() { if(first!=NULL) { Node *second=first->next; first->next=av; first=NULL; av=second; } }

void CircularList::~CircularList() { if(first!=NULL) { Node *second=first->next; first->next=av; first=NULL; av=second; } }

replace Node *newNode=new Node(data); Node *newNode=getNode();

STEMPNU

16

Application of List : Sparse Matrix

00000-90

0000000

0000000

0-8000-40

140000012

013001100 6

7

7

0

11

2

1

12

0

2

-4

1

5

-9

1

1

14

6

0

13

5

2

-8

5

0

11

2

downrow col

right

value

down

next

right

if ishead=YESif ishead=NO

STEMPNU

Application of List : Sparse Matrix

17

Circular Linked List with header node

6

7

7

STEMPNU

Application of List : Sparse Matrix

18

Circular Lined List for the header nodes (using right field)

6

7

7

STEMPNU

Application of List : Sparse Matrix

19

Circular Linked List for the header nodes using down field

0

11

2

STEMPNU

20

List for Sparse Matrix : Insert

00000-90

0000000

0000000

0-80010-40

140000012

0130011006

7

7

0

11

2

1

12

0

2

-4

1

5

9

1

1

14

6

0

13

5

2

-8

52

10

2

O (max{r,c})

2

10

2

STEMPNU

21

Doubly Linked List

14 8 00

0

Linked List : inefficient to go back

Head

Why not double links ?

STEMPNU

22

Doubly Linked List : Insertion and Deletion

14 8 00

0Insertion

x

20

p

void DoublyLinkedList::Insert(DblNode *p,*x) { p->leftLink=x; p->rightLink=x->rightLink; x->rightLink->leftLink=p; x->rightLink=p; }

void DoublyLinkedList::Insert(DblNode *p,*x) { p->leftLink=x; p->rightLink=x->rightLink; x->rightLink->leftLink=p; x->rightLink=p; }

14 8 00

0

DeletionO (1)

STEMPNU

23

Representation of Polynomial

P = x10 y3 z2 + 2 x8 y3 z2 + 3 x8 y2 z2 + x4 y4 z + 6 x3 y4 z + 2 y z

Coef Exp_x Exp_y Exp_z next

P = x10 y3 + 2 x8 y3 + 3 x8 y2 + x4 y4 + 6 x3 y4 + 2 y

Coef Exp_x Exp_y next

Depends on the number of variables

NOT a General Representation

How to represent it in more general way ?

STEMPNU

24

A General Way to Represent Polynomial

P = x10 y3 z2 + 2 x8 y3 z2 + 3 x8 y2 z2 + x4 y4 z + 6 x3 y4 z + 2 y z

P = ( (x10 + 2 x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z

P1(z)

P21(y)

P22(y)P211(x) P212(x) P22 (x)

Nested Polynomial Nested Linked List

STEMPNU

25

Generalized Lists

Definition A = (a0, a1, a2, an-1) where ai is ATOMIC NODE or a LIST

When ai is a list, it is called SUBLIST.

Linear List

Example D=() : NULL A=(a, (b, c)) : Finite B=(A, A, ()) = ((a, (b, c)), (a, (b, c)), ()) C=(a, C) = (a, (a, (a, …)))) : Infinite

Reusability of Generalized List : Shared List

STEMPNU

26

Implementation of Generalized List

Flag

Node

NextDLink

Data

Node / List

Pointer to List

Data of Node

Class GenList { private: GenListNode *first; public: ...};

Class GenList { private: GenListNode *first; public: ...};

Class GenListNode { friend class GenList; private: Boolean flag; Node *next; union { GenListNode*dlink; DataType data; };};

Class GenListNode { friend class GenList; private: Boolean flag; Node *next; union { GenListNode*dlink; DataType data; };};

STEMPNU

27

Generalized List : Example

P = ( (x10 + 2 x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z

N z L 2

N y L 3 L 2

N x N 101 N 82

N x N 83

L 2

N y L 4 L 1

N x N 41 N 36

N x N 00

STEMPNU

28

Generalized List : Example

D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C)

N a L

N b N c

A

L LB L

N a LC

STEMPNU

29

Operation of Generalized List: Copy

A=((a, b), ((c, d), e)) L L

L N e

A

N a N b

L c N d

void GenList:copy(const GenList& l) { first=copy(l.first);}

void GenList:copy(const GenList& l) { first=copy(l.first);}

GenListNode *GenList:copy(const GenListNode *p) { GenListNode *q=NULL; if(p!=NULL) { q=new GenListNode; q->flag=p->flag; if(p->flat==NODE) q->data=p->data; else q->dlink=copy(p->dlink); q->next=copy(p->next); } return q;}

GenListNode *GenList:copy(const GenListNode *p) { GenListNode *q=NULL; if(p!=NULL) { q=new GenListNode; q->flag=p->flag; if(p->flat==NODE) q->data=p->data; else q->dlink=copy(p->dlink); q->next=copy(p->next); } return q;}

One visit per node : Linear Scan : O(m )

Not Circular like C=(a, C)

STEMPNU

30

Operation of Generalized List: Equal

l=((a, b), ((c, d), e))

L L

L N e

l

N a N b

N c N d

int operator==(const GenList& l,m) { return equal(l.first,m.first);}

int equal(GenListNode *s, *t) { x=FALSE; if(s and t are null), return TRUE; if(s and t are not null), { if(s and p are node) { if(s->data==t->data) x=TRUE; else x=FALSE; } else x=equal(s->dlink,t->dlink); } if(x==TRUE) return(s->next,t->next); return FALSE;}

int operator==(const GenList& l,m) { return equal(l.first,m.first);}

int equal(GenListNode *s, *t) { x=FALSE; if(s and t are null), return TRUE; if(s and t are not null), { if(s and p are node) { if(s->data==t->data) x=TRUE; else x=FALSE; } else x=equal(s->dlink,t->dlink); } if(x==TRUE) return(s->next,t->next); return FALSE;}

L L

L N e

m

N a N b

N c N f

m=((a, b), ((c, f), e))

s

t

STEMPNU

31

Shared Linked List: Reference Counter

D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C)

N a L

N b N c

A

L LB L

N a LC

Shared List

Deletion of A with care

N a L

N b N c

A N 3

Head node with reference counter

Delete list when reference counter = 0

STEMPNU

32

Example: Design Shape List

Circle

Rectangle

Polygon

Triangle

Data: Closed Geometry

PTotal Area of P ?