Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // * define as pointer | & address of... to give a pointer a location to access, shows address of a value
- struct TListenKnoten
- {
- int data; //stored value of given node
- TListenKnoten* next; //stores the address of a following TLi structure node,
- TListenKnoten* prev; // new address value to store the previous value
- };
- void hinten_anfuegen(TListenKnoten* &anker,int &prevpointer , const int wert) //needs the structure address of anker and a value to assign
- {
- TListenKnoten* neuer_eintrag = new TListenKnoten; //reserves memory from the heap for the next struct, neuer_eintrag being the next node to be worked on
- TListenKnoten* prev_eintrag = nullptr;
- neuer_eintrag->data = wert; //allocating the three values for the node, its own address, the data it carries and the next point it references
- neuer_eintrag->next = nullptr;
- neuer_eintrag->prev = prevpointer;
- //new data point prev, to acces the node infront of it, anker being the value in front of it
- if (anker == nullptr)
- { //Anker as the "left" end of the list, the latest added value. Opposed to the nullpointer as the right end
- anker = neuer_eintrag;
- anker->prev = nullptr;
- } //this if gets called for the first added value to the list
- else //the else gets called for every subsequent addition to the node list, it assigns the proper values to the anker
- {
- TListenKnoten* ptr = anker; //anker can be seen as the current "selected" position of the node list, anker gets update to the latest value
- while (ptr->next != nullptr) //a while loop to go through the entire list, checking if the next value is the nullpointer
- ptr = ptr->next; //updates the value to check for the next one
- 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
- }
- }
- string liste_als_string(TListenKnoten* anker)
- {
- string resultat = "";
- if (anker == nullptr)
- return "Leere Liste.";
- else
- {
- resultat += "[ ";
- TListenKnoten* ptr = anker; // runs through all the list nodes, with ptr being the current TListenKnoten struct
- while (ptr != nullptr)
- {
- resultat += std::to_string(ptr->data);
- if (ptr->next != nullptr) // if the next node is not the "right" end of the list, aka the nullpointer,
- resultat += " , ";
- else
- resultat += " "; //else add an empty space to end the loop
- ptr = ptr->next; // goes to the next node by updating ptr. Giving it the address of the next node
- }
- resultat += "]";
- }
- return resultat;
- }
- void liste_ausgeben(TListenKnoten* anker) // outputting the values of the entire list, starts with addrss Anker to the latest node member
- {
- cout << liste_als_string(anker) << endl;
- }
- void liste_ausgeben_rueckwaerts(TListenKnoten* anker)
- {
- if (anker == nullptr)
- cout << "Leere Liste.";
- string resultat = "";
- TListenKnoten* ptr = anker;
- while (ptr != nullptr)
- {
- ptr = ptr->next;
- if (ptr->prev == nullptr)
- {
- resultat += "[ ";
- while (ptr != anker)
- {
- resultat += std::to_string(ptr->data);
- if (ptr->prev != nullptr)
- resultat += " , ";
- else
- resultat += " ";
- ptr = ptr->prev;
- }
- resultat += "]";
- }
- cout << resultat << endl;
- }
- }
- int main()
- {
- const int laenge = 10;
- TListenKnoten* anker = nullptr; //the address for the structure called anker is given the nullpointer address value
- int* prevpointer = nullptr;
- liste_ausgeben(anker);
- liste_ausgeben_rueckwaerts(anker);
- for (int i = 0; i < laenge; i++)
- hinten_anfuegen(anker,prevpointer, i);
- liste_ausgeben(anker);
- liste_ausgeben_rueckwaerts(anker);
- system("PAUSE");
- return 0;
- }#include <iostream>
- #include <string>
- using namespace std;
- // * define as pointer | & address of... to give a pointer a location to access, shows address of a value
- struct TListenKnoten
- {
- int data; //stored value of given node
- TListenKnoten* next; //stores the address of a following TLi structure node,
- TListenKnoten* prev; // new address value to store the previous value
- };
- void hinten_anfuegen(TListenKnoten* &anker,int &prevpointer , const int wert) //needs the structure address of anker and a value to assign
- {
- TListenKnoten* neuer_eintrag = new TListenKnoten; //reserves memory from the heap for the next struct, neuer_eintrag being the next node to be worked on
- TListenKnoten* prev_eintrag = nullptr;
- neuer_eintrag->data = wert; //allocating the three values for the node, its own address, the data it carries and the next point it references
- neuer_eintrag->next = nullptr;
- neuer_eintrag->prev = prevpointer;
- //new data point prev, to acces the node infront of it, anker being the value in front of it
- if (anker == nullptr)
- { //Anker as the "left" end of the list, the latest added value. Opposed to the nullpointer as the right end
- anker = neuer_eintrag;
- anker->prev = nullptr;
- } //this if gets called for the first added value to the list
- else //the else gets called for every subsequent addition to the node list, it assigns the proper values to the anker
- {
- TListenKnoten* ptr = anker; //anker can be seen as the current "selected" position of the node list, anker gets update to the latest value
- while (ptr->next != nullptr) //a while loop to go through the entire list, checking if the next value is the nullpointer
- ptr = ptr->next; //updates the value to check for the next one
- 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
- }
- }
- string liste_als_string(TListenKnoten* anker)
- {
- string resultat = "";
- if (anker == nullptr)
- return "Leere Liste.";
- else
- {
- resultat += "[ ";
- TListenKnoten* ptr = anker; // runs through all the list nodes, with ptr being the current TListenKnoten struct
- while (ptr != nullptr)
- {
- resultat += std::to_string(ptr->data);
- if (ptr->next != nullptr) // if the next node is not the "right" end of the list, aka the nullpointer,
- resultat += " , ";
- else
- resultat += " "; //else add an empty space to end the loop
- ptr = ptr->next; // goes to the next node by updating ptr. Giving it the address of the next node
- }
- resultat += "]";
- }
- return resultat;
- }
- void liste_ausgeben(TListenKnoten* anker) // outputting the values of the entire list, starts with addrss Anker to the latest node member
- {
- cout << liste_als_string(anker) << endl;
- }
- void liste_ausgeben_rueckwaerts(TListenKnoten* anker)
- {
- if (anker == nullptr)
- cout << "Leere Liste.";
- string resultat = "";
- TListenKnoten* ptr = anker;
- while (ptr != nullptr)
- {
- ptr = ptr->next;
- if (ptr->prev == nullptr)
- {
- resultat += "[ ";
- while (ptr != anker)
- {
- resultat += std::to_string(ptr->data);
- if (ptr->prev != nullptr)
- resultat += " , ";
- else
- resultat += " ";
- ptr = ptr->prev;
- }
- resultat += "]";
- }
- cout << resultat << endl;
- }
- }
- int main()
- {
- const int laenge = 10;
- TListenKnoten* anker = nullptr; //the address for the structure called anker is given the nullpointer address value
- int* prevpointer = nullptr;
- liste_ausgeben(anker);
- liste_ausgeben_rueckwaerts(anker);
- for (int i = 0; i < laenge; i++)
- hinten_anfuegen(anker,prevpointer, i);
- liste_ausgeben(anker);
- liste_ausgeben_rueckwaerts(anker);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement