Download presentation
Presentation is loading. Please wait.
Published byLiani Rachman Modified over 6 years ago
1
2018, Fall Pusan National University Ki-Joune Li
Linked Lists 2018, Fall Pusan National University Ki-Joune Li
2
Problems of Array Lack of Dynamic Properties Linked List 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
3
Linked List : Example 전도연 9 전지현 7 김희선 6 강혜정 5 Insert (선호도 순으로) 이영애 8
4
Linked List : Data Structures
Node 전도연 9 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 Node { friend class LinkedList; private: DataType data; Node *next; };
5
Linked List : Operations
LinkedList::delete(Node *p,*q) /* delete node p after q */ /* to delete the first node, q is NULL */ { if(q==NULL) first=first->next; else q->next=p->next; delete p; }; Delete node LinkedList::insert(DataType data,Node *p) /* if p==NULL, insert it to the first node*/ { Node *newNode=new Node(data); if(first==NULL) { first=newNode; newNode->next=NULL } else { newNode->next=p->next; p->next=newNode; } }; Insert after p q p: node to delete Node *LinkedList::search(Condition condition) { for(Node *ptr=first;ptr!=NULL; ptr=ptr->next) { if(checkCondition(condition)==TRUE) return ptr; } return NULL; }; Search with conditions
6
Linked List : Operations
Iterator Move the pointer (or index to the next) Node *ptr=first; while(ptr!=NULL) { ... ptr=ptr->next; } for(Node *ptr=first;ptr!=NULL; ptr=ptr->next) { ... } int i=0 while(i<N) { ... i++; }
7
Stacks by Linked List Push(Linked List): Insert at first
Class Stack { private: Node *top; public: push(DataType data); DataType pop(); }; Stack::push(DataType data) { Node *newNode=new Node(data); newNode->next=temp; top=newNode; }; Push(Linked List): Insert at first DataType Stack::pop() { if(top==NULL) ListEmpty(); DataType tmpData=top->data; Node *tmpNode=top; top=top->next; delete tmpNode; return tmpData; }; Pop: Remove from the first Top Top Time Complexity: O(1)
8
Queues by Linked List Insert: Insert at last Time Complexity: O(n)
Class Queue { private: Node *first; public: insert(DataType data); DataType delete(); }; Insert: Insert at last 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; }; first Time Complexity: O(n) Why not pointer to the last ?
9
O(n) operations to reach to the last node
Circular List O(n) operations to reach to the last node first 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 last last 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 first O(1) O(1)
10
Circular List with Head Node
Empty List Head Head Empty node with ptr to 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)
11
Application of List: Polynomials
a = 3 x x 8 + 3 3 14 2 8 3 N b = 12 x 12 14 7 N Coef Exp
12
Adding Polynomials a = 3 x 14 + 2 x 8 + 3 b = 12 x 14 + 7 x 5
b = 12 x x 5 12 14 7 N 5 c = a + b 15 14 2 8 7 5 3 N
13
Erasing Linked List first Become garbage Class LinkedList {
private: Node *first; public: LinkedList(); ~LinkedList(); }; Become garbage void LinkedList::~LinkedList() { Node *ptr=first; while(ptr!=NULL) { previousPtr=ptr; ptr=ptr->next; delete ptr; } };
14
Maintaining Available Node List
Destructor void LinkedList::~LinkedList() { Node *ptr=first; while(ptr!=NULL) { previousPtr=ptr; ptr=ptr->next; delete ptr; } }; Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList(); }; time consuming 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; } }; Insert 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; }; Delete node
15
Maintaining Available Node List
first Class CircularList { private: Node *first; static Node *av=NULL; public: CircularList(); ~CircularList(); }; Available Empty Node List av For add operation Node *CircularList::getNode() { Node *newNode; if(av==NULL) newNode=new Node(); else { newNode=av; av=av->next; } return newNode; } 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();
16
Application of List : Sparse Matrix
-9 -8 -4 14 12 13 11 6 7 11 2 1 12 -4 5 -9 14 6 13 -8 row col down right 11 2 if ishead=NO value if ishead=YES down right next
17
Application of List : Sparse Matrix
Circular Linked List with header node 6 7
18
Application of List : Sparse Matrix
Circular Lined List for the header nodes (using right field) 6 7
19
Application of List : Sparse Matrix
Circular Linked List for the header nodes using down field 11 2
20
List for Sparse Matrix : Insert
-9 -8 10 -4 14 12 13 11 6 7 11 2 13 5 1 12 1 14 6 2 -4 1 2 10 2 -8 5 2 10 O (max{r,c}) 5 9 1
21
Doubly Linked List Linked List : inefficient to go back
Head Why not double links ? 14 8
22
Doubly Linked List : Insertion and Deletion
Insertion x 14 8 p 20 void DoublyLinkedList::Insert(DblNode *p,*x) { p->leftLink=x; p->rightLink=x->rightLink; x->rightLink->leftLink=p; x->rightLink=p; } O (1) Deletion 14 8
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 Coef Exp_x Exp_y next P = x10 y3 + 2 x8 y3 + 3 x8 y2 + x4 y4 + 6 x3 y4 + 2 y Depends on the number of variables NOT a General Representation How to represent it in more general way ?
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 P21(y) P22(y) P211(x) P212(x) P22 (x) P = ( (x x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z P1(z) Nested Polynomial Nested Linked List
25
Generalized Lists Definition Linear List Example
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
26
Implementation of Generalized List
Data of Node Node Node / List Flag Data Next Class GenListNode { friend class GenList; private: Boolean flag; Node *next; union { GenListNode *dlink; DataType data; }; public: void copy(const GenList&) Boolean isEqual(GenList&)... DLink Pointer to List Class GenList { private: GenListNode *first; public: void copy(const GenList&) Boolean isEqual(GenList&)... };
27
Generalized List : Example
2 H y 3 x N 10 1 8 L 2 H y 4 1 x N 3 6 P = ( (x x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z H: Head Node L: Linked List N: Node H z
28
Generalized List : Example
D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) A N a L N b N c B L L L C N a L
29
Operation of Generalized List: Copy
A=((a, b), ((c, d), e)) A L L B=((a, b), ((c, d), e)) N a N b L N e void GenList:copy(const GenList& l) { first=l.copy(l.first); } L c N d q B L N a b
30
Operation of Generalized List: Copy
A=((a, b), ((c, d), e)) A L L N a N b L N e void GenList:copy(const GenList& l) { first=l.copy(l.first); } L c N d GenListNode *GenList:copy(const GenListNode *p) { GenListNode *q=NULL; if(p!=NULL) { q=new GenListNode; q->flag=p->flag; if(p->flag==LIST) q->dlink=p->copy(p->dlink); else q->data=p->data; q->next=p->copy(p->next); } return q; One visit per node : Linear Scan : O(m ) Not Circular like C=(a, C)
31
Operation of Generalized List: Equal
l=((a, b), ((c, d), e)) l L L m=((a, b), ((c, f), e)) 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; N a N b L N e N c N d t m L L N a N b L N e N c N f
32
Shared Linked List: Reference Counter
D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) Deletion of A with care Shared List Head node with reference counter A N a L N b N c N a L b c A 3 B L L L Delete list when reference counter = 0 C N a L
33
Example: Design Shape List
Data: Closed Geometry P Total Area of P ? Circle Rectangle Polygon Triangle
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.