-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListArray.h
60 lines (50 loc) · 1.14 KB
/
ListArray.h
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
#ifndef LISTARRAY_H
//#define LISTARRAY_H
#include <iostream>
#include "ADT.h"
using namespace std;
class ListArray : public ADT {
private:
int* array;
int capacity, size;
void resize() {
int* newArray = new int[capacity * 2];
for (int i = 0; i < size; i++) {
newArray[i] = array[i];
}
delete[] array;
array = newArray;
capacity = capacity * 2;
}
public:
ListArray() {
capacity = 1;
size = 0;
array = new int[capacity];
}
~ListArray() {
delete[] array;
}
void insert(int element) override {
if (size == capacity) {
resize();
}
array[size] = element;
size++;
}
void remove() override {
if (size > 0) {
size--;
} else {
cout << "Error: List is empty, cannot remove." << endl;
}
}
void print() const override {
cout << "List: ";
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
};
#endif