Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaoyan Li, 2007 1 CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.

Similar presentations


Presentation on theme: "Xiaoyan Li, 2007 1 CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer."— Presentation transcript:

1 Xiaoyan Li, 2007 1 CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

2 Xiaoyan Li, 2007 2 Reviews: Node and Linked List p Node p a class with a pointer to an object of the node class p core structure for the linked list p two versions of the “link” functions p why and how? p

3 Xiaoyan Li, 2007 3 The Complete node Class Definition p The node class is fundamental to linked lists p The private member variables p data_field p link_field p The member functions include: p A constructor p Set data and set link p Retrieve data and retrieve link class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data = init_data; link = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data = new_data; } void set_link(node* new_link) { link = new_link; } // Constant member function to retrieve the current data: value_type data( ) const { return data; } // Two slightly different member functions to retrieve // the current link: const node* link( ) const { return link; } node* link( ) { return link;} private: value_type data; node* link; }; default argument given by the value_type default constructor Why TWO? p. 213-4

4 Xiaoyan Li, 2007 4 Reviews: Node and Linked List p Linked Lists Traverse p How to access the next node by using link pointer of the current node p the special for loop size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; return count; }

5 Xiaoyan Li, 2007 5 Reviews: Node and Linked List p Insert p Insert at the head p set the head_ptr and the link of the new node correctly p Insert at any location p cursor pointing to the current node p need a pre-cursor to point to the node before the current node (two approaches) p the third approach: doubly linked list

6 Xiaoyan Li, 2007 6 Reviews: Node and Linked List p Delete p Delete at the head p set the head_ptr correctly p release the memory of the deleted node p Delete at any location p cursor pointing to the current node p need a pre-cursor to point to the node before the current node (two approaches) p the third approach: doubly linked list

7 Xiaoyan Li, 2007 7 Key points you need to know p Linked List Toolkit uses the node class which has p set and retrieve functions p The functions in the Toolkit are not member functions of the node class p length, insert(2), remove(2), search, locate, copy,... p compare their Big-Os with similar functions for an array p They can be used in various container classes, such as bag, sequence, etc. Toolkit Code

8 Xiaoyan Li, 2007 8 Container Classes using Linked Lists p Bag Class with a Linked List p Specification p Class definition p Implementation p Testing and Debugging p Sequence Class with a Linked List p Design suggestion – difference from bag p Arrays or Linked Lists: which approach is better? p Dynamic Arrays p Linked Lists p Doubly Linked Lists

9 Xiaoyan Li, 2007 9 Our Third Bag - Specification p The documentation p nearly identical to our previous bag p The programmer uses the bag do not need to know know about linked lists. p The difference p No worries about capacity therefore p no default capacity p no reserve function p because our new bag with linked list can grow or shrink easily! // static const size_type CAPACITY = _____ // bag::CAPACITY is the maximum number of items (in Bag 1) // that a bag can hold. (in Bag 1) // static const size_type DEFAULT_CAPACITY = _____ (in Bag 2) // bag::DEFAULT_CAPACITY is the initial capatity of a bag //that is created by the default constructor. (in Bag 2)

10 Xiaoyan Li, 2007 10 Our Third Bag – Class Definition p The invariant of the 3 rd bag class p the items in the bag are stored in a linked list (which is dynamically allocated)  the head pointer of the list is stored in the member variable head_ptr of the class bag  The total number of items in the list is stored in the member variable many_nodes. p The Header File & implementation file (code) (bag1, bag2, bag3) code

11 Xiaoyan Li, 2007 11 Our Third Bag – Class Definition p How to match bag::value_type with node::value_type p Following the rules for dynamic memory usage p Allocate and release dynamic memory p The law of the Big-Three class bag { public: public: typedef node::value_type value type;......}

12 Xiaoyan Li, 2007 12 Our Third Bag - Implementation p The Constructors p default constructor p copy constructor p Overloading the Assignment Operator p release and re-allocate dynamic memory p self-assignment check p The Destructor p return all the dynamic memory to the heap p Other functions and the code code

13 Xiaoyan Li, 2007 13 void bag::operator =(const bag& source) // FUNCTIONS for the linked list toolkit std::size_t list_length(const node* head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_insert(node* previous_ptr, const node::value_type& entry); node* list_search(node* head_ptr, const node::value_type& target); const node* list_search (const node* head_ptr, const node::value_type& target); node* list_locate(node* head_ptr, std::size_t position); const node* list_locate(const node* head_ptr, std::size_t position); void list_head_remove(node*& head_ptr); void list_remove(node* previous_ptr); void list_clear(node*& head_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);

14 Xiaoyan Li, 2007 14 void bag::operator =(const bag& source) { node *tail_ptr; node *tail_ptr; list_clear(head_ptr); list_clear(head_ptr); many_nodes = 0; many_nodes = 0; list_copy(source.head_ptr, head_ptr, tail_ptr); list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; many_nodes = source.many_nodes;}

