xiaoyan li, 2007 1 csc211 data structures lecture 10 the bag and sequence classes with linked lists...

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

Upload: bernard-may

Post on 31-Dec-2015

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 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

Xiaoyan Li, 2007Xiaoyan Li, 2007 11

CSC211 Data Structures CSC211 Data Structures

Lecture 10Lecture 10

The Bag and Sequence Classes with The Bag and Sequence Classes with Linked ListsLinked Lists

Instructor: Prof. Xiaoyan LiInstructor: Prof. Xiaoyan LiDepartment of Computer Science Department of Computer Science

Mount Holyoke CollegeMount Holyoke College

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 22

Reviews: Node and Linked ListReviews: Node and Linked List

NodeNode a class with a pointer to an object of the node a class with a pointer to an object of the node

classclass core structure for the linked listcore structure for the linked list two versions of the “link” functionstwo versions of the “link” functions

why and how?why and how?

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 33

The Complete node Class DefinitionThe Complete node Class Definition

The node class is fundamental to linked listsThe node class is fundamental to linked lists The private member variablesThe private member variables

data_fielddata_field link_fieldlink_field

The member functions include:The member functions include: A constructorA constructor Set data and set linkSet data and set link Retrieve data and retrieve linkRetrieve data and retrieve link

class node { public:

// TYPEDEFtypedef double value_type;

// CONSTRUCTORnode( 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

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 44

Reviews: Node and Linked ListReviews: Node and Linked List

Linked Lists TraverseLinked Lists Traverse How to access the next node by using link How to access the next node by using link

pointer of the current nodepointer of the current node the special for loopthe 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; }

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 55

Reviews: Node and Linked ListReviews: Node and Linked List

Insert Insert Insert at the head Insert at the head

set the head_ptr and the link of the new node set the head_ptr and the link of the new node correctlycorrectly

Insert at any locationInsert at any location cursor pointing to the current node cursor pointing to the current node need a pre-cursor to point to the node before the need a pre-cursor to point to the node before the

current node (two approaches)current node (two approaches) the third approach: the third approach: doubly linked listdoubly linked list

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 66

Reviews: Node and Linked ListReviews: Node and Linked List

Delete Delete Delete at the head Delete at the head

set the head_ptr correctlyset the head_ptr correctly release the memory of the deleted noderelease the memory of the deleted node

Delete at any locationDelete at any location cursor pointing to the current node cursor pointing to the current node need a pre-cursor to point to the node before the need a pre-cursor to point to the node before the

current node (two approaches)current node (two approaches) the third approach: the third approach: doubly linked listdoubly linked list

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 77

Key points you need to knowKey points you need to know

Linked List Toolkit uses the node class which hasLinked List Toolkit uses the node class which has set and retrieve functionsset and retrieve functions

The functions in the Toolkit are not member The functions in the Toolkit are not member functions of the node classfunctions of the node class length, insert(2), remove(2), search, locate, copy,...length, insert(2), remove(2), search, locate, copy,... compare their Big-Os with similar functions for an compare their Big-Os with similar functions for an

arrayarray

They can be used in various container classes, They can be used in various container classes, such as bag, sequence, etc.such as bag, sequence, etc.

Toolkit Code

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 88

Container Classes using Linked ListsContainer Classes using Linked Lists

Bag Class with a Linked ListBag Class with a Linked List SpecificationSpecification Class definitionClass definition ImplementationImplementation Testing and DebuggingTesting and Debugging

Sequence Class with a Linked ListSequence Class with a Linked List Design suggestion – difference from bagDesign suggestion – difference from bag

Arrays or Linked Lists: which approach is better?Arrays or Linked Lists: which approach is better? Dynamic Arrays Dynamic Arrays Linked ListsLinked Lists Doubly Linked ListsDoubly Linked Lists

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 99

Our Third Bag - SpecificationOur Third Bag - Specification

The documentationThe documentation nearly identical to our previous bagnearly identical to our previous bag The programmer uses the bag do not need to know The programmer uses the bag do not need to know

know about linked lists.know about linked lists. The differenceThe difference

