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