1 / 51

Linked List - I

Linked List - I. Abstract Data Type (ADT). ADT is a data type whose properties (domain and operations) are specified independently of any particular implementation. Logical (or ADT) level: abstract view of the domain and operations.

tate
Download Presentation

Linked List - I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked List - I

  2. Abstract Data Type (ADT) • ADT is a data type whose properties (domain and operations) are specified independently of any particular implementation. • Logical (or ADT) level:abstract view of the domain and operations. • Implementation level:specific representation of the structure to hold the data items, and the coding for operations.

  3. 4 Basic Kinds of ADT Operations • Constructor/Destructor – creates/deletes a new instance (object) of an ADT. • Transformer -- changes the state of one or more of the data values of an instance. • Observer -- allows us to observe the state of one or more of the data values of an instance without changing them. • Iterator -- allows us to process all the components in a data structure sequentially.

  4. List • Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. • Length The number of items in a list; the length can vary over time.

  5. Class ShoppingList class ShoppingList { public : int LengthIs ( ) const ; void RetrieveItem (int pos) ; void InsertItem (const Item& item, int pos ) ; void RemoveItem (int pos); void DeleteAll ( ); private : int size ; Node *front ; } ;

  6. Pointer to front node Data carried in node Pointer to next node A B C NULL Nodes Singly Linked List Diagram

  7. Item item; Node *link; A Class Node class Node { public: Node(Item i){item = i}; private: Item item; Node *link; }; Insert an item/node into shopping list? Delete an item/node?

  8. anItem pos D 0 size 3 Front Insert Example (1 of 6) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, //ignore prev ptr for the FrontInsert example *cur; Inserting D at position 0 prev cur ? ? front item link item link item link A B C

  9. anItem pos D 0 Front Insert Example (2 of 6) assert(pos >= 0 && pos <= size); size++; cur ? size front 4 item link item link item link A B C

  10. anItem pos D 0 Front Insert Example (3 of 6) if (pos == 0) { // Inserting at the front cur = front; cur size front 4 item link item link item link A B C

  11. anItem pos D 0 Front Insert Example (4 of 6) front = new Node(anItem); cur size front 4 item link item link item link A B C item link D ?

  12. anItem pos D 0 Front Insert Example (5 of 6) front->link = cur; cur size front 4 item link item link item link A B C item link D

  13. Front Insert Example (6 of 6) return; } size front 4 item link item link item link A B C item link D

  14. anItem pos A 0 front Empty Insert Example (1 of 6) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, //ignore prev for EmptyInsert example *cur; Inserting A at position 0 in empty list cur ? size 0

  15. anItem pos A 0 front Empty Insert Example (2 of 6) assert(pos >= 0 && pos <= size); size++; cur ? size 1

  16. anItem pos A 0 front Empty Insert Example (3 of 6) if (pos == 0) { // Inserting at the front cur = front; cur size 1

  17. anItem pos A 0 item link A front Empty Insert Example (4 of 6) front = new Node(anItem); cur size 1 ?

  18. anItem pos A 0 item link A front Empty Insert Example (5 of 6) front->link = cur; cur size 1

  19. item link A front Empty Insert Example (6 of 6) return; } size 1

  20. anItem pos D 2 size 3 Middle Insert Example (1 of 8) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 2 prev cur ? ? front item link item link item link A B C

  21. anItem pos D 2 size 4 Middle Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++; prev cur ? ? front item link item link item link A B C

  22. anItem pos D 2 size 4 Middle Insert Example (3 of 8) prev = NULL; cur = front; prev cur front item link item link item link A B C

  23. anItem pos D 1 size 4 Middle Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First Iteration prev cur front item link item link item link A B C

  24. anItem pos D 0 size 4 Middle Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Second Iteration prev cur front item link item link item link A B C

  25. anItem pos D 0 size 4 Middle Insert Example (6 of 8) prev->link = new Node(anItem); prev cur front item link item link item link A B C item link D ?

  26. anItem pos D 0 size 4 Middle Insert Example (7 of 8) prev->link->link = cur; prev cur front item link item link item link A B C item link D

  27. size 4 Middle Insert Example (8 of 8) } front item link item link item link A B C item link D

  28. anItem pos D 3 size 3 End Insert Example (1 of 8) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 3 prev cur ? ? front item link item link item link A B C

  29. anItem pos D 3 size 4 End Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++; prev cur ? ? front item link item link item link A B C

  30. anItem pos D 3 size 4 End Insert Example (3 of 8) prev = 0; cur = front; prev cur 0 front item link item link item link A B C

  31. anItem pos D 2 size 4 End Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First Iteration prev cur front item link item link item link A B C

  32. anItem pos D 1 size 4 End Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Second Iteration prev cur front item link item link item link A B C

  33. anItem pos D 0 size 4 End Insert Example (6 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Third Iteration prev cur front item link item link item link A B C

  34. anItem pos D 0 size 4 End Insert Example (7 of 8) prev->link = new Node(anItem); prev->link->link = cur; prev cur front item link item link item link A B C item link D

  35. size 4 End Insert Example (8 of 8) } front item link item link item link A B C item link D

  36. void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; assert(pos >= 0 && pos <= size); size++; if (pos == 0) { // Inserting at the front cur = front; front = new Node(anItem); front->link = cur; return; } prev = NULL; cur = front; while (pos > 0) { prev = cur; cur = cur->link; pos--; } prev->link = new Node(anItem); prev->link->link = cur; }

  37. pos 1 size 3 Remove Example (1 of 7) void ShoppingList::remove(int pos) { Node *cur, *prev; Removing at position 1 prev cur ? ? front item link item link item link A B C

  38. pos 1 size 2 Remove Example (2 of 7) assert(pos >= 0 && pos < size); size--; prev cur ? ? front item link item link item link A B C

  39. pos 1 size 2 Remove Example (3 of 7) prev = NULL; cur = front; prev cur front item link item link item link A B C

  40. Delete the node pointed by cur pos 0 size 2 Remove Example (4 of 7) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First iteration prev cur front item link item link item link A B C

  41. pos 0 size 2 Remove Example (5 of 7) prev->link = cur->link; prev cur front item link item link item link A B C

  42. pos 0 size 2 Remove Example (6 of 7) delete cur; prev cur front item link item link item link A B C

  43. size 2 Remove Example (7 of 7) } front item link item link A C

  44. kil ? size 3 Delete All Example (1 of 8) void ShoppingList::deleteAll() { Node *kil; item link item link item link A B C front

  45. kil size 3 Delete All Example (2 of 8) while (front != NULL) { kil = front; front = front->link; First iteration item link item link item link A B C front

  46. kil size 3 Delete All Example (3 of 8) kil->link = NULL; delete kil; } First iteration item link item link item link A B C front

  47. kil size 3 Delete All Example (4 of 8) while (front != NULL) { kil = front; front = front->link; Second iteration item link item link B C front

  48. kil size 3 Delete All Example (5 of 8) kil->link = NULL; delete kil; } Second iteration item link item link B C front

  49. kil size 3 Delete All Example (6 of 8) while (front != NULL) { kil = front; front = front->link; Third iteration item link C front

  50. kil size 3 Delete All Example (7 of 8) kil->link = 0; delete kil; } Third iteration item link C front 0

More Related