-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunorderedArrayListType.cpp
86 lines (66 loc) · 1.97 KB
/
unorderedArrayListType.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
#include "unorderedArrayListType.h"
#include "arrayListType.h"
void unorderedArrayListType::insertAt(int location,int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted is out of range." << endl;
else if (length >= maxSize)//list is full
cout << "Cannot insert in a full list. " << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at the specified position
length++; //increment the length
} //End else
} //end insertAt
void unorderedArrayListType::insertEnd(int insertItem)
{
if (length >= maxSize)//the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
} //End else
} //end insertEnd
int unorderedArrayListType::seqSearch(int searchItem) const
{
int loc;
bool found = false;
loc = 0;
while (loc < length && !found)
if (list[loc] == searchItem)
found = true;
else
loc++;
if (found)
return loc;
else
return -1;
} //end seqSearch
void unorderedArrayListType::remove(int removeItem)
{
int loc;
if (length == 0)
cout << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list." << endl << endl;
} //End else
} //end remove
void unorderedArrayListType::replaceAt(int location, int repItem)
{
if (location < 0 || location >= length)
cout << "The location of the item to be replaced is out of range." << endl;
else
list[location] = repItem;
} //end replaceAt
unorderedArrayListType::unorderedArrayListType(int size)
: arrayListType(size)
{
} //end constructor