Title: CSC211 Data Structures
1CSC211 Data Structures
- Lecture 10
- The Bag and Sequence Classes with Linked Lists
- Instructor Prof. Xiaoyan Li
- Department of Computer Science
- Mount Holyoke College
2Reviews 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?
-
3The Complete node Class Definition
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
- The node class is fundamental to linked lists
- The private member variables
- data_field
- link_field
- The member functions include
- A constructor
- Set data and set link
- Retrieve data and retrieve link
Why TWO? p. 213-4
4Reviews 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
5Reviews 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
6Reviews 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
7Key points you need to know
Toolkit 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.
8Container Classes using Linked Lists
- Bag Class with a Linked List
- Specification
- Class definition
- Implementation
- Testing and Debugging
- Sequence Class with a Linked List
- Design suggestion difference from bag
- Arrays or Linked Lists which approach is better?
- Dynamic Arrays
- Linked Lists
- Doubly Linked Lists
9Our 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
- No worries about capacity therefore
- no default capacity
- no reserve function
- because our new bag with linked list can grow or
shrink easily!
// static const size_type CAPACITY _____ //
bagCAPACITY is the maximum number of items //
that a bag can hold. (in Bag 1)
// static const size_type DEFAULT_CAPACITY
_____ // bagDEFAULT_CAPACITY is the initial
capatity of a bag //that is created by the
default constructor. (in Bag 2)
10Our 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)
11Our Third Bag Class Definition
- How to match bagvalue_type with
nodevalue_type - Following the rules for dynamic memory usage
- Allocate and release dynamic memory
- The law of the Big-Three
class bag public typedef nodevalue_type
value type ......
12Our Third Bag - Implementation
- The Constructors
- default constructor
- copy constructor
- Overloading the Assignment Operator
- release and re-allocate dynamic memory
- self-assignment check
- The Destructor
- return all the dynamic memory to the heap
- Other functions and the code
13void bagoperator (const bag source)
- // FUNCTIONS for the linked list toolkit
- stdsize_t list_length(const node head_ptr)
- void list_head_insert(node head_ptr, const
nodevalue_type entry) - void list_insert(node previous_ptr, const
nodevalue_type entry) - node list_search(node head_ptr, const
nodevalue_type target) - const node list_search (const node head_ptr,
const nodevalue_type target) - node list_locate(node head_ptr, stdsize_t
position) - const node list_locate(const node head_ptr,
stdsize_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)
14void bagoperator (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
15void bagoperator (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
-
How to implement the copy constructor?
How to implement the destructor?
16Sequence 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?
- member variables
- value semantics
- Performance (time and space)
17Sequence Design Suggestions
- Private member variables
- many_nodes
- head_ptr
- tail_ptr
- Why tail_ptr
- cursor pointer to the current item (or NULL)
- precursor pointer to the item before the
current item - Why precursor?
- Dont forget
- the dynamic allocation/release
- the value semantics and
- the Law of the Big-Three
18Sequence 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) -
19list_copy(const node source_ptr, node
head_ptr, node tail_ptr)
- Set head_ptr and tail_ptr to NULL
- If (souce_ptr NULL), then teturn with no
futher work - 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. - 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.
20list_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-gtdata(
)) - 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-gtlink( )
- while (source_ptr ! NULL)
- list_insert(tail_ptr,
- source_ptr-gtdata( ))
- tail_ptr tail_ptr-gtlink( )
- source_ptr source_ptr-gtlink( )
-
21Sequence 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?
- 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)
22Choose 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
23Choose 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
24Dynamic Arrays vs Linked Lists
- Arrays are better at random access
- O (1) vs. O(n)
- 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 - for example for insert O(1) vs. O(n)
- Resizing can be Inefficient for a Dynamic Array
- re-allocation, copy, release
25Reading 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