2017, Fall Pusan National University Ki-Joune Li

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linear Lists – Linked List Representation
Data Structures ADT List
CSE Lecture 12 – Linked Lists …
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Foundation of Computing Systems Lecture 2 Linked Lists.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Chapter 4.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linked List Containers. Linked List Example Polynomials –Interested in representing and manipulating polynomials –Polynomial defined as: Y = coef_n *
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
Chap 4 Linked Lists. Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Chapter 4 (cont.) Additional Lists Operations Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Linked Lists 2014, Fall Pusan National University Ki-Joune Li.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
Linear Data Structures
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
UNIT-II Topics to be covered Singly linked list Circular linked list
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Copy Constructor / Destructors Stacks and Queues
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Lists CS 3358.
UNIT-3 LINKED LIST.
Linked list.
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
CMSC 341 Lecture 5 Stacks, Queues
Data Structures ADT List
Linked List (Part I) Data structure.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
2018, Fall Pusan National University Ki-Joune Li
Stacks and Queues.
2017, Fall Pusan National University Ki-Joune Li
Stacks CS-240 Dick Steflik.
Recursive Objects Singly Linked Lists.
2018, Fall Pusan National University Ki-Joune Li
LINKED LIST Dr. T. Kokilavani Assistant Professor
Data Structures Chapter 4: Linked Lists.
Linked Lists.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
2019, Fall Pusan National University Ki-Joune Li
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

2017, Fall Pusan National University Ki-Joune Li Linked Lists 2017, Fall Pusan National University Ki-Joune Li

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

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

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; };

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; } }; 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 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

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; first=first->next; delete tmpNode; return tmpData; }; Pop: Remove from the first Top Top Time Complexity: O(1)

Queues by Linked List Insert: Insert at last Time Complexity: O(n) 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 ?

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)

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)

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

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

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; } };

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

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();

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

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

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

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

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

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

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

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 ?

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 = ( (x10 + 2 x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z P1(z) Nested Polynomial Nested Linked List

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

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&)... };

Generalized List : Example 2 H y 3 x N 10 1 8 L 2 H y 4 1 x N 3 6 P = ( (x10 + 2 x8 ) y3 + 3 x8 y2 ) z2 + ( ( x4 + 6 x3 ) y4 + 2 y ) z H: Head Node L: Linked List N: Node H z

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

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

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)

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

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

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