Deleting a pointer

Why can't I delete this pointer? I'm trying to delete a member in a list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
struct Node
{
	int value;
	Node *next;
};


int main()
{
	
	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 = NULL;
	}
	aktual->value = 10;
	aktual = start;

	while (aktual)
	{
		cout << aktual->value << endl;
		aktual = aktual->next;
	}
	
	while (aktual)
	{

		//if (aktual->next == NULL)break;
		if(aktual->next->value==4 && aktual->next!=NULL)
		{
			Node *tmp = aktual->next;
			aktual->next = aktual->next->next;
			delete tmp;
			break;
		}

		aktual = aktual->next;
	}


	cout << endl;

	aktual = start;
	while (aktual )
	{
		cout << aktual->value << endl;
		aktual = aktual->next;
	}



	return 0;
}
Last edited on
This is your code, with
1) different order of conditions on your line 33. Why is the order important?
2) A debugging output on your line 31.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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;
	aktual = start;

	while (aktual)
	{
		cout << aktual->value << endl;
		aktual = aktual->next;
	}

	while (aktual)
	{
		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;
		}

		aktual = aktual->next;
	}
	cout << "\n\n";

	aktual = start;
	while (aktual )
	{
		cout << aktual->value << endl;
		aktual = aktual->next;
	}

	return 0;
}

Does this tell you something?


Hint: Your lines 21 and 47 do something that your line 28 does not.


A pedantic note: One does not delete a pointer. One does deallocate memory pointed to by pointer.
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.":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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;
}

Tnx a lot i think i'm starting to understand pointers but there is still a long way to go.
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 ?
1
2
3
4
5
*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. 

Does that help?
yes it does thx a lot :D i have a new topic on pointers so if you'r there :D
Topic archived. No new replies allowed.