Linked List Lecturer Kritawan Siriboon Room no 913
Linked List Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley
Sequential (Implicit) Array Implementation Problem Logical List : ABCD insert : shift out i Physical List B C D tail = 3 delete : shift in B C D
Solve Inserting Problem 3 2 ? Logical Linked List
Solve Deleting Problem 2 p 1
Dynamic Implementation
Address-of Operator& • Take the address of the operand. &i 0013 FF 60 • int i = 8; • &i ������ 0013 FF 60 8 i
Pointer Type Dereference (Indirection) Operator * • Pointer keeps the address (instead of normal data). &i • int i = 8; • &i ������ 0013 FF 60 • int *pi = &i; 0013 FF 60 8 i *pi 0013 FF 60 pi • *operand : location that the operand points to *pi is i as long as pi keep &i
Test 1 by Drawing What Happen ? • • int x = 8; int y = 5; int *p, *q; p = &x; q = &y; *p = *q + x; p = q; What happen ?
Pointer to Dynamic heap location & new function • new int ��� (allocates) heap memory ���� int ��� returns ��� address ��� memory ������������ ruturn ��� null Heap can only access through pointer ! main() { *q 5 q main() pi stack *pi i 8 heap int i = 8; int *pi = &i; int *q = new int; *q = 5; } What happen? q = &i; Danger! memory leak.
Test 2 by Drawing What Happen ? • • • int *p, *q; p = new int; *p = 8; q = p; cout << *p << *q; p = new int; *p = 1; *q = *p; q = p; //or q = 0; what happen?
Dynamic Implementation : node & constructor template<class T> node struct node{ T data; node* next; node(const T& d): data(d), next(0) { } }; int main() { node<char> n('A'); n. data = 'B'; //What happen? n. next = 0; } template<class T> class node{ public: T data; node* next; }; //constructor n A n. data n. next node<char> n; n. data = 'A'; n. next = 0;
Dynamic Node template<class T> node struct node{ T data; node* next; node(const T& d): data(d), next(0) { } }; int main() { node<char>* h = new node<char>(‘A'); (*h). data = ‘B'; h->data = ‘C'; h AB } node<char>* h (*h). data (*h). next h->data h->next
Creating Linked List node<char> *h , *t, *p; h = new node<char>(‘A’); t = h; p = new node<char>(‘B’); t->next = p; t h *h Looping to add a new node to the tail. A (*t). data (*t). next t->data t->next B p
printlist(h) p A template<class T> void printlist(node<T> *p){ while (p) { cout << p->data << ' '; p = p->next; } cout << 'n'; }
Insert After q 3 2 ? insertafter(q, d); node<char> *p = new node<char>(d); p->next = q->next; q->next = p;
Deleting After q 2 p 1 deleteafter(q); node<char> *p = q->next; q->next = p->next; p->next = 0; return p;
Passing by Value int i = 5; int i = k; int *p = &k; void f() { void sth(int i){ void Add 1(int *p){ int k = 8; int x = 3; (*p)++; sth(k); //pass data i = x; } sth(5); } Add 1(&k); //pass address void Ad 1(int& ref){ Ad 1(k); //pass by reference ref++; } } sth( f( ) x ) i 3 3 8 3 53 k 8 Add 1( ) p f( ) k 8 9 *p
Pass Address & Passing by Reference int i = 5; int *p = &k; int i = k; void f() { void sth(int i){ void Add 1(int *p){ int k = 8; int x = 3; (*p)++; sth(k); //pass data i = x; } sth(5); } int& ref = k; Add 1(&k); //pass address void Ad 1(int& ref){ Ad 1(k); //pass by reference ref++; } } sth( f( ) x ) i 3 3 8 3 53 k 8 Add 1( ) f( ) p ref k 8 9 10 *p
- Slides: 22