15 Xiaoyan Li, 2007 15 void bag::operator =(const bag& source) { node *tail_ptr; // needed for argument to list_copy node *tail_ptr; // needed for argument to list_copy if (this == &source) // check for self-assignment if (this == &source) // check for self-assignment return; return; list_clear(head_ptr); list_clear(head_ptr); many_nodes = 0; many_nodes = 0; list_copy(source.head_ptr, head_ptr, tail_ptr); list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; many_nodes = source.many_nodes;} How to implement the copy constructor? How to implement the destructor?

16 Xiaoyan Li, 2007 16 Sequence Class with Linked List p Compare three implementations p using a fixed size array (assignment 2) p using a dynamic array (assignment 3) p using a linked list (assignment 4) p What are the differences? p member variables p value semantics p Performance (time and space)

17 Xiaoyan Li, 2007 17 Sequence – Design Suggestions p Private member variables  many_nodes :  head_ptr  tail_ptr : p Why tail_ptr  cursor : pointer to the current item (or NULL)  precursor : pointer to the item before the current item p Why precursor? p Don’t forget p the dynamic allocation/release p the value semantics and p the Law of the Big-Three

18 Xiaoyan Li, 2007 18 Sequence – Value Semantics p Goal of assignment and copy constructor p make one sequence equals to a new copy of another  Can we just use list_copy in the Toolkit?  void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); p

19 Xiaoyan Li, 2007 19 list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); 1. Set head_ptr and tail_ptr to NULL 2. If (souce_ptr == NULL), then teturn with no futher work 3. Allocate a new node for the head node of the new list that we are creating. Make both head_ptr and tail_prt point to this new node, and copy data from the head node of the source list. 4. Make source_ptr point to the second node, third node, and so on. At each node that source_ptr points to, add one new node to the tail of the new list, let tail_ptr point to the newly added node.

20 Xiaoyan Li, 2007 20 list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); { head_ptr = NULL; head_ptr = NULL; tail_ptr = NULL; tail_ptr = NULL; // Handle the case of the empty list. // Handle the case of the empty list. if (source_ptr == NULL) return; if (source_ptr == NULL) return; // Make the head node for the newly created list, and put data in it. // Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data( )); list_head_insert(head_ptr, source_ptr->data( )); tail_ptr = head_ptr; tail_ptr = head_ptr; // Copy the rest of the nodes one at a time, adding at the tail of new list source_ptr = source_ptr->link( ); source_ptr = source_ptr->link( ); while (source_ptr != NULL) while (source_ptr != NULL) { list_insert(tail_ptr, { list_insert(tail_ptr, source_ptr->data( )); source_ptr->data( )); tail_ptr = tail_ptr->link( ); tail_ptr = tail_ptr->link( ); source_ptr = source_ptr->link( ); source_ptr = source_ptr->link( ); }}

21 Xiaoyan Li, 2007 21 Sequence – Value Semantics p Goal of assignment and copy constructor p make one sequence equals to a new copy of another  Can we just use list_copy in the Toolkit?  list_copy(source.head_ptr, head_ptr, tail_ptr); p Problems ( deep copy – new memory allocation) p many_nodes OKAY p head_ptr and tail_ptr OKAY p How to set cursor and precursor ? (you need to handle this in the fourth assignment)

22 Xiaoyan Li, 2007 22 Choose a dynamic array, a linked list, or a doubly linked list? Frequent random access operations Operations occur at a cursor Operations occur at a two- way cursor Frequent resizing may be needed

23 Xiaoyan Li, 2007 23 Choose a dynamic array, a linked list, or a doubly linked list? Frequent random access operations Use a dynamic array Operations occur at a cursor Use a linked list Operations occur at a two- way cursor Use a doubly linked list Frequent resizing may be needed Use a linked list

24 Xiaoyan Li, 2007 24 Dynamic Arrays vs Linked Lists p Arrays are better at random access p O (1) vs. O(n) p Linked lists are better at insertions/ deletions at a cursor p O(1) vs O(n) p Doubly linked lists are better for a two-way cursor p for example for insert O(1) vs. O(n) p Resizing can be Inefficient for a Dynamic Array p re-allocation, copy, release

25 Xiaoyan Li, 2007 25 Reading and Programming Assignments p Reading after Class p Chapter 6 —Templates and Iterators p Programming Assignment 4 p Detailed guidelines online already! p Breakdown of points: p (70 points) Basis points (passes the seq_ex3 test) p (5 points) invariant of the class p (10 points) running time analysis p (15 points) Other implementation details


Download ppt "Xiaoyan Li, 2007 1 CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer."

Similar presentations


Ads by Google