No worries about capacity thereforeNo worries about capacity therefore no default capacityno default capacity no reserve functionno reserve function

because our new bag with linked list can grow or shrink because our new bag with linked list can grow or shrink easily!easily!

// static const size_type CAPACITY = _____ // bag::CAPACITY is the maximum number of items

// that a bag can hold. (in Bag 1) (in Bag 1)

// static const size_type DEFAULT_CAPACITY = _____ // bag::DEFAULT_CAPACITY is the initial capatity of a

bag //that is created by the default constructor. (in Bag 2) (in Bag 2)

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1010

Our Third Bag – Class DefinitionOur Third Bag – Class Definition

The invariant of the 3The invariant of the 3rdrd bag class bag class the items in the bag are stored in a linked list (which is the items in the bag are stored in a linked list (which is

dynamically allocated)dynamically allocated) the head pointer of the list is stored in the member the head pointer of the list is stored in the member

variable variable head_ptrhead_ptr of the class bag of the class bag The total number of items in the list is stored in the The total number of items in the list is stored in the

member variable member variable many_nodesmany_nodes..

The Header File & implementation file (The Header File & implementation file (codecode) ) (bag1, bag2, bag3)(bag1, bag2, bag3)

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1111

Our Third Bag – Class DefinitionOur Third Bag – Class Definition

How to match bag::value_type with How to match bag::value_type with node::value_typenode::value_type

Following the rules for dynamic memory usageFollowing the rules for dynamic memory usage Allocate and release dynamic memory Allocate and release dynamic memory The law of the Big-ThreeThe law of the Big-Three

class bag class bag {{ public:public:

typedef typedef node::value_typenode::value_type value type; value type;............

}}

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1212

Our Third Bag - ImplementationOur Third Bag - Implementation

The ConstructorsThe Constructors default constructor default constructor copy constructorcopy constructor

Overloading the Assignment OperatorOverloading the Assignment Operator release and re-allocate dynamic memoryrelease and re-allocate dynamic memory self-assignment checkself-assignment check

The DestructorThe Destructor return all the dynamic memory to the heap return all the dynamic memory to the heap

Other functions and the Other functions and the codecode

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1313

void bag::operator =(const bag& source)void bag::operator =(const bag& source)

// FUNCTIONS for the linked list toolkit // FUNCTIONS for the linked list toolkit std::size_t list_length(const node* head_ptr); std::size_t list_length(const node* head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_insert(node* previous_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); node* list_search(node* head_ptr, const node::value_type& target); const node* list_search (const 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); node* list_locate(node* head_ptr, std::size_t position); const node* list_locate(const 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_head_remove(node*& head_ptr); void list_remove(node* previous_ptr); void list_remove(node* previous_ptr); void list_clear(node*& head_ptr); void list_clear(node*& head_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1414

void bag::operator =(const bag& source)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;

}}

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1515

void bag::operator =(const bag& source)void bag::operator =(const bag& source)

{{ node *tail_ptr; // needed for argument to list_copynode *tail_ptr; // needed for argument to list_copy if (this == &source) // check for self-assignmentif (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 copy constructor?

How to implement the destructor?How to implement the destructor?

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1616

Sequence Class with Linked ListSequence Class with Linked List

Compare three implementationsCompare three implementations using a fixed size array (assignment 2)using a fixed size array (assignment 2) using a dynamic array (assignment 3)using a dynamic array (assignment 3) using a linked list (assignment 4)using a linked list (assignment 4)

What are the differences?What are the differences? member variablesmember variables value semantics value semantics Performance (time and space)Performance (time and space)

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1717

Sequence – Design SuggestionsSequence – Design Suggestions

Private member variables Private member variables many_nodesmany_nodes:: head_ptrhead_ptr tail_ptrtail_ptr : :

Why tail_ptrWhy tail_ptr cursorcursor : pointer to the current item (or NULL) : pointer to the current item (or NULL) precursorprecursor: pointer to the item before the current item : pointer to the item before the current item

Why precursor?Why precursor?

Don’t forget Don’t forget the dynamic allocation/releasethe dynamic allocation/release the value semantics and the value semantics and the Law of the Big-Threethe Law of the Big-Three

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1818

Sequence – Value SemanticsSequence – Value Semantics

Goal of assignment and copy constructorGoal of assignment and copy constructor make one sequence equals to a new copy of make one sequence equals to a new copy of

anotheranother Can we just use Can we just use list_copylist_copy in the Toolkit? in the Toolkit?

void void list_copy(const node* source_ptr, node*& list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);head_ptr, node*& tail_ptr);

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 1919

list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);

