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

286 рядки
8.1KB

  1. \documentclass[uebung]{../../../lecture}
  2. \title{IPI: Übungsblatt 8}
  3. \author{Samuel Weidemaier, Christian Merten}
  4. \usepackage[]{listings}
  5. \usepackage{xcolor}
  6. \lstdefinestyle{mystyle}{
  7. commentstyle=\color{gray},
  8. language=C++,
  9. keywordstyle=\color{blue},
  10. numberstyle=\tiny\color{gray},
  11. stringstyle=\color{black},
  12. basicstyle=\ttfamily\footnotesize,
  13. breakatwhitespace=false,
  14. breaklines=true,
  15. captionpos=b,
  16. keepspaces=true,
  17. numbers=left,
  18. numbersep=5pt,
  19. showspaces=false,
  20. showstringspaces=false,
  21. showtabs=false,
  22. tabsize=2
  23. }
  24. \lstset{style=mystyle}
  25. \begin{document}
  26. \punkte
  27. \begin{aufgabe} Konstruktoren
  28. \begin{tabular}{llllllllllll}
  29. \textbf{37}: & a int-Konstruktor & b int-Konstruktor \\
  30. \textbf{38}: & c Copy-Konstruktor & d Konstruktor & e Konstruktor \\
  31. \textbf{39}: & d int-Zuweisung \\
  32. \textbf{40}: & T Copy-Konstruktor & T Addition & U int-Konstruktor & V Copy-Konstruktor
  33. & U Destruktor \\
  34. & W Copy-Konstruktor & V Destruktor & d Zuweisung & W Destruktor & T Destruktor\\
  35. \textbf{41}: & d Addition & T int-Konstruktor & U Copy-Konstruktor & T Destruktor & U Addition \\
  36. & V int-Konstruktor & W Copy-Konstruktor & V Destruktor & e Zuweisung & W Destruktor\\
  37. & U Destruktor \\
  38. \textbf{42}: & e Destruktor & d Destruktor & c Destruktor & b Destruktor & a Destruktor \\
  39. \end{tabular}
  40. \end{aufgabe}
  41. \begin{aufgabe}
  42. \begin{enumerate}[(a)]
  43. \item Der Implementierung liegt weiterhin eine einfach verkettete Liste zu Grunde. Konkret
  44. wird die \lstinline{struct IntListElem} verwendet.
  45. Es wird ein Zeiger \lstinline{IntListElem* first} auf das erste Listenelement, eine
  46. Variable \lstinline{int count}, die die Länge der Liste speichert und zwei Hilfsmethoden,
  47. \lstinline{IntListElem* getListElement(int position)}, die einen Zeiger auf das interne
  48. Listenelement zurückgibt und \lstinline{void copy(IntList& other)}, die die Listenelemente
  49. von einer anderen Liste kopiert, verwendet.
  50. Der leere Konstruktor setzt \lstinline{first} und \lstinline{count} auf 0, genauso wie
  51. der Destruktor. Dies führt zur Initialisierung bzw. Leerung der Liste. Der Copy-Konstruktor
  52. bzw. Zuweisungsoperator initialisieren bzw. leeren die Liste ebenfalls zunächst und
  53. kopieren dann mithilfe der \lstinline{copy} Methode, die Elemente einer zweiten Liste.
  54. \item
  55. \begin{lstlisting}[language=C++, title=listclass.cpp, captionpos=b]
  56. #include<stdio.h>
  57. // a list element
  58. struct IntListElem {
  59. int value; // int value
  60. IntListElem* next; // pointer to the next element
  61. };
  62. // A list of integers
  63. // Beware: indexing is done starting from 1 as the excercise sheet requests it
  64. class IntList {
  65. public:
  66. // empty constructor, creates an empty list
  67. IntList();
  68. // copy constructor, intializes by copying from 'other'
  69. IntList(IntList& other);
  70. // assignment operator, removes own elements and copies from 'other'
  71. IntList& operator=(IntList& other);
  72. // destructor, removes list content
  73. ~IntList();
  74. // returns the number of elements in the list
  75. int getCount();
  76. // returns if the list is empty
  77. bool isEmpty();
  78. // prints the list
  79. void print();
  80. // inserts 'element' in the beginning of the list
  81. void insert(int element);
  82. // inserts 'element' AFTER 'position'
  83. void insert(int element, int position);
  84. // removes the element AFTER 'position'
  85. void remove(int position);
  86. // returns the element AT 'position'
  87. int getElement(int position);
  88. private:
  89. // pointer to first element
  90. IntListElem* first;
  91. // element count
  92. int count;
  93. // returns the IntListElem AT 'position'
  94. IntListElem* getListElement(int position);
  95. // Kopiert die Liste 'other'
  96. void copy(IntList& other);
  97. };
  98. // creates new empty list
  99. IntList::IntList() {
  100. first = 0;
  101. count = 0;
  102. }
  103. // creates a new list by copying an old one
  104. IntList::IntList(IntList& list) {
  105. copy(list);
  106. }
  107. // Assignment operator
  108. IntList& IntList::operator=(IntList& other) {
  109. if (this == &other) { // if assigning to the same object
  110. return *this; // do nothing
  111. }
  112. copy(other); // otherwise copy contents
  113. return *this;
  114. }
  115. // destroys the list
  116. IntList::~IntList() {
  117. first = 0; // remove the connection to the list elements
  118. count = 0; // reset list count
  119. };
  120. // private helper that copies from 'other' list
  121. void IntList::copy(IntList& other) {
  122. // clear and copy elements
  123. first = 0;
  124. count = 0;
  125. // copy element by element from 'other' to this new list
  126. for (int i = 1; i <= other.count; i++) {
  127. insert(other.getElement(i), i-1);
  128. }
  129. }
  130. // private helper that returns the list element struct AT 'position'
  131. IntListElem* IntList::getListElement(int position) {
  132. IntListElem* current = first;
  133. for (int i = 1; i < position; i++) {
  134. // if list is not long enough
  135. if (current->next == 0) {
  136. // return the last element
  137. return current;
  138. }
  139. current = current->next;
  140. }
  141. return current;
  142. }
  143. // return element AT 'position'
  144. int IntList::getElement(int position) {
  145. if (position < 1 || position > count) {
  146. printf("Error: Index out of Bounds, no element at position %d\n", position);
  147. return -1;
  148. }
  149. return getListElement(position)->value; // return the value of the element
  150. }
  151. // insert 'element' AFTER 'position'
  152. void IntList::insert(int element, int position) {
  153. if (position < 0 || position > count) { // check for invalid positions
  154. printf("Error: Index out of Bounds, can't insert.\n");
  155. return;
  156. }
  157. IntListElem* elem = new IntListElem(); // create a new list element
  158. elem->value = element; // with the given value
  159. if (position == 0) { // insert in the very beginning
  160. elem->next = first; // the successor of elem is the old first, now second element
  161. first = elem; // make the new element the new first
  162. } else {
  163. IntListElem* where = getListElement(position); // find the element to insert after
  164. elem->next = where->next; // make the old successor of where now follow elem
  165. where->next = elem; // make elem the new successor of where
  166. }
  167. count += 1;
  168. return;
  169. }
  170. // insert 'element' in the very beginning of the list
  171. void IntList::insert(int element) {
  172. insert(element, 0); // insert AFTER position 0
  173. }
  174. // remove next element AFTER 'position'
  175. void IntList::remove(int position) {
  176. if (position < 0 || position >= count) { // check for invalid positions
  177. printf("Error: Index out of bounds, can't remove after %d\n", position);
  178. return;
  179. }
  180. if (position == 0) { // remove first element
  181. first = first->next; // new first is the new element
  182. } else {
  183. IntListElem* where = getListElement(position); // find the element to remove after
  184. where->next = where->next->next; // remove by skipping the next element of where
  185. }
  186. count -= 1; // reduce element count
  187. return;
  188. }
  189. // check if list is empty
  190. bool IntList::isEmpty() {
  191. return count == 0;
  192. }
  193. // returns the list's size
  194. int IntList::getCount() {
  195. return count;
  196. }
  197. // print a list of integers
  198. void IntList::print() {
  199. printf("["); // opening brackets
  200. IntListElem* current = first;
  201. while (current != 0) { // while not last element
  202. printf("%d", current->value); // print value
  203. if (current->next != 0) {
  204. printf(", "); // if not the last element, append a comma
  205. }
  206. current = current->next; // go to next
  207. }
  208. printf("]\n"); // closing brackets
  209. }
  210. int main() {
  211. IntList list;
  212. list.insert(30);
  213. list.insert(20);
  214. list.insert(10);
  215. list.print();
  216. list.remove(2);
  217. list.print();
  218. list.insert(30,2);
  219. list.print();
  220. list.insert(40,3);
  221. list.print();
  222. IntList copy(list);
  223. copy.print();
  224. copy.remove(0);
  225. copy.print();
  226. list.print();
  227. copy = list;
  228. copy.print();
  229. return 0;
  230. }
  231. \end{lstlisting}
  232. \end{enumerate}
  233. \end{aufgabe}
  234. \end{document}