Linked Lists 2015, Fall Pusan National University Ki-Joune Li PNU STEM 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 2 PNU STEM Linked List : Example 전도연 9 전지현 7 김희선 6 강혜정 5 Insert (선호도 순으로) 이영애 8 3 PNU STEM 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; }; 4 PNU STEM Linked List : Operations Insert after p Delete node 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::delete(Node *p,*q) { // delete node p after q if(q==NULL) first=first->next; else q->next=p->next; delete p; }; q p: node to delete Search with conditions Node *LinkedList::search(Condition condition) { for(Node *ptr=first;ptr!=NULL; ptr=ptr->next) { if(checkCondition(condition)==TRUE) return ptr; } return NULL; }; 5 PNU STEM Stacks by Linked List Class Stack { private: LinkedList *list; public: push(DataType data); DataType pop(); }; Pop: Remove from the first DataType LinkedList::deleteFirst() { if(first==NULL) ListEmpty(); DataType tmpData=first->data; Node *tmpNode=first; first=first->next; delete tmpNode; return tmpData; }; Push(Linked List): Insert at first LinkedList::insertFirst(DataType data) { Node *newNode=new Node(data); newNode->next=temp; first=newNode; }; Top Top Time Complexity: O(1) 6 PNU STEM Stacks by Linked List Class Stack { private: LinkedList *list; public: push(DataType data); DataType pop(); }; Push Stack::push(DataType data) { list->insertFirst(data); }; Pop DataType Stack::pop() { return list->deleteFirst(data); }; 7 PNU STEM Queues by Linked List Class Queue { private: Node *list; public: insert(DataType data); DataType delete(); }; first 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; Time Complexity: O(n) } q->next=newNode; }; Why not pointer to the last ? 8 PNU STEM Circular List O(n) operations to reach to the last node first Insert: Insert at last last Delete : delete the first DataType CircularList::delete() { if(last==NULL) QueueEmpty(); DataType tmpData=last->data; Node *first=last->next; last->next=first->next; delete first; O(1) retun tmpData; }; CircularList::insert(DataType data) { Node *newNode=new Node(data); if(last==NULL) { last=newNode; last->next=last; return; } newNode->next=last->next; O(1) last->next=newNode; }; 9 PNU STEM Circular List with Head Node Empty List Head Empty node with ptr to next Head newNode Insert: Insert at first (with head node) CircularList::insert(DataType data) { Node *newNode=new Node(data); newNode->next=head->next; head->next=newNode; }; 10 PNU STEM Application of List: Polynomials a = 3 x 14 + 2 x 8 + 3 3 14 2 8 b = 12 x 14 + 7 12 14 7 0 Coef 3 0 N N Exp 11 PNU STEM Adding Polynomials a = 3 x 14 + 2 x 8 + 3 3 14 2 8 b = 12 x 14 + 7 x 5 12 14 7 5 c=a+b 15 14 2 8 3 0 7 5 3 0 N N N 12 PNU STEM Erasing Linked List 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; } }; 13 PNU STEM Maintaining Available Node List Destructor Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList(); }; void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; } }; Insert after p 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; } }; Delete node LinkedList::delete(Node *p,*q) { // delete node p after q if(q==NULL) first=first->next; else q->next=p->next; delete p; }; time consuming operations 14 PNU STEM Maintaining Available Node List Class CircularList { private: Node *first; static Node *av=NULL; public: CircularList(); ~CircularList(); }; first av Available Empty Node List 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(); 15 PNU STEM Application of List : Sparse Matrix 0 0 11 0 0 13 0 12 0 0 0 0 0 14 0 -4 0 0 0 -8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -9 0 0 0 0 0 row col down right 0 6 7 7 02 11 05 13 10 12 16 14 21 -4 25 -8 2 11 if ishead=NO if ishead=YES down value right next 51 -9 16 PNU STEM Application of List : Sparse Matrix Circular Linked List with header node 6 7 7 17 PNU STEM Application of List : Sparse Matrix Circular Lined List for the header nodes (using right field) 6 7 7 18 PNU STEM Application of List : Sparse Matrix Circular Linked List for the header nodes using down field 0 2 11 19 PNU STEM List for Sparse Matrix : Insert 0 0 11 0 0 13 0 12 0 0 0 0 0 14 0 -4 10 0 0 -8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -9 0 0 0 0 0 6 7 7 02 11 05 13 10 12 16 14 21 -4 22 10 25 -8 22 10 O (max{r,c}) 51 9 20 PNU STEM Doubly Linked List Linked List : inefficient to go back Head Why not double links ? 0 0 14 8 0 21 PNU STEM Doubly Linked List : Insertion and Deletion 0 Insertion x 0 14 8 0 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 0 0 14 8 0 22 PNU STEM 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 ? 23 PNU STEM 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 P211(x) P212(x) P22 (x) P1(z) P22(y) P21(y) Nested Polynomial Nested Linked List 24 PNU STEM 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 Reusability of Generalized List : Shared List D=() : NULL A=(a, (b, c)) : Finite B=(A, A, ()) = ((a, (b, c)), (a, (b, c)), ()) C=(a, C) = (a, (a, (a, …)))) : Infinite 25 PNU STEM Implementation of Generalized List Node / List Data of Node Node Flag Data DLink Next Pointer to List Class GenList { private: GenListNode *first; public: ... }; Class GenListNode { friend class GenList; private: Boolean flag; Node *next; union { GenListNode *dlink; DataType data; }; }; 26 PNU STEM Generalized List : Example P = ( (x10 + 2 x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z N z L N y 2 L L 3 L 2 N y 2 L N x N x N x N 3 8 N 1 10 N 2 8 4 L 1 N x N 0 0 N 1 4 N 6 3 27 PNU STEM Generalized List : Example D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) A N a L N b B L L C N a L N c L 28 PNU STEM Operation of Generalized List: Copy A=((a, b), ((c, d), e)) A L N a void GenList:copy(const GenList& l) { first=copy(l.first); } One visit per node : Linear Scan : O(m ) Not Circular like C=(a, C) L N b L N e 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->flat==NODE) q->data=p->data; else q->dlink=copy(p->dlink); q->next=copy(p->next); } return q; } 29 PNU STEM Operation of Generalized List: Equal s l=((a, b), ((c, d), e)) m=((a, b), ((c, f), e)) l L 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 L N b L N e N c N d t m L N a L N b L N e N c N f 30 PNU STEM Shared Linked List: Reference Counter D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) Deletion of A with care Shared List A N a Head node with reference counter L N b N c A B L L C N a L L N 3 N a L N b N c Delete list when reference counter = 0 31 PNU STEM Example: Design Shape List P Total Area of P ? Data: Closed Geometry Circle Rectangle Polygon Triangle 32