1.1. Set head_ptr and tail_ptr to NULLSet head_ptr and tail_ptr to NULL

2.2. If (souce_ptr == NULL), then teturn with no futher workIf (souce_ptr == NULL), then teturn with no futher work

3.3. Allocate a new node for the head node of the new list that we are 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, creating. Make both head_ptr and tail_prt point to this new node, and copy data from the head node of the source list.and copy data from the head node of the source list.

4.4. Make source_ptr point to the second node, third node, and so on. At 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 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. the new list, let tail_ptr point to the newly added node.

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 2020

list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);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// 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( ); } } } }

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 2121

Sequence – Value SemanticsSequence – Value Semantics

Goal of assignment and copy constructorGoal of assignment and copy constructor make one sequence equals to a new copy of anothermake one sequence equals to a new copy of another

Can we just use Can we just use list_copylist_copy in the Toolkit? in the Toolkit? list_copy(source.head_ptr, head_ptr, tail_ptr);list_copy(source.head_ptr, head_ptr, tail_ptr);

Problems ( deep copy – new memory allocation)Problems ( deep copy – new memory allocation) many_nodes OKAYmany_nodes OKAY head_ptr and tail_ptr OKAY head_ptr and tail_ptr OKAY How to set cursor and precursor ? (you need to handle How to set cursor and precursor ? (you need to handle

this in the fourth assignment)this in the fourth assignment)

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 2222

Choose a dynamic array, a linked list, or a doubly linked list?Choose a dynamic array, a linked list, or a doubly linked list?

Frequent random access Frequent random access operationsoperations

Operations occur at a Operations occur at a cursorcursor

Operations occur at a two-Operations occur at a two-way cursorway cursor

Frequent resizing may be Frequent resizing may be neededneeded

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 2323

Choose a dynamic array, a linked list, or a doubly linked list?Choose a dynamic array, a linked list, or a doubly linked list?

Frequent random access Frequent random access operationsoperations

Use a dynamic arrayUse a dynamic array

Operations occur at a Operations occur at a cursorcursor

Use a linked listUse a linked list

Operations occur at a two-Operations occur at a two-way cursorway cursor

Use a doubly linked listUse a doubly linked list

Frequent resizing may be Frequent resizing may be neededneeded

Use a linked listUse a linked list

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 2424

Dynamic Arrays vs Linked ListsDynamic Arrays vs Linked Lists

Arrays are better at random accessArrays are better at random access O (1) vs. O(n)O (1) vs. O(n)

Linked lists are better at insertions/ deletions at a Linked lists are better at insertions/ deletions at a cursorcursor O(1) vs O(n)O(1) vs O(n)

Doubly linked lists are better for a two-way cursorDoubly linked lists are better for a two-way cursor for example for insert O(1) vs. O(n)for example for insert O(1) vs. O(n)

Resizing can be Inefficient for a Dynamic ArrayResizing can be Inefficient for a Dynamic Array re-allocation, copy, release re-allocation, copy, release

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

Xiaoyan Li, 2007Xiaoyan Li, 2007 2525

Reading and Programming AssignmentsReading and Programming Assignments

Reading after ClassReading after Class Chapter 6 —Templates and IteratorsChapter 6 —Templates and Iterators

Programming Assignment 4Programming Assignment 4 Detailed guidelines online already!Detailed guidelines online already! Breakdown of points:Breakdown of points:

(70 points) Basis points (passes the seq_ex3 test)(70 points) Basis points (passes the seq_ex3 test) (5 points) invariant of the class(5 points) invariant of the class (10 points) running time analysis(10 points) running time analysis (15 points) Other implementation details(15 points) Other implementation details