1 / 32

Linked Lists

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)

Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  3. 전도연 9 전지현 7 김희선 이영애 강혜정 6 8 5 Linked List : Example Insert (선호도 순으로)

  4. 전도연 9 Linked List : Data Structures 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 Node { friend class LinkedList; private:DataType data; Node *next; };

  5. p: node to delete q Delete node 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; }; 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; } }; 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; }; Linked List : Operations

  6. Push(Linked List): Insert at first LinkedList::insertFirst(DataType data) { Node *newNode=new Node(data); newNode->next=temp; first=newNode;}; 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;}; Stacks by Linked List Class Stack { private: LinkedList *list; public: push(DataType data);DataType pop();}; Top Top Time Complexity: O(1)

  7. Push Pop Stack::push(DataType data) { list->insertFirst(data); }; DataType Stack::pop() { return list->deleteFirst(data); }; Stacks by Linked List Class Stack { private: LinkedList *list; public: push(DataType data);DataType pop();};

  8. Queues by Linked List Class Queue { private: Node *list; 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. Insert: Insert at 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;}; 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; retun tmpData;}; Circular List O(n) operations to reach to the last node first last O(1) O(1)

  10. Insert: Insert at first (with head node) CircularList::insert(DataType data) { Node *newNode=new Node(data); newNode->next=head->next; head->next=newNode;}; Circular List with Head Node Empty List Head Head Empty node with ptr to next newNode

  11. 3 2 3 Coef 12 7 14 0 8 0 14 Exp N N Application of List: Polynomials a = 3 x 14 + 2 x8 + 3 b = 12 x 14 + 7

  12. 7 3 3 7 2 3 2 12 15 14 8 0 14 5 14 8 5 0 N N N Adding Polynomials a = 3 x 14 + 2 x8 + 3 b = 12 x 14 + 7 x5 c = a + b

  13. Erasing Linked List first Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList();}; Become garbage void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; }};

  14. Delete node 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;}; 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; } }; Maintaining Available Node List Destructor void LinkedList::~LinkedList() { Node *ptr=first; while(first!=NULL) { ptr=first->next; delete first; }}; Class LinkedList { private: Node *first; public: LinkedList(); ~LinkedList();}; time consuming operations

  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. 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 5 2 0 6 0 1 1 2 0 1 0 2 2 6 1 5 5 7 0 -9 0 0 0 0 0 -4 12 14 11 7 13 -8 -9 11 Application of List : Sparse Matrix row col down right if ishead=NO value if ishead=YES down right next

  17. 6 7 7 Application of List : Sparse Matrix Circular Linked List with header node

  18. 6 7 7 Application of List : Sparse Matrix Circular Lined List for the header nodes (using right field)

  19. 0 2 11 Application of List : Sparse Matrix Circular Linked List for the header nodes using down field

  20. 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 2 1 5 0 1 2 2 2 6 0 6 5 5 2 1 1 2 2 7 0 0 -9 0 0 0 0 0 12 10 -8 13 14 7 9 11 -4 10 List for Sparse Matrix : Insert O (max{r,c})

  21. 14 8 0 0 0 Doubly Linked List Linked List : inefficient to go back Head Why not double links ?

  22. 20 14 8 0 0 0 0 0 8 14 0 Doubly Linked List : Insertion and Deletion Insertion x p void DoublyLinkedList::Insert(DblNode *p,*x) { p->leftLink=x; p->rightLink=x->rightLink; x->rightLink->leftLink=p; x->rightLink=p; } O (1) Deletion

  23. Coef Exp_x Exp_y Exp_z next Coef Exp_x Exp_y next 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 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. P1(z) P211(x) P212(x) P22 (x) P22(y) P21(y) 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 Nested Polynomial Nested Linked List

  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

  26. Class GenListNode { friend class GenList; private: Boolean flag; Node *next; union { GenListNode *dlink; DataType data; }; }; Implementation of Generalized List Data of Node Node Node / List Flag Data Next DLink Pointer to List Class GenList { private: GenListNode *first; public: ...};

  27. N L N N L N N N L L N N N N N L L N N x 3 1 y 2 z y 0 1 x x 6 x 4 2 0 4 8 1 2 8 3 3 2 10 Generalized List : Example P = ( (x10 + 2 x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) 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 N a N b L N e void GenList:copy(const GenList& l) { first=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->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)

  30. Operation of Generalized List: Equal s 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

  31. Head node with reference counter Shared List A N 3 N a L N b N c Shared Linked List: Reference Counter D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) Deletion of A with care A N a L N b N c B L L L Delete list when reference counter = 0 C N a L

  32. P Data: Closed Geometry Circle Rectangle Polygon Triangle Example: Design Shape List Total Area of P ?

More Related