forked from Adel-Nasim/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray Based List.txt
207 lines (176 loc) · 3.88 KB
/
Array Based List.txt
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <cassert>
using namespace std;
class arrayListType
{
public:
arrayListType(int size = 100);
arrayListType(arrayListType& otherList); //copy constructor
~arrayListType(); // destructor
bool isEmpty();
bool isFull();
int listSize();
int maxListSize();
void print();
bool isItemAtEqual(int loc, int item);
void insertAt(int loc, int item);
void insertEnd(int item);
void removeAt(int loc);
void retrieveAt(int loc, int& item);
void replaceAt(int loc, int item);
void clearList();
int seqSearch(int item);
void insertNoDuplicate(int item);
void remove(int item);
private:
int *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
arrayListType::arrayListType(int size)
{
/* initilize the private members */
if(size <= 0)
{
cout << " Wrong Size " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int [maxSize];
assert(list != NULL); //terminate if unable to allocate memory space
}
arrayListType::arrayListType(arrayListType& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new int [maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate memory space
for(int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
}
arrayListType::~arrayListType()
{
delete [] list;
}
bool arrayListType::isEmpty()
{
return (length == 0);
}
bool arrayListType::isFull()
{
return (length == maxSize);
}
int arrayListType::listSize()
{
return length;
}
int arrayListType::maxListSize()
{
return maxSize;
}
void arrayListType::print()
{
for(int i = 0; i < length; i++)
cout<<list[i]<<" ";
cout<<endl;
}
bool arrayListType::isItemAtEqual(int loc, int item)
{
if(loc < 0 || loc >= length)
return false;
else
return (list[loc] == item);
}
void arrayListType::insertAt(int loc, int item)
{
if(isFull())
cout<<" The List is Full " << endl;
else if(loc < 0 || loc > length)
cout << "Out of Range " << endl;
else
{
for(int i = length; i > loc; i--)
list[i] = list[i - 1]; //shift right
list[loc] = item; //insert the item at the specified position
length++; //increment the length
}
}
void arrayListType::insertEnd(int item)
{
if(isFull())
cout<<" The List is Full " << endl;
else
list[length++] = item;
}
void arrayListType::retrieveAt(int loc, int& item)
{
if(loc < 0 || loc >= length)
cout << "Out of Range " << endl;
else
item = list[loc];
}
void arrayListType::replaceAt(int loc, int item)
{
if(loc < 0 || loc >= length)
cout << "Out of Range " << endl;
else
list[loc] = item;
}
void arrayListType::clearList()
{
length = 0;
}
int arrayListType::seqSearch(int item)
{
for(int loc = 0; loc < length; loc++)
if(list[loc] == item)
return loc;
return -1;
}
void arrayListType::insertNoDuplicate(int item)
{
if(isFull())
cout<<" The List is Full " << endl;
else
{
int flag = seqSearch(item);
if(flag == -1)
list[length++] = item;
else
cout<<"No duplicates are allowed."<<endl;
}
}
void arrayListType::remove(int item)
{
int loc = seqSearch(item);
if(loc == -1)
cout<<"The item to be deleted is not in the list" << endl;
else
removeAt(loc);
}
void arrayListType::removeAt(int loc)
{
if(loc < 0 || loc >= length)
cout<<"The location of the item to be removed is out of range."<<endl;
else
{
for(int i = loc; i < length - 1; i++)
list[i] = list[i+1];
length--;
}
}
int main()
{
arrayListType lst1;
for(int i = 0; i < 20; i++)
lst1.insertAt(i, i * i);
lst1.print();
int x;
lst1.retrieveAt(10, x);
cout<< x << endl;
arrayListType lst2(lst1);
lst2.print();
return 0;
}