当前提交
4288750a6b
共有 2 个文件被更改,包括 213 次插入0 次删除
  1. 2
    0
      Makefile
  2. 211
    0
      listDArrayAll.cpp

+ 2
- 0
Makefile 查看文件

@@ -0,0 +1,2 @@
1
+all:
2
+	g++ -o listDArrayAll listDArrayAll.cpp -std=c++11

+ 211
- 0
listDArrayAll.cpp 查看文件

@@ -0,0 +1,211 @@
1
+#include <iostream>
2
+#include <cassert>
3
+#include <cstdlib>
4
+
5
+using namespace std;
6
+
7
+typedef int ElementType;
8
+
9
+
10
+class ArrayList {
11
+private: 
12
+  ElementType  *A;
13
+  int length;
14
+  int allocationSize;
15
+
16
+  void resize(int newAllocationSize);
17
+
18
+public:
19
+  ArrayList(int allocSize = 4);
20
+  void append(ElementType val);
21
+  void prepend(ElementType val);
22
+  void insertAfter(int idx, ElementType val);
23
+  int search(ElementType key) const;
24
+  void removeAt(int idx);
25
+
26
+  void display(ostream &out) const;
27
+
28
+  void operator=(const ArrayList& L);
29
+  ~ArrayList();
30
+  ArrayList(const ArrayList& L);
31
+
32
+
33
+};
34
+
35
+ostream& operator<<(ostream &out, const ArrayList &AL);
36
+
37
+// Constructor. The allocsize parameter has a default value of 4.
38
+ArrayList::ArrayList(int allocSize): allocationSize(allocSize) {
39
+  A = new ElementType[allocationSize];
40
+  length = 0;
41
+}
42
+
43
+// resize(): changes the capacity of the dynamic array to the
44
+//           specified allocationSize.
45
+
46
+void ArrayList::resize(int newAllocationSize) {
47
+  int *newA = new ElementType[newAllocationSize];
48
+
49
+  for (int i = 0; i < length && i < newAllocationSize; i++) 
50
+    newA[i] = A[i];
51
+  
52
+  delete A;
53
+  A = newA;
54
+  allocationSize = newAllocationSize;
55
+  length = min(allocationSize, length);
56
+}
57
+
58
+
59
+// append(): given val, inserts it at the end of the list
60
+
61
+void ArrayList::append(ElementType val) {
62
+  if (length == allocationSize -1) 
63
+    resize(allocationSize * 2);
64
+  
65
+  A[length] = val;
66
+  length++;
67
+}
68
+
69
+
70
+// append(): given val, inserts it at the start of the list
71
+
72
+void ArrayList::prepend(ElementType val) {
73
+  if (length == allocationSize -1) 
74
+    resize(allocationSize * 2);
75
+  
76
+  for (int i = length; i > 0; i--) 
77
+    A[i] = A[i-1];
78
+  
79
+  A[0] = val;
80
+  length++;
81
+}
82
+
83
+// insertAfter(): given val inserts it after the index provided
84
+//                idx = -1 means the same as prepend.
85
+
86
+void ArrayList::insertAfter(int idx, ElementType val) {
87
+  if (idx < -1 || idx >= length) 
88
+    cout << "Invalid index for insertion: " << idx << endl;
89
+  else {
90
+    if (length == allocationSize -1) 
91
+      resize(allocationSize * 2);
92
+
93
+    for (int i = length; i > idx + 1; i--) 
94
+      A[i] = A[i-1];
95
+    
96
+    A[idx+1] = val;
97
+    length++;
98
+  }
99
+}
100
+
101
+// search() : given a key, linear search through the list
102
+
103
+int ArrayList::search(ElementType key) const {
104
+  for (int i = 0; i < length; i++) {
105
+    if (A[i] == key) return i;
106
+  }
107
+  return -1;
108
+}
109
+
110
+// removeAt(): given idx, remove that element at that position
111
+
112
+void ArrayList::removeAt(int idx) {
113
+  if (idx < 0 || idx >= length) 
114
+    cout << "Invalid index in removeAt: " << idx << endl;
115
+  else {
116
+    for (int i = idx; i < length - 1; i++)
117
+         A[i] = A[i + 1];
118
+
119
+    length--;
120
+   }
121
+}
122
+
123
+// display(): helper function for display the contents of the list
124
+
125
+void ArrayList::display(ostream &out) const{
126
+  for (int i = 0; i < length; i++)
127
+    cout << A[i] << " ";
128
+}
129
+
130
+// Overload of the operator<<, so that cout << L works!
131
+
132
+ostream& operator<<(ostream &out, const ArrayList &AL) {
133
+  AL.display(out);
134
+  return out;
135
+}   
136
+
137
+// Overload of the assignment operator.  
138
+
139
+void ArrayList::operator=(const ArrayList& L) {
140
+  if (this != &L) {
141
+    length = L.length;
142
+    allocationSize = L.allocationSize;
143
+    delete [] A;
144
+    A = new int[allocationSize];
145
+    for (int i = 0; i < length; i++) {
146
+      A[i] = L.A[i];
147
+    }   
148
+  }
149
+}
150
+
151
+// Destructor 
152
+
153
+ArrayList::~ArrayList(){
154
+  delete [] A;
155
+}
156
+
157
+// Overload of the copy constructor. 
158
+
159
+ArrayList::ArrayList(const ArrayList& L){
160
+  if (this == &L) {
161
+    length = 0; 
162
+    allocationSize = 4;
163
+    A = new int[allocationSize];
164
+  }
165
+  else {
166
+    length = L.length;
167
+    allocationSize = L.allocationSize;
168
+    A = new int[allocationSize];
169
+    for (int i = 0; i < length; i++) {
170
+      A[i] = L.A[i];
171
+    }
172
+  }
173
+}
174
+
175
+
176
+
177
+
178
+// The client provides an interface to run the various list functions. 
179
+// -- a is append, e.g. a 15 : appends a 15 to the list
180
+// -- i is insertAfter, e.g. i 0 20: inserts a 20 after position 0
181
+// -- r is erase, e.g. r 4 removes the element at position 4. 
182
+
183
+int main() {
184
+
185
+  int position, element;
186
+  ArrayList L;
187
+  string inst;
188
+  
189
+  cout << "$ ";
190
+  while (cin >> inst && inst != "q") {
191
+    if (inst == "a") {
192
+      cin >> element;
193
+      cout << "Appending " << element   << endl; 
194
+      L.append(element);
195
+    }
196
+    else if (inst == "i") {
197
+      cin >> position;
198
+      cin >> element;
199
+      cout << "Inserting " << element   << endl; 
200
+      L.insertAfter(position,element);
201
+    }
202
+    else if (inst == "r") {
203
+      cin >> position; 
204
+      cout << "Erasing element after position " << position << endl;
205
+      L.removeAt(position);
206
+    }
207
+    else if (inst == "d")  cout << L << endl;
208
+    cout << "$ ";
209
+  }
210
+  return 0;
211
+}