Download presentation
Presentation is loading. Please wait.
Published bySharleen Sparks Modified over 9 years ago
1
Linked Lists 2014, Fall Pusan National University Ki-Joune Li
2
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
3
STEMPNU 3 Linked List : Example 전도연 9 전지현 7 김희선 6 이영애 8 Insert ( 선호도 순으로 ) 강혜정 5
4
STEMPNU 4 Linked List : Data Structures 전도연 9 Node DataLink 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: DataTypedata; Node *next; }; Class Node { friend class LinkedList; private: DataTypedata; Node *next; };
5
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 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 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 conditions
6
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 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 first Top Time Complexity: O(1)
7
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); }; Push DataType Stack::pop() { return list->deleteFirst(data); }; DataType Stack::pop() { return list->deleteFirst(data); }; Pop
8
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 last Class 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 ? Time Complexity: O(n)
9
STEMPNU 9 Circular List first 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 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 first O(1)
10
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) newNode
11
STEMPNU 11 Application of List: Polynomials 314 a = 3 x 14 + 2 x 8 + 3 b = 12 x 14 + 7 283N0 12147N0 CoefExp
12
STEMPNU 12 Adding Polynomials 314 a = 3 x 14 + 2 x 8 + 3 b = 12 x 14 + 7 x 5 283N0 12147N5 c = a + b 15142875 3N0
13
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; } };
14
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 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 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; } }; Destructor time consuming operations
15
STEMPNU 15 Available Empty Node List Maintaining Available Node List av first Class 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 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();
16
STEMPNU 16 Application of List : Sparse Matrix 00000-90 0000000 0000000 0-8000-40 140000012 013001100 6 7 7 0 2 1 12 0 2 -4 1 5 -9 1 1 14 6 0 13 5 2 -8 5 0 11 2 down rowcol right value down next right if ishead=YES if ishead=NO
17
STEMPNU Application of List : Sparse Matrix 17 Circular Linked List with header node 6 7 7
18
STEMPNU Application of List : Sparse Matrix 18 Circular Lined List for the header nodes (using right field) 6 7 7
19
STEMPNU Application of List : Sparse Matrix 19 Circular Linked List for the header nodes using down field 0 11 2
20
STEMPNU 20 List for Sparse Matrix : Insert 00000-90 0000000 0000000 0-80010-40 140000012 013001100 6 7 7 0 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
21
STEMPNU 21 Doubly Linked List 14800 0 Linked List : inefficient to go back Head Why not double links ?
22
STEMPNU 22 Doubly Linked List : Insertion and Deletion 14800 0 Insertion 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; } 14800 0 Deletion O (1)
23
STEMPNU 23 Representation of Polynomial P = x 10 y 3 z 2 + 2 x 8 y 3 z 2 + 3 x 8 y 2 z 2 + x 4 y 4 z + 6 x 3 y 4 z + 2 y z CoefExp_xExp_yExp_znext P = x 10 y 3 + 2 x 8 y 3 + 3 x 8 y 2 + x 4 y 4 + 6 x 3 y 4 + 2 y CoefExp_xExp_ynext Depends on the number of variables NOT a General Representation How to represent it in more general way ?
24
STEMPNU 24 A General Way to Represent Polynomial P = x 10 y 3 z 2 + 2 x 8 y 3 z 2 + 3 x 8 y 2 z 2 + x 4 y 4 z + 6 x 3 y 4 z + 2 y z P = ( (x 10 + 2 x 8 ) y 3 + 3 x 8 y 2 ) z 2 + ( ( x 4 + 6 x 3 ) y 4 + 2 y ) z P 1 (z) P 21 (y) P 22 (y) P 211 (x)P 212 (x)P2 2 (x) Nested PolynomialNested Linked List
25
STEMPNU 25 Generalized Lists Definition A = (a 0, a 1, a 2, a n-1 ) where a i is ATOMIC NODE or a LIST When a i 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
STEMPNU 26 Implementation of Generalized List Flag Node Next DLink 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: Booleanflag; Node *next; union { GenListNode*dlink; DataTypedata; }; Class GenListNode { friend class GenList; private: Booleanflag; Node *next; union { GenListNode*dlink; DataTypedata; };
27
STEMPNU 27 Generalized List : Example P = ( (x 10 + 2 x 8 ) y 3 + 3 x 8 y 2 ) z 2 + ( ( x 4 + 6 x 3 ) y 4 + 2 y ) z Nz L2 NyL3L2 NxN101N82 NxN83 L2 NyL4L1 NxN41N36 NxN00
28
STEMPNU 28 Generalized List : Example D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) NaL NbNc A LLBL NaLC
29
STEMPNU 29 Operation of Generalized List: Copy A=((a, b), ((c, d), e)) LL LNe A NaNb LcNd 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)
30
STEMPNU 30 Operation of Generalized List: Equal l=((a, b), ((c, d), e)) LL LNe l NaNb NcNd 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; } LL LNe m NaNb NcNf m=((a, b), ((c, f), e)) s t
31
STEMPNU 31 Shared Linked List: Reference Counter D=() A=(a, (b, c)) B=(A, A, ()) C=(a, C) NaL NbNc A LLBL NaLC Shared List Deletion of A with care NaL NbNc AN3 Head node with reference counter Delete list when reference counter = 0
32
STEMPNU 32 Example: Design Shape List Circle Rectangle Polygon Triangle Data: Closed Geometry P Total Area of P ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.