1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// FILE: bag3.cxx
// CLASS implemented: bag (see bag3.h for documentation)
// INVARIANT for the bag ADT:
// 1. The items in the bag are stored on a linked list;
// 2. The head pointer of the list is stored in the member variable head_ptr;
// 3. The total number of items in the list is stored in the member variable
// many_nodes.
#include <cassert> // Provides assert
#include <cstdlib> // Provides NULL, rand, size_t
#include "node1.h" // Provides node and the linked list functions
#include "bag3.h"
using namespace std;
namespace main_savitch_5
{
bag::bag( )
// Library facilities used: cstdlib
{
head_ptr = NULL;
many_nodes = 0;
}
bag::bag(const bag& source)
// Library facilities used: node1.h
{
node *tail_ptr; // Needed for argument of list_copy
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
bag::~bag( )
// Library facilities used: node1.h
{
list_clear(head_ptr);
many_nodes = 0;
}
bag::size_type bag::count(const value_type& target) const
// Library facilities used: cstdlib, node1.h
{
size_type answer;
const node *cursor; // Use const node* since we don't change the nodes.
answer = 0;
cursor = list_search(head_ptr, target);
while (cursor != NULL)
{
// Each time that cursor is not NULL, we have another occurrence of
// target, so we add one to answer, and move cursor to the next
// occurrence of the target.
++answer;
cursor = cursor->link( );
cursor = list_search(cursor, target);
}
return answer;
}
bag::size_type bag::erase(const value_type& target)
// Library facilities used: cstdlib, node1.h
{
size_type answer = 0;
node *target_ptr;
target_ptr = list_search(head_ptr, target);
while (target_ptr != NULL)
{
// Each time that target_ptr is not NULL, we have another occurrence
// of target. We remove this target using the same technique that
// was used in erase_one.
target_ptr->set_data( head_ptr->data( ) );
target_ptr = target_ptr->link( );
target_ptr = list_search(target_ptr, target);
list_head_remove(head_ptr);
--many_nodes;
++answer;
}
return answer;
}
bool bag::erase_one(const value_type& target)
// Library facilities used: cstdlib, node1.h
{
node *target_ptr;
target_ptr = list_search(head_ptr, target);
if (target_ptr == NULL)
return false; // target isn't in the bag, so no work to do
target_ptr->set_data( head_ptr->data( ) );
list_head_remove(head_ptr);
--many_nodes;
return true;
}
bag::value_type bag::grab( ) const
// Library facilities used: cassert, cstdlib, node1.h
{
size_type i;
const node *cursor; // Use const node* since we don't change the nodes.
assert(size( ) > 0);
i = (rand( ) % size( )) + 1;
cursor = list_locate(head_ptr, i);
return cursor->data( );
}
void bag::insert(const value_type& entry)
// Library facilities used: node1.h
{
list_head_insert(head_ptr, entry);
++many_nodes;
}
void bag::operator +=(const bag& addend)
// Library facilities used: cstdlib, node1.h
{
node *copy_head_ptr;
node *copy_tail_ptr;
if (addend.many_nodes > 0)
{
list_copy(addend.head_ptr, copy_head_ptr, copy_tail_ptr);
copy_tail_ptr->set_link( head_ptr );
head_ptr = copy_head_ptr;
many_nodes += addend.many_nodes;
}
}
void bag::operator =(const bag& source)
// Library facilities used: node1.h
{
node *tail_ptr; // Needed for argument to list_copy
if (this == &source)
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
bag operator +(const bag& b1, const bag& b2)
{
bag answer;
answer += b1;
answer += b2;
return answer;
}
}
|