Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // * define as pointer | & address of... to give a pointer a location to access, shows address of a value
  5. struct TListenKnoten
  6.  
  7. {
  8. int data; //stored value of given node
  9. TListenKnoten* next; //stores the address of a following TLi structure node,
  10. TListenKnoten* prev; // new address value to store the previous value
  11. };
  12.  
  13. void hinten_anfuegen(TListenKnoten* &anker,int &prevpointer , const int wert) //needs the structure address of anker and a value to assign
  14. {
  15. TListenKnoten* neuer_eintrag = new TListenKnoten; //reserves memory from the heap for the next struct, neuer_eintrag being the next node to be worked on
  16. TListenKnoten* prev_eintrag = nullptr;
  17.  
  18. neuer_eintrag->data = wert; //allocating the three values for the node, its own address, the data it carries and the next point it references
  19. neuer_eintrag->next = nullptr;
  20. neuer_eintrag->prev = prevpointer;
  21.  
  22. //new data point prev, to acces the node infront of it, anker being the value in front of it
  23. if (anker == nullptr)
  24. { //Anker as the "left" end of the list, the latest added value. Opposed to the nullpointer as the right end
  25. anker = neuer_eintrag;
  26. anker->prev = nullptr;
  27. } //this if gets called for the first added value to the list
  28. else //the else gets called for every subsequent addition to the node list, it assigns the proper values to the anker
  29. {
  30. TListenKnoten* ptr = anker; //anker can be seen as the current "selected" position of the node list, anker gets update to the latest value
  31. while (ptr->next != nullptr) //a while loop to go through the entire list, checking if the next value is the nullpointer
  32. ptr = ptr->next; //updates the value to check for the next one
  33. ptr->next = neuer_eintrag; //after reaching the end of the list, with the next one being the nullptr, the new node is added between the nullptr and the previous node
  34.  
  35.  
  36. }
  37.  
  38. }
  39.  
  40. string liste_als_string(TListenKnoten* anker)
  41. {
  42. string resultat = "";
  43. if (anker == nullptr)
  44. return "Leere Liste.";
  45. else
  46. {
  47. resultat += "[ ";
  48. TListenKnoten* ptr = anker; // runs through all the list nodes, with ptr being the current TListenKnoten struct
  49. while (ptr != nullptr)
  50. {
  51. resultat += std::to_string(ptr->data);
  52. if (ptr->next != nullptr) // if the next node is not the "right" end of the list, aka the nullpointer,
  53. resultat += " , ";
  54. else
  55. resultat += " "; //else add an empty space to end the loop
  56. ptr = ptr->next; // goes to the next node by updating ptr. Giving it the address of the next node
  57. }
  58. resultat += "]";
  59. }
  60. return resultat;
  61. }
  62.  
  63. void liste_ausgeben(TListenKnoten* anker) // outputting the values of the entire list, starts with addrss Anker to the latest node member
  64. {
  65. cout << liste_als_string(anker) << endl;
  66. }
  67.  
  68. void liste_ausgeben_rueckwaerts(TListenKnoten* anker)
  69. {
  70. if (anker == nullptr)
  71. cout << "Leere Liste.";
  72.  
  73. string resultat = "";
  74.  
  75. TListenKnoten* ptr = anker;
  76. while (ptr != nullptr)
  77. {
  78. ptr = ptr->next;
  79. if (ptr->prev == nullptr)
  80. {
  81. resultat += "[ ";
  82.  
  83. while (ptr != anker)
  84. {
  85. resultat += std::to_string(ptr->data);
  86. if (ptr->prev != nullptr)
  87. resultat += " , ";
  88. else
  89. resultat += " ";
  90. ptr = ptr->prev;
  91. }
  92. resultat += "]";
  93. }
  94. cout << resultat << endl;
  95. }
  96. }
  97.  
  98.  
  99.  
  100. int main()
  101. {
  102. const int laenge = 10;
  103. TListenKnoten* anker = nullptr; //the address for the structure called anker is given the nullpointer address value
  104. int* prevpointer = nullptr;
  105. liste_ausgeben(anker);
  106. liste_ausgeben_rueckwaerts(anker);
  107.  
  108. for (int i = 0; i < laenge; i++)
  109. hinten_anfuegen(anker,prevpointer, i);
  110.  
  111. liste_ausgeben(anker);
  112. liste_ausgeben_rueckwaerts(anker);
  113. system("PAUSE");
  114. return 0;
  115. }#include <iostream>
  116. #include <string>
  117. using namespace std;
  118. // * define as pointer | & address of... to give a pointer a location to access, shows address of a value
  119. struct TListenKnoten
  120.  
  121. {
  122. int data; //stored value of given node
  123. TListenKnoten* next; //stores the address of a following TLi structure node,
  124. TListenKnoten* prev; // new address value to store the previous value
  125. };
  126.  
  127. void hinten_anfuegen(TListenKnoten* &anker,int &prevpointer , const int wert) //needs the structure address of anker and a value to assign
  128. {
  129. TListenKnoten* neuer_eintrag = new TListenKnoten; //reserves memory from the heap for the next struct, neuer_eintrag being the next node to be worked on
  130. TListenKnoten* prev_eintrag = nullptr;
  131.  
  132. neuer_eintrag->data = wert; //allocating the three values for the node, its own address, the data it carries and the next point it references
  133. neuer_eintrag->next = nullptr;
  134. neuer_eintrag->prev = prevpointer;
  135.  
  136. //new data point prev, to acces the node infront of it, anker being the value in front of it
  137. if (anker == nullptr)
  138. { //Anker as the "left" end of the list, the latest added value. Opposed to the nullpointer as the right end
  139. anker = neuer_eintrag;
  140. anker->prev = nullptr;
  141. } //this if gets called for the first added value to the list
  142. else //the else gets called for every subsequent addition to the node list, it assigns the proper values to the anker
  143. {
  144. TListenKnoten* ptr = anker; //anker can be seen as the current "selected" position of the node list, anker gets update to the latest value
  145. while (ptr->next != nullptr) //a while loop to go through the entire list, checking if the next value is the nullpointer
  146. ptr = ptr->next; //updates the value to check for the next one
  147. ptr->next = neuer_eintrag; //after reaching the end of the list, with the next one being the nullptr, the new node is added between the nullptr and the previous node
  148.  
  149.  
  150. }
  151.  
  152. }
  153.  
  154. string liste_als_string(TListenKnoten* anker)
  155. {
  156. string resultat = "";
  157. if (anker == nullptr)
  158. return "Leere Liste.";
  159. else
  160. {
  161. resultat += "[ ";
  162. TListenKnoten* ptr = anker; // runs through all the list nodes, with ptr being the current TListenKnoten struct
  163. while (ptr != nullptr)
  164. {
  165. resultat += std::to_string(ptr->data);
  166. if (ptr->next != nullptr) // if the next node is not the "right" end of the list, aka the nullpointer,
  167. resultat += " , ";
  168. else
  169. resultat += " "; //else add an empty space to end the loop
  170. ptr = ptr->next; // goes to the next node by updating ptr. Giving it the address of the next node
  171. }
  172. resultat += "]";
  173. }
  174. return resultat;
  175. }
  176.  
  177. void liste_ausgeben(TListenKnoten* anker) // outputting the values of the entire list, starts with addrss Anker to the latest node member
  178. {
  179. cout << liste_als_string(anker) << endl;
  180. }
  181.  
  182. void liste_ausgeben_rueckwaerts(TListenKnoten* anker)
  183. {
  184. if (anker == nullptr)
  185. cout << "Leere Liste.";
  186.  
  187. string resultat = "";
  188.  
  189. TListenKnoten* ptr = anker;
  190. while (ptr != nullptr)
  191. {
  192. ptr = ptr->next;
  193. if (ptr->prev == nullptr)
  194. {
  195. resultat += "[ ";
  196.  
  197. while (ptr != anker)
  198. {
  199. resultat += std::to_string(ptr->data);
  200. if (ptr->prev != nullptr)
  201. resultat += " , ";
  202. else
  203. resultat += " ";
  204. ptr = ptr->prev;
  205. }
  206. resultat += "]";
  207. }
  208. cout << resultat << endl;
  209. }
  210. }
  211.  
  212.  
  213.  
  214. int main()
  215. {
  216. const int laenge = 10;
  217. TListenKnoten* anker = nullptr; //the address for the structure called anker is given the nullpointer address value
  218. int* prevpointer = nullptr;
  219. liste_ausgeben(anker);
  220. liste_ausgeben_rueckwaerts(anker);
  221.  
  222. for (int i = 0; i < laenge; i++)
  223. hinten_anfuegen(anker,prevpointer, i);
  224.  
  225. liste_ausgeben(anker);
  226. liste_ausgeben_rueckwaerts(anker);
  227. system("PAUSE");
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement