No Description

listDArrayAll.cpp 4.5KB

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