I did not set the actual ptr on start :D so when i type delete tmp the next value will be allocated in this case instead of aktual->next there will be allocated aktual->next->next?
When iterating through a list, consider using a for loop to express the iteration. This way you separate the code that "does the loop" from the code that "executes in the loop.":
#include <iostream>
struct Node
{
int value;
Node *next;
};
int
main()
{
using std::cout;
using std::endl;
Node *start = new Node;
Node *aktual = start;
for (int i = 0; i < 10; i++) {
aktual->value = i;
aktual->next = new Node;
aktual = aktual->next;
aktual->next = nullptr;
}
aktual->value = 10;
for (aktual = start; aktual; aktual = aktual->next) {
cout << aktual->value << endl;
}
for (aktual = start; aktual; aktual = aktual->next) {
if (aktual->next)
cout << " Val " << aktual->next->value;
if (aktual->next and 4 == aktual->next->value) {
Node *tmp = aktual->next;
aktual->next = aktual->next->next;
delete tmp;
break;
}
}
cout << "\n\n";
for (aktual = start; aktual; aktual = aktual->next) {
cout << aktual->value << endl;
}
return 0;
}
Once you're comfortable with pointers, you can greatly simplify the list code by using a headOrNext pointer. This is a pointer to the pointer to the current node. In other words, it points to the head of the list (start in your case), or the next pointer of some node. Doing this lets you delete the first item in the list without special code. Right now your code can't delete that item.
#include <iostream>
struct Node
{
int value;
Node *next;
};
int
main()
{
using std::cout;
using std::endl;
Node *start = nullptr;
Node *cur; // current node
Node **startOrNext = &start;;
for (int i = 0; i <= 10; i++) {
// Note that this creates the new node as you insert it.
// Your previous code always created the next node after
// the one whose value you set - very confusing.
*startOrNext = new Node;
cur = *startOrNext;
cur->value = i;
cur->next = nullptr;
startOrNext = &cur->next;
}
for (cur = start; cur; cur = cur->next) {
cout << cur->value << endl;
}
// This loop interates startOrNext through the pointers
// and sets cur along the way. Note that the test part
// assigns cur = *startOrNext, and then if cur is nullptr
// it will exit the loop.
for (startOrNext = &start; (cur = *startOrNext); startOrNext = &cur->next) {
cout << " Val " << cur->value;
if (4 == cur->value) {
Node *tmp = cur;
*startOrNext = cur->next;
delete tmp;
break;
}
}
cout << "\n\n";
for (cur = start; cur; cur = cur->next) {
cout << cur->value << endl;
}
return 0;
}
In the second code you are using headOrNext pointer (StartorNext) to allocate new Node and then with cut pointer you initialise the value and then initialise cur to StartorNext ?
*startOrNext = new Node; // create a new node and add it at the end of the list
cur = *startOrNext; // point cur to the new node.
cur->value = i; // set the value
cur->next = nullptr; // Set the next pointer
startOrNext = &cur->next; // set startOrNext to point to the next pointer.