Doubly linked list ordered insert

I'm curious as to why I cannot delete pointer cur in my d_ordered_insert function, since isn't it just a copy of the front pointer? (When I uncommented the delete cur; the program crashes.

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
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

struct dll_node//doubly linked-list node
{
  string data_;
  dll_node* next;
  dll_node* back;
};

dll_node* d_ordered_insert(string node_data_, dll_node** front, dll_node** end)
{
	dll_node* cur = *front;//NB: need this ptr to pt to cur potential node where newNode will be inserted before or after the node cur pts to
	dll_node* newNode = new dll_node;
	newNode->data_ = node_data_;
	
	if ( *front == NULL && *end == NULL )//if empty list, then newNode is the front and end of list!
	{
	   *front = newNode; 
	   *end = newNode;
	   newNode->next = NULL;
	   newNode->back = NULL;
	   return *front;
	}

	if ( *front != NULL && cur == *front && newNode->data_ < cur->data_)//if newNode is smallest (cur initially pts to front of list so cur == *front checks that
	{
	   newNode->next = *front;
	   newNode->back = NULL;
	   *front = newNode;
	   // delete cur;//Q: Why can't I delete this? Isn't it a copy of the front ptr?
	   return *front;
	}
	
	for ( ; cur != NULL; cur = cur->next )//if newNode goes somewhere else in list
	{
	   if ( cur->next != NULL && newNode->data_ < cur->data_)//if newNode smaller than some node and not end of list, link in new node
	   {
		(cur->back)->next = newNode;
		newNode->next = cur;
		newNode->back = cur->back;
		return *front;
  	   }
		
	   else if ( cur->next == NULL )//if we are @ EOL (end of list) so new node must be largest node
	   {
		cur->next = newNode;
		newNode->back = cur;
		newNode->next = NULL;
		*end = newNode;
		return *front;
	   }
	}
}

int main()
{
  dll_node* front = NULL;
  dll_node* end = NULL;

  d_ordered_insert("ze", &front, &end);
  d_ordered_insert("a", &front, &end);
  return 0;
}
¿What do you think that delete does?
It makes no sense to kill nodes in an insert function.
Last edited on
But if I want to insert a node that happens to be the smallest, front ptr points to newNode, and I don't use cur, so why can't I just delete it?
Because your not deleting the pointer your deleting the memory it points to. In this case you appear to be adding a node as the new front node then deleting the next node (old front).
Edit: The memory the pointer itself is stored will get freed when it is out of scope.
Last edited on
So we only use delete for deleting dynamically allocated memory, not for deleting pointers?

as in:

1
2
3
4
5
char* list = new list[10];
delete[] list;

int* a = new int;
delete a;


BUT NOT:

1
2
char* ptr_a = &anotherPointer;
char* ptr_b = *somePointerToPointer;


No one wants to help...
Last edited on
Topic archived. No new replies allowed.