Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

211 строки
5.4KB

  1. #include<stdio.h>
  2. // a list element
  3. struct IntListElem {
  4. int value; // int value
  5. IntListElem* next; // pointer to the next element
  6. };
  7. // A list of integers
  8. // Beware: indexing is done starting from 1 as the excercise sheet requests it
  9. class IntList {
  10. public:
  11. // empty constructor, creates an empty list
  12. IntList();
  13. // copy constructor, intializes by copying from 'other'
  14. IntList(IntList& other);
  15. // assignment operator, removes own elements and copies from 'other'
  16. IntList& operator=(IntList& other);
  17. // destructor, removes list content
  18. ~IntList();
  19. // returns the number of elements in the list
  20. int getCount();
  21. // returns if the list is empty
  22. bool isEmpty();
  23. // prints the list
  24. void print();
  25. // inserts 'element' in the beginning of the list
  26. void insert(int element);
  27. // inserts 'element' AFTER 'position'
  28. void insert(int element, int position);
  29. // removes the element AFTER 'position'
  30. void remove(int position);
  31. // returns the element AT 'position'
  32. int getElement(int position);
  33. private:
  34. // pointer to first element
  35. IntListElem* first;
  36. // element count
  37. int count;
  38. // returns the IntListElem AT 'position'
  39. IntListElem* getListElement(int position);
  40. // Kopiert die Liste 'other'
  41. void copy(IntList& other);
  42. };
  43. // creates new empty list
  44. IntList::IntList() {
  45. first = 0;
  46. count = 0;
  47. }
  48. // creates a new list by copying an old one
  49. IntList::IntList(IntList& list) {
  50. copy(list);
  51. }
  52. // Assignment operator
  53. IntList& IntList::operator=(IntList& other) {
  54. if (this == &other) { // if assigning to the same object
  55. return *this; // do nothing
  56. }
  57. copy(other); // otherwise copy contents
  58. return *this;
  59. }
  60. // destroys the list
  61. IntList::~IntList() {
  62. first = 0; // remove the connection to the list elements
  63. count = 0; // reset list count
  64. };
  65. // private helper that copies from 'other' list
  66. void IntList::copy(IntList& other) {
  67. // clear and copy elements
  68. first = 0;
  69. count = 0;
  70. // copy element by element from 'other' to this new list
  71. for (int i = 1; i <= other.count; i++) {
  72. insert(other.getElement(i), i-1);
  73. }
  74. }
  75. // private helper that returns the list element struct AT 'position'
  76. IntListElem* IntList::getListElement(int position) {
  77. IntListElem* current = first;
  78. for (int i = 1; i < position; i++) {
  79. // if list is not long enough
  80. if (current->next == 0) {
  81. // return the last element
  82. return current;
  83. }
  84. current = current->next;
  85. }
  86. return current;
  87. }
  88. // return element AT 'position'
  89. int IntList::getElement(int position) {
  90. if (position < 1 || position > count) {
  91. printf("Error: Index out of Bounds, no element at position %d\n", position);
  92. return -1;
  93. }
  94. return getListElement(position)->value; // return the value of the element
  95. }
  96. // insert 'element' AFTER 'position'
  97. void IntList::insert(int element, int position) {
  98. if (position < 0 || position > count) { // check for invalid positions
  99. printf("Error: Index out of Bounds, can't insert.\n");
  100. return;
  101. }
  102. IntListElem* elem = new IntListElem(); // create a new list element
  103. elem->value = element; // with the given value
  104. if (position == 0) { // insert in the very beginning
  105. elem->next = first; // the successor of elem is the old first, now second element
  106. first = elem; // make the new element the new first
  107. } else {
  108. IntListElem* where = getListElement(position); // find the element to insert after
  109. elem->next = where->next; // make the old successor of where now follow elem
  110. where->next = elem; // make elem the new successor of where
  111. }
  112. count += 1;
  113. return;
  114. }
  115. // insert 'element' in the very beginning of the list
  116. void IntList::insert(int element) {
  117. insert(element, 0); // insert AFTER position 0
  118. }
  119. // remove next element AFTER 'position'
  120. void IntList::remove(int position) {
  121. if (position < 0 || position >= count) { // check for invalid positions
  122. printf("Error: Index out of bounds, can't remove after %d\n", position);
  123. return;
  124. }
  125. if (position == 0) { // remove first element
  126. first = first->next; // new first is the new element
  127. } else {
  128. IntListElem* where = getListElement(position); // find the element to remove after
  129. where->next = where->next->next; // remove by skipping the next element of where
  130. }
  131. count -= 1; // reduce element count
  132. return;
  133. }
  134. // check if list is empty
  135. bool IntList::isEmpty() {
  136. return count == 0;
  137. }
  138. // returns the list's size
  139. int IntList::getCount() {
  140. return count;
  141. }
  142. // print a list of integers
  143. void IntList::print() {
  144. printf("["); // opening brackets
  145. IntListElem* current = first;
  146. while (current != 0) { // while not last element
  147. printf("%d", current->value); // print value
  148. if (current->next != 0) {
  149. printf(", "); // if not the last element, append a comma
  150. }
  151. current = current->next; // go to next
  152. }
  153. printf("]\n"); // closing brackets
  154. }
  155. int main() {
  156. IntList list;
  157. list.insert(30);
  158. list.insert(20);
  159. list.insert(10);
  160. list.print();
  161. list.remove(2);
  162. list.print();
  163. list.insert(30,2);
  164. list.print();
  165. list.insert(40,3);
  166. list.print();
  167. IntList copy(list);
  168. copy.print();
  169. copy.remove(0);
  170. copy.print();
  171. list.print();
  172. copy = list;
  173. copy.print();
  174. return 0;
  175. }