Linked List Lecturers Text 1 Boontee Kruatrachue Kritawan
Linked List Lecturers : Text : รศ. ดร. บญธร เครอตราช 1 Boontee Kruatrachue Kritawan Siriboon Room no. 913 Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Stack push pop top LIFO List ( Last. In. First. Out ) • An ordered collection of items. One end is called a top of the stack. • Items are inserted onto the top of the stack. => push • Items are deleted from the top of the stack. => pop รศ. ดร. บญธร เครอตราช 3 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Queue แถวคอย front / head rear/tail en. Queue de. Queue FIFO List First. In. First. Out • An ordered collection of items. There are 2 ends, head (front) and tail (rear). • Items are inserted to the head (front). => en. Queue • Items are deleted from the tail (rear). => de. Queue รศ. ดร. บญธร เครอตราช 4 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
List : Sequential (Implicit) Array Implementation A B C D Problem : fix positions i Sequential Array insert i ? : shift out B C D delete : shift in B C D รศ. ดร. บญธร เครอตราช 6 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Linked List unfix positions 4 3 Implicit Array Sequential Array Problem : fix positions order? 1 7 7 3 NODE 1 15 15 Linked List Logical linked list รศ. ดร. บญธร เครอตราช 7 รศ. กฤตวน ศรบรณ link KMITL คำวา Logical หมายความวาในความคดของเรา เชน link แทนดวยลกศร แทนการเชอมโยงกน ในการ implement จรง (physical) ลกศรอาจเปนไดหลายอยาง เชน 01076249 Data Structures & Algorithms : Linked List pointer หรอ index ของ array
Implement a Physical Linked List รป logical linked list Dynamic Implementation use pointer as Linked array Implementation use array-index แทนดวย ตวเลข index เปน link รป plysical linked list แบบ dynamic implementation dynamic (allocation) ใชกบ pointer เพราะ เปนสวนของ memory ทเรยกมาใชและคนกลบไปทใดกไดตา มตองการ โดยใช malloc() และ free () ตรงขามกบ altomatic allocation ทเกดขนใน stack ของการเรยก function รศ. ดร. บญธร เครอตราช 8 รศ. กฤตวน ศรบรณ รป plysical linked list แบบ linked array implementation KMITL 01076249 Data Structures & Algorithms : Linked List
Solve Inserting Problem รศ. ดร. บญธร เครอตราช 9 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Solve Deleting Problem รศ. ดร. บญธร เครอตราช 10 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Dynamic Implementation รศ. ดร. บญธร เครอตราช 11 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Pointer int i = 3; int j = 5; & address of operator pointer keeps the address. Pointer to int what p points to takes operand’s address. int *p = &i; *p = 7; * dereference (indirection) operator takes what operand p = &j; points to *p = 9; 12 ff 48 P เครอตราช 12 รศ. กฤตวน ศรบรณ KMITL 12 ff 54 12 ff 60 12 ff 54 5 9 12 ff 60 3 7 *p j *p i รศ. ดร. บญธร address of i 01076249 Data Structures & Algorithms : Linked List
Common Misunderstanding int *p = &i; address is a int *p; What of int type of p = &i; *p? int = &i; int *q = p; int *q; q = p; q points to what p points to s and u are pointers int *s, t, *u; t is int รศ. ดร. บญธร เครอตราช 13 รศ. กฤตวน ศรบรณ wrong KMITL =What p points to? q p 59 *q *p i int *s; int t; int *u; 59 59 8 01076249 Data Structures & Algorithms : Linked List
Drawing what happen ? 1. 2. 3. 4. 5. 6. 7. รศ. ดร. บญธร เครอตราช 14 int x = int y = int *p, p = &x; q = &y; *p = *q p = q; รศ. กฤตวน ศรบรณ 8; 5; *q; + x; //What happen ? KMITL 01076249 Data Structures & Algorithms : Linked List
Dynamic pointer, malloc(), new() #include <stdlib. h> //to use malloc() void f(){ Heap can only int i = 8; access through pointer ! 59 int *p = &i ; address of i *q ในทนคอ 20 int *q = (int*) malloc(sizeof(int)); 20 *q new int; //C++ 5 heap Allocates *q = 5; จอง int ใน heap returns heap address or null ถา allocate ไมได malloc() returns void* แต q เปน int* ดงนนจงตอง casts (เปลยน type)ใหเปน int* ดวย (int*) r! e ak. g e n l Da ory m me q = &i; ? ? ? รศ. ดร. บญธร เครอตราช 18 รศ. กฤตวน ศรบรณ KMITL q 59 20 p *q *p 59 i 59 8 01076249 Data Structures & Algorithms : Linked List stack
free() delete() & NULL free (p); delete p; p = NULL; p = 0; //C++ • Deallocate what p points. คน ไมใช • NULL : pointer constant. • Used as ลบ memory ท – initialize value pช p = 0; p = NULL; คน คอทำใหวางสำหรบให – linked list ending. (*p). next = NULL; p->next = 0; malloc (new) ใหม • ดงนน free(p) จะทำให *p invalid) ใชไมไดแลว เมอคนแลวอาจมคนอนน รศ. ดร. บญธร เครอตราช 20 รศ. กฤตวน ศรบรณ ) KMITL 01076249 Data Structures & Algorithms : Linked List
Drawing what happen ? 1. 2. 3. 4. 5. 6. 7. 8. 9. int *p, *q; p = (int*) malloc(sizeof(int)); //new int; *p = 5; q = p; printf(“ %d %dn”, *p, *q); //cout << *p << *q; //c++ p = (int*) malloc(sizeof(int)); //new int; *p = 1; *q = *p; q = p; //or q = 0; or q = NULL; //what happen? รศ. ดร. บญธร เครอตราช 21 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
struct & Field Accessing struct node{ char data; struct node* next; }; struct เปน type ซง เกบขอมลไดมากกวา 1 ตว ขอมล ไม ตองม type เดยวกน n A struct node n; n. data = ‘A'; n. next = NULL; //n. next = 0; รศ. ดร. บญธร เครอตราช 22 รศ. กฤตวน ศรบรณ n. data n. next ขอมลแตละตวเรยก field access field : var. Name. field_name n เปนพนท heap ? KMITL memory ใน stack หรอ ใน stack 01076249 Data Structures & Algorithms : Linked List
Dynamic Node memory ใน p เปนพนท stack #include <stdlib. h> p struct node{ *p char data; *p เปนพนท memory struct node* next; เปน ใน heap (*p). data (*p). next }; struct node p->data p->next To use this form, struct node *p; p must be pointer p = (struct node*) malloc(sizeof(struct node)); (*p). data = ‘A'; // p->data = ‘A‘; // p->next = 0; (*p). next = 0; free(p) ������ node ��� p ��� free(p); x A • ��� Deallocate the node ��� p ���. • The node might be used by other. • *p invalid. รศ. ดร. บญธร เครอตราช 23 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Node in heap VS Node in stack Dynamic Automatic { struct node n; n. data = ‘A’; n. next = 0; } struct node *p; p =(struct node*)malloc(sizeof(struct node)); (*p). data = ‘A’; //p->data = ‘A’; (*p). next = 0; //p->next = 0; //. . . free(p); • node is in stack. • node (that is malloced / newed) is in heap. • automatic node: • dynamic node: – come when defined. – gone when out of scope. รศ. ดร. บญธร เครอตราช 24 – come when malloc/new. – gone when free/delete. • p is in stack. p is the stack variable that keeps the address of the malloced node. รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
typedef เปนการตง type ใหม จาก type ทมอยแลว typedef define a new type from the existing typedef existing_type new_typedef int data. Type; // only change here char string struct node{ data. Type data; // unchange here struct node* next; }; void f(data. Type d){ //. . . // unchange here typedef struct node; typedef node* node. Ptr; void f(node. Ptr p){ } รศ. ดร. บญธร เครอตราช 25 รศ. กฤตวน ศรบรณ KMITL // for short 01076249 Data Structures & Algorithms : Linked List
Get. Node() 1. Design how to call our function. p 10 p = get. Node(‘A’, NULL); Return type ? node* Function Name ? (How parameters ‘re passing? ) 2 nd parameter: • Type? • Pass by? 1 st parameter: • Type? , • Pass by? get. Node (data. Type data, node* nxt){ 10 node *q = (node*) malloc(sizeof(node)); *q q->data = data; //(*q). data q->next = nxt; A //(*q). next = nxt; get. Node() q 10 nxt NULL data ‘A’ caller of get. Node() p 10 { เครอตราช 26 รศ. กฤตวน ศรบรณ heap = data; return q; รศ. ดร. บญธร A KMITL 01076249 Data Structures & Algorithms : Linked List stack
C t p h : & C++ A q Creating a List B node *p = getnode(‘A’, NULL); node<char>* p = new node<char>(‘A’); //c++ node *h , *t; //head & tail of the list h = t = p; node *q = getnode(‘B’, NULL); node<char>* q = new node<char>(‘B’); //c++ p->next = q; รศ. ดร. บญธร เครอตราช 27 รศ. กฤตวน ศรบรณ Looping to add a new node to the tail. KMITL 01076249 Data Structures & Algorithms : Linked List
insert. After () q insert node data after a node pointed by q insert. After(‘i’, q); 2 1 ? C++ same algorithms , in detail later node<char> *p = new node<char>(data); p->next = q->next; q->next = p; Return type Fun. Name (parameter list) void insert. After(data. Type data, node* q){ node *p = getnode(data, q->next); q->next = p; } รศ. ดร. บญธร เครอตราช 28 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
print() Design how to call. print(head->next); head p Return type ? NULL output: Function Name (How parameters ‘re passing? ) B C D void print(node* p){ while (p){ // while (p != NULL) printf("%c ", p->data); p = p->next; } } รศ. ดร. บญธร เครอตราช 29 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
delete. After() Design how to call. q ? delete. After(q); delete a node after a node pointed by q Return type Fun. Name p 2 1 (parameter list) void delete. After(node* q){ { //C++, same algorithms , in detail later node *p = q->next; node<char> *p = q->next; q->next = p->next; free(p); } รศ. ดร. บญธร เครอตราช 30 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Dynamic VS Sequential Array Dynamic • Insertion / Deletion Shifting Problem. • Solved. • Random Access. • Sequential Access. • Automatic Allocation. • Dynamic Allocation. • Lifetime : from allocated by malloc()/new until deallocated by free()/delete. • Need spaces for linkes. • Lifetime : from defined until its scope finishes. • Only keeps data. รศ. ดร. บญธร เครอตราช 31 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Dummy Node To insert & delete at 1 st position change head ie. make special case. p “Dummy Node” solves the problem. head dummy head Empty List has a dummy node. รศ. ดร. บญธร เครอตราช 32 รศ. กฤตวน ศรบรณ KMITL dummy 01076249 Data Structures & Algorithms : Linked List
Head & Tail Nodes tail head Circular List tail Why ptr to tail ? Why not ptr to head? รศ. ดร. บญธร เครอตราช 33 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Doubly VS Singly Linked List head prev data next previous prev data next tail prev data next Doubly Circular List tail prev data next รศ. ดร. บญธร เครอตราช 34 prev data next รศ. กฤตวน ศรบรณ KMITL prev data next 01076249 Data Structures & Algorithms : Linked List
Linked Stack top Check if it support every operations. Linked Queue rear front Can switch front & rear ? รศ. ดร. บญธร เครอตราช 35 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
h Lift it x% Where t 1 to I love Lift_up ? x% Lab : Bottom Up h 2 Take the bottom up. you very t 2 Try to print h 2. Opps ! infinite loop ! much C++ รศ. ดร. บญธร เครอตราช 36 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Lab : Riffle Shuffle h 1 t 1 4 1 5 2 6 3 h 2 h 1 t 2 4 1 5 2 6 3 h 2 t 2 7 7 8 8 • Riffle Shuffle each node of each packet from the top. 9 • put the rest at the back of the result packet. t 1 9 • Lift up to 2 packets. รศ. ดร. บญธร เครอตราช 37 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Lab : Riffle Shuffle h 1 4 1 5 2 6 4 1 5 2 t 2 6 3 7 7 t 1 h 2 8 t 1 8 9 9 t 2 3 • Riffle Shuffle each node of each packet from the top. • put the rest at the back of the result packet. Lift up to 2 packets. รศ. ดร. บญธร เครอตราช 38 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Linked Array Implementation รศ. ดร. บญธร เครอตราช 39 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Linked Array Implementation List : ABCDEFG node n[8]; int fn ; //first free node Free (available) List Stack //or first available int av; 2 2 fn = fn= 1 fn = 3 3 4 5 5 4 6 -1 7 6 7 Only 1 free node : node 2 1 At first : all nodes are free. Free. Node List : 0 2 5 6 7 1 4 รศ. ดร. บญธร เครอตราช 40 รศ. กฤตวน ศรบรณ KMITL -1 All nodes are free. Free. Node List : 01234567 01076249 Data Structures & Algorithms : Linked List
Using/Returning free node from/to free list(stack) fn= 2 2 2 3 3 3 5 fn= 4 -1 6 take out 7 1 st node 1 รศ. ดร. บญธร เครอตราช 41 2 2 3 3 5 take out 5 4 another 4 -1 node fn= 6 -1 node 6 7 7 1 1 รศ. กฤตวน ศรบรณ KMITL fn= -1 6 7 1 fn= 6 4 return -1 node 2 6 first 7 1 01076249 Data Structures & Algorithms : Linked List
Data Structure - Initialize Available List (Free List) #define SIZE 8 typedef char data. Type; fn = 1 2 struct node{ data. Type data; int next; }; typedef struct node; 3 4 5 int main() { node n[SIZE]; int fn = init_avail(n) ; 6 7 //some people use av // instead of fn int init_avail(node* n){//initialize available list for (int i = 0; i < SIZE-1; i++) n[i]. next = i+1; n[i]. next = -1; //last available node return 0; //return first available (free) node n At first : all nodes are free. Free. Node List : 01234567 } รศ. ดร. บญธร เครอตราช 42 รศ. กฤตวน ศรบรณ KMITL -1 01076249 Data Structures & Algorithms : Linked List
get. Node() h //calling A int h = get. Node(‘A’, -1, n, &fn); #define SIZE 8 typedef char data. Type; struct node{ data. Type data; int next; }; typedef struct node; int main() { node n[SIZE]; int fn = 0; = h= ret. Node fn = int get. Node(data. Type d, int nx, node* n, int* pfn){ A int ret. Node = *pfn; //get the first free node *pfn = n[*pfn]. next; //change first free node //filling return node 3 5 4 h = n[ret. Node]. data = d; n[ret. Node]. next = nx; return ret. Node; 2 -1 -1 fn= 6 } 7 1 รศ. ดร. บญธร เครอตราช 43 รศ. กฤตวน ศรบรณ KMITL results 01076249 Data Structures & Algorithms : Linked List
p insert. After() h q C X -1 insert. After(‘x’, p, n, fn); void insert. After(data. Type d, h = A D 5 7 q = fn = X 6 0 p = B 0 4 int p , node* n, int& fn){ int q = get. Node(d, n[p]. next , n, fn); n[p]. next = q; C } h= q= p= fn= A D X B E รศ. ดร. บญธร เครอตราช 44 รศ. กฤตวน ศรบรณ 3 KMITL 3 -1 5 7 0 4 1 -1 results E 1 -1 node n[8]; fn = 4; //free. Node 01076249 Data Structures & Algorithms : Linked List
p delete. After() C h q fn delte. After(p, n, &fn); q= C h = A D void delete. After(int p, node* n, int* pfn){ int q = n[p]. next; n[p]. next = n[q]. next; //delete //return q to available list q from the list fn=q= C h= A D p= B n[q]. next = *pfn; *pfn = q; } E รศ. ดร. บญธร เครอตราช 45 รศ. กฤตวน ศรบรณ 3 -1 5 7 6 3 1 -1 results KMITL -1 fn = //q = the deleting node p = 3 4 5 7 6 B E 0 3 1 -1 01076249 Data Structures & Algorithms : Linked List
Sharing pool of nodes t Circular List: W X Y Z C B stack : A top 5 3 X j top = 4 C 5 Y 0 13 i 3 6 W h 2 7 C -1 -1 k 11 -1 1 2 7 fn = 8 9 f = 10 f r free. Node stack : 8 6 13 11 (available stack) เครอตราช 46 9 12 6 queue : h i j k รศ. ดร. บญธร Z B t = 0 รศ. กฤตวน ศรบรณ KMITL 11 12 13 r = 14 14 1 01076249 Data Structures & Algorithms : Linked List
Applications 1. Polynomial Expression 2. Multilists 3. Radix Sort
Polynomial expression How about. . . ? A=5 x 3 + 4 x 2 - 7 x + 10 5 x 85 + 7 x + 1 + B= x 3 + 2 x - 8 C=6 x 3 + 4 x 2 - 5 x + + x 76 - 8 2 What data structure will you use? Array? Sparse -> Linked List ( has lots of 0 data ) รศ. ดร. บญธร เครอตราช 48 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
Multilists . 1 class ����������� 2. �������� �� class ������ 0 s 1 0 C 1 s 1 c 1 3 C 4 รศ. ดร. บญธร 2 s 3 3 s 4 s 3 c 1 s 3 c 2 1 C 2 2 C 3 1 s 2 s 1 c 3 เครอตราช 49 s 5 c 2 s 3 c 3 s 3 c 4 รศ. กฤตวน ศรบรณ KMITL 4 s 5 s 4 c 3 s 4 c 4 01076249 Data Structures & Algorithms : Linked List
Radix Sort input: 0 729 27 512 216 8 0 343 1 251 125 1 334 46 512 621 72 9876543210 8 1 0 927 612 72 215 521 9876543210 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ output : 216 125 64 50 KMITL 8 972 46 343 64 27 8 1 0 251 162 433 64 125 27 297 8 1 0 01076249 Data Structures & Algorithms : Linked List
Radix Sort input: 512 343 1 512 0 125 9876543210 8 1 0 216 512 1 343 729 27 125 216 64 0 125 8 729 216 27 64 8 27729 64 343 9876543210 64 27 8 1 0 125 216 343 512 729 9876543210 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List output: 512 343 216 125 64 27 8 1 0 51
C++ : Template template<class T> struct node{ T data; node* next; }; int main(){ //node in stack node<char> n; n. data = 'A'; n. next = NULL; template<class T> class node{ public: T data; node* next; }; //node in stack node<int> n 2; n. data = 3; n. next = NULL; n A n. data n. next //node in heap node<char>* h = new node<char>; h->data = 'B'; h B h->next = NULL; รศ. ดร. บญธร เครอตราช 52 รศ. กฤตวน ศรบรณ } KMITL n 2 3 n. data n. next //node in heap node<int>* h 2 = new node<char>; h 2 ->data = 5; h 2 5 h 2 ->next = NULL; 01076249 Data Structures & Algorithms : Linked List
C++ : Constructor template<class T> //constructor A special function : class node{ • Function name = class name. public: • Is called automatically when obj is created. T data; • Can has >1 constructors. node* next; initializer : node(const T& d, node* nxt) : data(d), next(nxt) { } • form : field (data) inits data to the field }; • Only used in constructor. int main(){ //node in stack node<char> n; n. data = 'A'; n. next = NULL; } C n A int main(){ //node in stack node<char> n ('A', NULL) ; n. data n. next //node in heap node<char>* h = new node<char>; node<char>* h h->data = 'B'; = new node<char> h B h->next = NULL; } รศ. ดร. บญธร เครอตราช 53 รศ. กฤตวน ศรบรณ KMITL ; (‘B', NULL) 01076249 Data Structures & Algorithms : Linked List
C++ : Default Argument //Default Argument template<class T> Means : class node{ if no argument is passed public: the default value is used. T data; node* next; =0 node(const T& d, node* nxt = NULL ): data(d), next(nxt) { } }; //The default value is NULL. int main(){ Means //node in stack node<char> n (‘A’) ; n ('A', NULL) 2 nd No argument, Default NULL is used. n A n. data n. next //node in heap h B node<char>* h = new node<char>(‘B’, &n) ; } 2 nd argument is passed. Default NULL is not used. . รศ. ดร. บญธร เครอตราช 54 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
C++ : Class List A class includes template<class T> • data members class node{ //data • member functions (methods) T data; public: node* next; //methods node(const T& d, node* nxt = 0): data(d), next(nxt) { } //constructor }; int main() { template<class T> node<char> n(‘A’); class list{ public: list<char> l; node<T> *h, *t; //data } //method list(): h(NULL), t(NULL) { } //constructor }; รศ. ดร. บญธร เครอตราช 55 รศ. กฤตวน ศรบรณ KMITL data next n A h l 01076249 Data Structures & Algorithms : Linked List t
C++ : insert. End() template<class T> struct node{ T data; node* next; node(const T& d, node* nxt=0) : data(d), next(nxt) { } }; template<class T> class list{ node<T> *h, *t; //data public: list(): h(0), t(0) { } void insert. End(const T&); void print(); //later }; template<class T> void list<T>: : insert. End(const T& d){ if (!h) h = t = new node<T>(d); else{ t->next = new node<T>(d); t = t->next; } } รศ. ดร. บญธร เครอตราช 56 รศ. กฤตวน ศรบรณ int main() { list<char> l; l. insert. End('A'); l. insert. End('B'); l. insert. End('C'); l. print(); } A KMITL B h t l C 01076249 Data Structures & Algorithms : Linked List
C++ : print() l #include <iostream> using std: : cout; A template<class T> void list<T>: : print(){ node<T> *p = h; while (p){ cout << p->data << " p = p->next; } cout << ‘n’; } รศ. ดร. บญธร เครอตราช 57 รศ. กฤตวน ศรบรณ h t KMITL B p "; output: A D C NULL B C D 01076249 Data Structures & Algorithms : Linked List
C++ pp h 1 รศ. ดร. บญธร เครอตราช 58 4 รศ. กฤตวน ศรบรณ : y print() t 2 KMITL y 3 x x 5 01076249 Data Structures & Algorithms : Linked List
C++ : Private Vs Public A class : //Before //All members are public. • puclic member : • can be accessed anywhere. template<class T> • struct’s default is public. struct node{ • private member : T data; • For encapsulation, data should be private. node* next; node(const T& d, node* nxt=0) • can only be accessed by it’s own class function. • Other function cannot access it. : data(d), next(nxt) { } • class’s default is private }; template<class T> //Should be class node{ T data; //private node* next; //private public: node(const T& d, node* nxt=0) : data(d), next(nxt) { } }; รศ. ดร. บญธร เครอตราช 59 รศ. กฤตวน ศรบรณ Now, function in class list cannot access its node : data, next. KMITL 01076249 Data Structures & Algorithms : Linked List
C++ : Friend template<class T> class node { friend class list<T>; • Making F be a friend of A • makes F can access A as if it is A itself. • A friend can be either a function or a class. T data; //private node* next; //private public: node(const T& d, node* nxt=0) : data(d), next(nxt) { } }; template<class T> void list<T>: : print() { node<T> *p = h; while (p){ //ok list is a friend of node cout << p->data << " "; p = p->next; } cout << ‘n’; } template<class T> class list{ node<T> *h, *t; public: list(): h(0), t(0) { } print(); }; รศ. ดร. บญธร เครอตราช 60 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
C++ : How can we access a list head/tail ? template<class T> class node { l friend class list<T>; A T data; node* next; public: node(const T& d, node* nxt=0) : data(d), next(nxt) { } }; template<class T> class list{ node<T> *h, *t; //private public: list(): h(0), t(0) { } print(); }; รศ. ดร. บญธร เครอตราช 61 รศ. กฤตวน ศรบรณ h t B int main() { list<char> l; //. . . l. insert. After(‘D‘, l. h } C ); • No! h is private ! • Need pointer p that can access list’s head or tail or to any node in a list. • Here comes the iterator class : ist. Itr KMITL 01076249 Data Structures & Algorithms : Linked List
C++ : class list iterator 1 template<class T> class list{ class list<T>; //forward declaration class list. Itr<T>; //forward declaration template<class T> class node { friend class list. Itr<T>; node<T> *h, *t; public: list(): h(0), t(0) { } print(); friend class list<T>; friend class list. Itr<T>; T data; node* next; public: node(const T& d, node* nxt=0) : data(d), next(nxt) { } }; list. Itr<T> first() { return list. Itr(h); } int main() { list<char> l; //. . . l. insert. After(‘D‘, l. first() ); } รศ. ดร. บญธร เครอตราช 62 รศ. กฤตวน ศรบรณ }; KMITL list. Itr<T> last() { return list. Itr(t); } 01076249 Data Structures & Algorithms : Linked List
C++ template<class T> class list. Itr { friend class list<T>; : //current position to a list. Node node<T> *current; list. Itr(node<T>*p): current(p){ } public: class list iterator 2 int main() { list<char> l; //. . . l. insert. After(‘D‘, l. first() ); list. Itr<char> p(l. first()); list. Itr(): current(0){} bool is. Null(){return current==NULL; } } void advance(){ if (current != NULL) current = current->next; else cout<<"can't advance, current==NULL. n"; } const T& get. Data() const{ if (current != NULL) current = current->data; else cout<<"can't get data, current==NULL. n"; } } ; รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms 63 : Linked List
C++ : this pointer When an object calls a member function, the object’s address is passed automatically to the function to ‘this’ pointer. เมอออปเจคใดๆ เรยกเมมเบอรฟงกชน แอดเดรสของออปเจคนนจะถกสงมายงฟงกชนนนโดยอตโนมตใ นตวแปรพอยนเตอร this template<class T> point p, q; class point{ p. move(1, 2); int x, y; public: // this keeps &p void move(int xx, int yy) { x += xx; same as (*this). x += xx; y += yy; (*this). y += yy; q. move(3, 4); } }; รศ. ดร. บญธร this->x += xx; this->y += yy; เครอตราช 64 รศ. กฤตวน ศรบรณ KMITL // this keeps &q 01076249 Data Structures & Algorithms : Linked List
- Slides: 64