Linked List ( ต่อ ) Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley
this pointer template class point{ int x, y; public: void move(int xx, int yy) { } }; x += xx; y += yy; this->x += xx; this->y += yy; point p, q; p.move(1,2); // this keeps &p q.move(3,4); // this keeps &q same as When an object calls a member function, the object’s address is passed automatically to the function as ‘this’ pointer. เมื่อออปเจคใดๆ เรียกเมมเบอร์ฟังก์ชั่น แอดเดรสของออปเจคนั้นจะถูกส่งมายังฟังก์ชั่น นั้นโดยอัตโนมัติในรูป this พอยน์เตอร์ (*this).x += xx; (*this). y += yy;
Dummy Node Can insert / delete to any position? p Using “Dummy Node” dummy head dummy head Empty List
Head & Tail Nodes Curcular List head tail
Doubly VS Singly Linked List Doubly Curcular List tailhead prev data next tail previous prev data next
Linked Stack Linked Queue top front rear Check if it support every operations.
Lab : Scrambling List h Cut it. Take the bottom up. I love you very much C++ Where ? h2 t2 t1 Now try. Print h2. Opps!
Lab : Scrambling List h1 Shuffle t h2 t2
Lab : Scrambling List h1 Shuffle t h2 t2
Linked Array Implementation List : ABCDEFG Create Available List (Stack) node n[8]; int avail = 0; avail = avail =
Using avialable node from avail stack. av= Returning avialable node to avail stack av= take out 1st node take out another node av= take out another node av= return node 2 first av=
old_av= avail; avail = n[avail].next ; n[old_av].data = d; n[old_av].next = nx; return old_av; getnode() node n[8]; int avail = 0; avail = int h = getnode(d,nx) h = A -1A 0 old_av = results av= A -1 h =
p h insertafter() X q E D C B A 5 h = av = 6 1 p = insertafter(p,d); int q = getnode(d,n[p].next); n[p].next = q; node n[8]; q = X 04 0 results h= av= p= q= C A D X B E
deleteafter() int i = delteafter(p); p h E D C B A 5 h = av = 6 1 p = q q = 3 C 3 int q = n[p].next; n[p].next = n[q].next; results h= av= p= q= C A D B E
Sharing pool of nodes. B Z Y X 5 h = av = 6 r = top = queue : r h g f C W 2 12 C f = r 7 h 3 g 14 f top C B stack : A r f Circular List: W X Y Z h
Sequential Array Insertion / Deletion Shifting Problem. Random Access. Static Allocation. Only keeps data. Solved. Sequential Access. Dynamic Allocation. Need space for linkes. Dynamic h
Application : Polynomial expression 5x 3 + 4x 2 - 7x + 10 x 3 + 2x How about... 5x x + 1 x
Application : Multilists 1. class หนึ่งๆ มีใครลงบ้าง 2. นร. คน หนึ่งๆ ลง class ใดบ้าง C1 C2 C3 C4 s1s2 s3s4s5
Application : Multilists 1. class หนึ่งๆ มีใครลงบ้าง 2. นร. คน หนึ่งๆ ลง class ใดบ้าง C1 C2 C3 C4 s1 s2 s3 s4 s s1 c1 s3 c1 s3 c2s5 c2 s3 c3 s4 c3 s1 c3 s2 c4s4 c4
Application : Radix Sort input: output :
Application : Radix Sort input: output: