-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.cpp
More file actions
134 lines (116 loc) · 3.61 KB
/
ArrayList.cpp
File metadata and controls
134 lines (116 loc) · 3.61 KB
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
// Created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
/** Implementation file for the class ArrayList.
@file ArrayList.cpp */
#include "ArrayList.h" // Header file
template<class ItemType>
ArrayList<ItemType>::ArrayList() : itemCount(0), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor
template<class ItemType>
bool ArrayList<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
int ArrayList<ItemType>::getLength() const
{
return itemCount;
} // end getLength
template<class ItemType>
bool ArrayList<ItemType>::insert(int newPosition, const ItemType& newEntry)
{
bool ableToInsert = (newPosition >= 1)
&& (newPosition <= itemCount + 1)
&& (itemCount < maxItems);
if (ableToInsert)
{
// Make room for new entry by shifting all entries at
// positions >= newPosition toward the end of the array
// (no shift if newPosition == itemCount + 1)
for (int entryPosition = itemCount; entryPosition >= newPosition; entryPosition--)
items[entryPosition + 1] = items[entryPosition]; // copy the entry right
// Insert new entry
items[newPosition] = newEntry;
itemCount++; // Increase count of entries
} // end if
return ableToInsert;
} // end insert
template<class ItemType>
bool ArrayList<ItemType>::remove(int position)
{
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
// Remove entry by shifting all entries after the one at
// position toward the beginning of the array
// (no shift if position == itemCount)
for (int entryPosition = position; entryPosition < itemCount; entryPosition++)
items[entryPosition] = items[entryPosition + 1]; // copy entry on the right to left
itemCount--; // Decrease count of entries
} // end if
return ableToRemove;
} // end remove
template<class ItemType>
void ArrayList<ItemType>::clear()
{
itemCount = 0;
} // end clear
template<class ItemType>
ItemType ArrayList<ItemType>::getEntry(int position) const throw(PrecondViolatedExcep)
{
// Enforce precondition
bool ableToGet = (position >= 1) && (position <= itemCount);
if (ableToGet)
{
return items[position];
}
else
{
std::string message = "getEntry() called with an empty list or ";
message = message + "invalid position.";
throw(PrecondViolatedExcep(message));
} // end if
} // end getEntry
template<class ItemType>
void ArrayList<ItemType>::replace(int position, const ItemType& newEntry) throw(PrecondViolatedExcep)
{
// Enforce precondition
bool ableToSet = (position >= 1) && (position <= itemCount);
if (ableToSet)
{
items[position] = newEntry;
}
else
{
std::string message = "replace() called with an empty list or ";
message = message + "invalid position.";
throw(PrecondViolatedExcep(message));
} // end if
} // end replace
//print out the list
template<class ItemType>
void ArrayList<ItemType>::print()
{
{
std::cout << "The Array list contains " << std::endl;
for (int pos = 1; pos <= itemCount; pos++)
{
try
{
std::cout << items[pos] << " ";
}
catch (PrecondViolatedExcep except)
{
std::cout << "Exception thrown getting entry inserted at position " << pos << std::endl;
std::cout << except.what() << std::endl;
}
} // end for
std::cout << std::endl;
} // end displayList
}
// End of implementation file.
template<class ItemType>
void ArrayList<ItemType>::sorting()
{
}