Topics <ul><li>Meaning of a Linked List </li></ul><ul><li>Meaning of a Dynamic Linked List </li></ul><ul><li>Traversal, In...
To implement the List ADT <ul><li>The programmer must </li></ul><ul><ul><li>1)  choose a concrete data representation  for...
Recall: 4 Basic Kinds of ADT Operations <ul><li>Constructors  -- create a new instance (object) of an ADT    </li></ul><ul...
List Operations <ul><li>Transformers  </li></ul><ul><ul><li>Insert  </li></ul></ul><ul><ul><li>Delete </li></ul></ul><ul><...
ADT List Operations <ul><li>Iterator </li></ul><ul><ul><li>Reset </li></ul></ul><ul><ul><li>GetNextItem </li></ul></ul><ul...
Array-based  class List Reset IsFull Length  IsPresent Delete IsEmpty Insert GetNexItem Private data: length data  [ 0] [1...
<ul><li>// Specification file array-based list (list.h) </li></ul><ul><li>const  int  MAX_LENGTH  =  50; </li></ul><ul><li...
Implementation Structures  <ul><li>Use a  built-in array  stored in contiguous memory locations, implementing operations I...
Implementation Possibilities for a List List Linked list Built-in array Built-in  dynamic data and pointers Built-in array...
A Linked List <ul><li>A  linked list  is a list in which the order of the components is determined by an explicit link mem...
Dynamic Linked List <ul><li>A  dynamic linked list  is one in which the  nodes are linked together by pointers and an exte...
Nodes can be located anywhere in memory <ul><li>The link member holds the memory address of the next node in the list  </l...
<ul><li>// Type declarations </li></ul><ul><li>struct NodeType  </li></ul><ul><li>{ </li></ul><ul><li>char info; </li></ul...
Pointer Dereferencing and Member Selection ‘ A’  6000 ptr .   info  .   link ptr ptr .   info  .   link ‘ A’  6000 *ptr pt...
ptr  is a pointer to a node ‘ A’  6000 ptr .   info  .   link ptr
*ptr  is the entire node pointed to by ptr .   info  .   link ‘ A’  6000 *ptr ptr
ptr->info  is a node member ptr->info (*ptr).info  // Equivalent ‘ A’  6000 ptr .   info  .   link
ptr->link  is a node member ptr->link (*ptr).link  // Equivalent ‘ A’  6000 ptr .   info  .   link
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Traversing a Dynamic Linked List // Pre:  head points to a dynamic linked list ptr  =  head; while (ptr != NULL)  { cout  ...
Using Operator  new <ul><li>Recall </li></ul><ul><li>If memory is available in the free store (or heap), operator new allo...
Inserting a Node at the Front of a List <ul><li>char  item = ‘B’; </li></ul><ul><li>NodePtr  location; </li></ul><ul><li>l...
Inserting a Node at the Front of a List <ul><li>char  item = ‘B’; </li></ul><ul><li>NodePtr  location; </li></ul><ul><li>l...
Inserting a Node at the Front of a List <ul><li>char  item = ‘B’; </li></ul><ul><li>NodePtr  location; </li></ul><ul><li>l...
Inserting a Node at the Front of a List <ul><li>char  item = ‘B’; </li></ul><ul><li>NodePtr  location; </li></ul><ul><li>l...
Inserting a Node at the Front of a List <ul><li>char  item = ‘B’; </li></ul><ul><li>NodePtr  location; </li></ul><ul><li>l...
Inserting a Node at the Front of a List <ul><li>char  item = ‘B’; </li></ul><ul><li>NodePtr  location; </li></ul><ul><li>l...
<ul><li>When you use the operator  delete </li></ul><ul><li>The object currently pointed to by the pointer is deallocated ...
Deleting the First Node from the List  <ul><li>NodePtr  tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>te...
Deleting the First Node from the List <ul><li>NodeType *  tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>...
Deleting the First Node from the List <ul><li>NodeType *  tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>...
Deleting the First Node from the List <ul><li>NodeType *  tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>...
Deleting the First Node from the List <ul><li>NodeType *  tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>...
What is a Sorted List? <ul><li>A  sorted list  is a variable-length, linear   collection of homogeneous elements, ordered ...
ADT HybridList Operations <ul><li>Transformers   </li></ul><ul><ul><li>InsertAsFirst  </li></ul></ul><ul><ul><li>Insert  <...
Upcoming SlideShare
Loading in …5
×

Lec6 mod linked list

714 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Lec6 mod linked list

  1. 1. Topics <ul><li>Meaning of a Linked List </li></ul><ul><li>Meaning of a Dynamic Linked List </li></ul><ul><li>Traversal, Insertion and Deletion of Elements in a Dynamic Linked List </li></ul><ul><li>Specification of a Dynamic Linked Sorted List </li></ul><ul><li>Insertion and Deletion of Elements in a Dynamic Linked Sorted List </li></ul>
  2. 2. To implement the List ADT <ul><li>The programmer must </li></ul><ul><ul><li>1) choose a concrete data representation for the list, and </li></ul></ul><ul><ul><li>2) implement the list operations </li></ul></ul>
  3. 3. Recall: 4 Basic Kinds of ADT Operations <ul><li>Constructors -- create a new instance (object) of an ADT </li></ul><ul><li>Transformers -- change the state of one or more of the data values of an instance </li></ul><ul><li> </li></ul><ul><li>Observers -- allow client to observe the state of one or more of the data values of an instance without changing them </li></ul><ul><li>Iterators -- allow client to access the data values in sequence </li></ul>
  4. 4. List Operations <ul><li>Transformers </li></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><li>Delete </li></ul></ul><ul><ul><li>Sort </li></ul></ul><ul><li>Observers </li></ul><ul><ul><li>IsEmpty </li></ul></ul><ul><ul><li>IsFull </li></ul></ul><ul><ul><li>Length </li></ul></ul><ul><ul><li>IsPresent </li></ul></ul>change state observe state
  5. 5. ADT List Operations <ul><li>Iterator </li></ul><ul><ul><li>Reset </li></ul></ul><ul><ul><li>GetNextItem </li></ul></ul><ul><li>Reset prepares for the iteration </li></ul><ul><li>GetNextItem returns the next item in sequence </li></ul><ul><li>No transformer can be called between calls to GetNextItem (Why?) </li></ul>Iteration Pair
  6. 6. Array-based class List Reset IsFull Length IsPresent Delete IsEmpty Insert GetNexItem Private data: length data [ 0] [1] [2] [MAX_LENGTH-1] currentPos SelSort
  7. 7. <ul><li>// Specification file array-based list (list.h) </li></ul><ul><li>const int MAX_LENGTH = 50; </li></ul><ul><li>typedef int ItemType; </li></ul><ul><li>class List // Declares a class data type </li></ul><ul><li>{ </li></ul><ul><li>public: // Public member functions </li></ul><ul><li>List(); // constructor </li></ul><ul><li>bool IsEmpty () const ; </li></ul><ul><li>bool IsFull () const; </li></ul><ul><li>int Length () const ; // Returns length of list </li></ul><ul><li>void Insert (ItemType item); </li></ul><ul><li>void Delete (ItemType item); </li></ul><ul><li>bool IsPresent(ItemType item) const ; </li></ul><ul><li>void SelSort (); </li></ul><ul><li>void Reset (); </li></ul><ul><li>ItemType GetNextItem (); </li></ul><ul><li>private: // Private data members </li></ul><ul><li> int length; // Number of values currently stored </li></ul><ul><li> ItemType data[MAX_LENGTH]; </li></ul><ul><li>int CurrentPos; // Used in iteration </li></ul><ul><li>}; </li></ul>
  8. 8. Implementation Structures <ul><li>Use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed </li></ul><ul><li>Use a linked list in which items are not necessarily stored in contiguous memory locations </li></ul><ul><li>A linked list avoids excessive data movement from insertions and deletions </li></ul>
  9. 9. Implementation Possibilities for a List List Linked list Built-in array Built-in dynamic data and pointers Built-in array of structs
  10. 10. A Linked List <ul><li>A linked list is a list in which the order of the components is determined by an explicit link member in each node </li></ul><ul><li>Each node is a struct containing a data member and a link member that gives the location of the next node in the list </li></ul>head ‘ X’ ‘C’ ‘L’
  11. 11. Dynamic Linked List <ul><li>A dynamic linked list is one in which the nodes are linked together by pointers and an external pointer (or head pointer) points to the first node in the list </li></ul>head “ Ted” “Irv” “Lee”
  12. 12. Nodes can be located anywhere in memory <ul><li>The link member holds the memory address of the next node in the list </li></ul>head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000
  13. 13. <ul><li>// Type declarations </li></ul><ul><li>struct NodeType </li></ul><ul><li>{ </li></ul><ul><li>char info; </li></ul><ul><li>NodeType* link; </li></ul><ul><li>} </li></ul><ul><li>typedef NodeType* NodePtr; </li></ul><ul><li>// Variable DECLARATIONS </li></ul><ul><li>NodePtr head; </li></ul><ul><li>NodePtr ptr; </li></ul>Declarations for a Dynamic Linked List . info . link ‘ A’ 6000
  14. 14. Pointer Dereferencing and Member Selection ‘ A’ 6000 ptr . info . link ptr ptr . info . link ‘ A’ 6000 *ptr ptr . info . link (*ptr).info ptr->info ‘ A’ 6000
  15. 15. ptr is a pointer to a node ‘ A’ 6000 ptr . info . link ptr
  16. 16. *ptr is the entire node pointed to by ptr . info . link ‘ A’ 6000 *ptr ptr
  17. 17. ptr->info is a node member ptr->info (*ptr).info // Equivalent ‘ A’ 6000 ptr . info . link
  18. 18. ptr->link is a node member ptr->link (*ptr).link // Equivalent ‘ A’ 6000 ptr . info . link
  19. 19. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  20. 20. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  21. 21. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  22. 22. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  23. 23. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  24. 24. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  25. 25. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  26. 26. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  27. 27. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  28. 28. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  29. 29. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  30. 30. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  31. 31. Using Operator new <ul><li>Recall </li></ul><ul><li>If memory is available in the free store (or heap), operator new allocates the requested object and returns a pointer to the memory allocated </li></ul><ul><li>The dynamically allocated object exists until the delete operator destroys it </li></ul>
  32. 32. Inserting a Node at the Front of a List <ul><li>char item = ‘B’; </li></ul><ul><li>NodePtr location; </li></ul><ul><li>location = new NodeType; </li></ul><ul><li>location->info = item; </li></ul><ul><li>location->link = head; </li></ul><ul><li>head = location; </li></ul>‘ B’ item head ‘ X’ ‘C’ ‘L’
  33. 33. Inserting a Node at the Front of a List <ul><li>char item = ‘B’; </li></ul><ul><li>NodePtr location; </li></ul><ul><li>location = new NodeType; </li></ul><ul><li>location->info = item; </li></ul><ul><li>location->link = head; </li></ul><ul><li>head = location; </li></ul>location head ‘ X’ ‘C’ ‘L’ ‘ B’ item
  34. 34. Inserting a Node at the Front of a List <ul><li>char item = ‘B’; </li></ul><ul><li>NodePtr location; </li></ul><ul><li>location = new NodeType; </li></ul><ul><li>location->info = item; </li></ul><ul><li>location->link = head; </li></ul><ul><li>head = location; </li></ul>location head ‘ X’ ‘C’ ‘L’ ‘ B’ item
  35. 35. Inserting a Node at the Front of a List <ul><li>char item = ‘B’; </li></ul><ul><li>NodePtr location; </li></ul><ul><li>location = new NodeType; </li></ul><ul><li>location->info = item; </li></ul><ul><li>location->link = head; </li></ul><ul><li>head = location; </li></ul>location ‘ B’ head ‘ X’ ‘C’ ‘L’ ‘ B’ item
  36. 36. Inserting a Node at the Front of a List <ul><li>char item = ‘B’; </li></ul><ul><li>NodePtr location; </li></ul><ul><li>location = new NodeType; </li></ul><ul><li>location->info = item; </li></ul><ul><li>location->link = head; </li></ul><ul><li>head = location; </li></ul>location ‘ B’ head ‘ X’ ‘C’ ‘L’ ‘ B’ item
  37. 37. Inserting a Node at the Front of a List <ul><li>char item = ‘B’; </li></ul><ul><li>NodePtr location; </li></ul><ul><li>location = new NodeType; </li></ul><ul><li>location->info = item; </li></ul><ul><li>location->link = head; </li></ul><ul><li>head = location; </li></ul>‘ X’ ‘C’ ‘L’ location ‘ B’ head ‘ B’ item
  38. 38. <ul><li>When you use the operator delete </li></ul><ul><li>The object currently pointed to by the pointer is deallocated and the pointer is considered undefined </li></ul><ul><li>The object’s memory is returned to the free store </li></ul>Using Operator delete
  39. 39. Deleting the First Node from the List <ul><li>NodePtr tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>tempPtr = head; </li></ul><ul><li>head = head->link; </li></ul><ul><li>delete tempPtr; </li></ul>‘ B’ ‘X’ ‘C’ ‘L’ tempPtr head item
  40. 40. Deleting the First Node from the List <ul><li>NodeType * tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>tempPtr = head; </li></ul><ul><li>head = head->link; </li></ul><ul><li>delete tempPtr; </li></ul>‘ B’ ‘X’ ‘C’ ‘L’ tempPtr head item ‘ B’
  41. 41. Deleting the First Node from the List <ul><li>NodeType * tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>tempPtr = head; </li></ul><ul><li>head = head->link; </li></ul><ul><li>delete tempPtr; </li></ul>‘ B’ ‘X’ ‘C’ ‘L’ tempPtr head item ‘ B’
  42. 42. Deleting the First Node from the List <ul><li>NodeType * tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>tempPtr = head; </li></ul><ul><li>head = head->link; </li></ul><ul><li>delete tempPtr; </li></ul>‘ B’ ‘X’ ‘C’ ‘L’ tempPtr head item ‘ B’
  43. 43. Deleting the First Node from the List <ul><li>NodeType * tempPtr; </li></ul><ul><li>item = head->info; </li></ul><ul><li>tempPtr = head; </li></ul><ul><li>head = head->link; </li></ul><ul><li>delete tempPtr; </li></ul>‘ X’ ‘C’ ‘L’ tempPtr head item ‘ B’
  44. 44. What is a Sorted List? <ul><li>A sorted list is a variable-length, linear collection of homogeneous elements, ordered according to the value of one or more data members </li></ul><ul><li>The transformer operations must maintain the ordering </li></ul><ul><li>In addition to Insert and Delete, let’s add two new operations to our list </li></ul><ul><li>InsertAsFirst and RemoveFirst </li></ul>
  45. 45. ADT HybridList Operations <ul><li>Transformers </li></ul><ul><ul><li>InsertAsFirst </li></ul></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><li>RemoveFirst </li></ul></ul><ul><ul><li>Delete </li></ul></ul><ul><li>Same observers and iterators as ADT List </li></ul><ul><li>Since we have two insertion and two deletion operations, let’s call this a Hybrid List </li></ul>change state

×