Data Structures - Mount Holyoke College
Download
Report
Transcript Data Structures - Mount Holyoke College
CSC211
Data Structures
Lecture 10
The Bag and Sequence Classes with
Linked Lists
Instructor: Prof. Xiaoyan Li
Department of Computer Science
Mount Holyoke College
Xiaoyan Li, 2007
1
Reviews: Node and Linked List
Node
a class with a pointer to an object of the node
class
core structure for the linked list
two versions of the “link” functions
why and how?
Xiaoyan Li, 2007
2
class node
{
public:
The Complete node Class Definition
default argument given
by the value_type
default constructor
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR
node(
const value_type& init_data = value_type( ),
node* init_link = NULL
)
{ data = init_data; link = init_link; }
The
node class is fundamental to linked lists
The private member variables
Member functions to set the data and link fields:
data_field //void
set_data(const value_type& new_data) { data = new_data; }
link_field
The
void set_link(node* new_link)
{ link = new_link; }
// Constant member function to retrieve the current data:
value_type data( ) const { return data; }
member functions include:
// Two slightly different member functions to retrieve
A constructor
// the current link:
const node* link( ) const { return link; }
data and
set
node*
link(link
)
{ return link;}
Retrieve
private:data and retrieve link
Set
Why TWO? p. 213-4
value_type data;
node* link;
};
Xiaoyan Li, 2007
3
Reviews: Node and Linked List
Linked
Lists Traverse
How
to access the next node by using link
pointer of the current node
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;
}
Xiaoyan Li, 2007
4
Reviews: Node and Linked List
Insert
Insert at the head
set
the head_ptr and the link of the new node
correctly
Insert at any location
cursor
pointing to the current node
need a pre-cursor to point to the node before the
current node (two approaches)
the third approach: doubly linked list
Xiaoyan Li, 2007
5
Reviews: Node and Linked List
Delete
Delete at the head
set
the head_ptr correctly
release the memory of the deleted node
Delete at any location
cursor
pointing to the current node
need a pre-cursor to point to the node before the
current node (two approaches)
the third approach: doubly linked list
Xiaoyan Li, 2007
6
Key points you need to knowToolkit Code
Linked List Toolkit uses the node class which has
set and retrieve functions
The functions in the Toolkit are not member
functions of the node class
length, insert(2), remove(2), search, locate, copy,...
compare their Big-Os with similar functions for an
array
They can be used in various container classes,
such as bag, sequence, etc.
Xiaoyan Li, 2007
7
Container Classes using Linked Lists
Bag Class with a Linked List
Sequence Class with a Linked List
Specification
Class definition
Implementation
Testing and Debugging
Design suggestion – difference from bag
Arrays or Linked Lists: which approach is better?
Dynamic Arrays
Linked Lists
Doubly Linked Lists
Xiaoyan Li, 2007
8
Our Third Bag - Specification
The documentation
nearly identical to our previous bag
The programmer uses the bag do not need to know
know about linked lists.
The difference
// static const size_type CAPACITY = _____
No worries
about capacity
// bag::CAPACITY
is thetherefore
maximum number of items
no default
// that acapacity
bag can hold. (in Bag 1)
no reserve function
because our new bag with linked list can grow or shrink
// static const size_type DEFAULT_CAPACITY = _____
easily!// bag::DEFAULT_CAPACITY is the initial capatity of a bag
//that is created by the default constructor. (in Bag 2)
Xiaoyan Li, 2007
9
Our Third Bag – Class Definition
The invariant of the 3rd bag class
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.
The Header File & implementation file (code)
(bag1, bag2, bag3)
Xiaoyan Li, 2007
10
Our Third Bag – Class Definition
How to match bag::value_type with
node::value_type
class bag
{
public:
typedef node::value_type value type;
......
}
Following the rules for dynamic memory usage
Xiaoyan Li, 2007
Allocate and release dynamic memory
The law of the Big-Three
11
Our Third Bag - Implementation
The Constructors
Overloading the Assignment Operator
release and re-allocate dynamic memory
self-assignment check
The Destructor
default constructor
copy constructor
return all the dynamic memory to the heap
Other functions and the code
Xiaoyan Li, 2007
12
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);
Xiaoyan Li, 2007
13
void bag::operator =(const bag& source)
{
node *tail_ptr;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
Xiaoyan Li, 2007
14
void bag::operator =(const bag& source)
{
node *tail_ptr; // needed for argument to list_copy
if (this == &source) // check for self-assignment
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
Xiaoyan Li, 2007
How to implement the copy constructor?
How to implement the destructor?
15
Sequence Class with Linked List
Compare three implementations
using a fixed size array (assignment 2)
using a dynamic array (assignment 3)
using a linked list (assignment 4)
What are the differences?
Xiaoyan Li, 2007
member variables
value semantics
Performance (time and space)
16
Sequence – Design Suggestions
Private member variables
many_nodes:
head_ptr
tail_ptr :
cursor : pointer to the current item (or NULL)
precursor: pointer to the item before the current item
Why tail_ptr
Why precursor?
Don’t forget
the dynamic allocation/release
the value semantics and
the Law of the Big-Three
Xiaoyan Li, 2007
17
Sequence – Value Semantics
Goal of assignment and copy constructor
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);
Xiaoyan Li, 2007
18
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.
Xiaoyan Li, 2007
19
list_copy(const node* source_ptr, node*& head_ptr,
node*& tail_ptr);
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list.
if (source_ptr == NULL) return;
// Make the head node for the newly created list, and put data in it.
list_head_insert(head_ptr, source_ptr->data( ));
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( );
while (source_ptr != NULL)
{ list_insert(tail_ptr,
source_ptr->data( ));
tail_ptr = tail_ptr->link( );
source_ptr = source_ptr->link( );
}
}
Xiaoyan Li, 2007
20
Sequence – Value Semantics
Goal of assignment and copy constructor
Can we just use list_copy in the Toolkit?
make one sequence equals to a new copy of another
list_copy(source.head_ptr, head_ptr, tail_ptr);
Problems ( deep copy – new memory allocation)
many_nodes OKAY
head_ptr and tail_ptr OKAY
How to set cursor and precursor ? (you need to handle
this in the fourth assignment)
Xiaoyan Li, 2007
21
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 twoway cursor
Frequent resizing may be
needed
Xiaoyan Li, 2007
22
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- Use a doubly linked list
way cursor
Frequent resizing may be
needed
Xiaoyan Li, 2007
Use a linked list
23
Dynamic Arrays vs Linked Lists
Arrays are better at random access
Linked lists are better at insertions/ deletions at a
cursor
O(1) vs O(n)
Doubly linked lists are better for a two-way cursor
O (1) vs. O(n)
for example for insert O(1) vs. O(n)
Resizing can be Inefficient for a Dynamic Array
Xiaoyan Li, 2007
re-allocation, copy, release
24
Reading and Programming
Assignments
Reading after Class
Chapter 6 —Templates and Iterators
Programming Assignment 4
Detailed guidelines online already!
Breakdown of points:
(70
points) Basis points (passes the seq_ex3 test)
(5 points) invariant of the class
(10 points) running time analysis
(15 points) Other implementation details
Xiaoyan Li, 2007
25