-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.cpp
113 lines (93 loc) · 2.58 KB
/
Table.cpp
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
// implement table to store records
class Table
{
public:
Table(int = 10); // default constructor
Table(const Table &); // copy constructor
~Table(); // destructor
const Table &operator=(const Table &); // assignment operator
void insertRecord(int, const Record &); // insert record
bool deleteRecord(int); // delete record
bool retrieveRecord(int, Record &) const; // retrieve record
int getNumberOfRecords() const; // get number of records
void printTable() const; // print table
private:
int size; // size of table
int numberOfRecords; // number of records in table
Record *ptr; // pointer to table
}; // end class Table
Table::Table(int tableSize)
{
size = tableSize;
numberOfRecords = 0;
ptr = new Record[size];
}
Table::Table(const Table &tableToCopy)
{
size = tableToCopy.size;
numberOfRecords = tableToCopy.numberOfRecords;
ptr = new Record[size];
for (int i = 0; i < numberOfRecords; i++)
ptr[i] = tableToCopy.ptr[i];
}
Table::~Table()
{
delete[] ptr;
}
const Table &Table::operator=(const Table &right)
{
if (&right != this)
{
if (size != right.size)
{
delete[] ptr;
size = right.size;
ptr = new Record[size];
} // end inner if
numberOfRecords = right.numberOfRecords;
for (int i = 0; i < numberOfRecords; i++)
ptr[i] = right.ptr[i];
} // end outer if
return *this;
}
void Table::insertRecord(int key, const Record &value)
{
int newRecord = numberOfRecords;
ptr[newRecord].setKey(key);
ptr[newRecord].setValue(value.getValue());
numberOfRecords++;
}
bool Table::deleteRecord(int key)
{
int i;
for (i = 0; i < numberOfRecords; i++)
if (ptr[i].getKey() == key)
break;
if (i == numberOfRecords)
return false;
for (int j = i; j < numberOfRecords - 1; j++)
ptr[j] = ptr[j + 1];
numberOfRecords--;
return true;
}
bool Table::retrieveRecord(int key, Record &value) const
{
for (int i = 0; i < numberOfRecords; i++)
if (ptr[i].getKey() == key)
{
value = ptr[i].getValue();
return true;
} // end if
return false;
}
int Table::getNumberOfRecords() const
{
return numberOfRecords;
}
void Table::printTable() const
{
for (int i = 0; i < numberOfRecords; i++)
{
cout << ptr[i].getKey() << ' ' << ptr[i].getValue() << endl;
} // end for
}