Linked List, implementation at Nth node

I have to write a program which has the user be able to enter a specific value at a specific position of the linkedlist, replacing that node with the user defined value

EX)
Enter the value 5 at 2nd node, which will override the old value at the 2nd node with the new one

I am getting a compiler error which terminates my program right after the user presses the return key after he/she has given a position to change the value

Error: Unhandled exception at 0x013C50C1 in Linked(1).exe: 0xC0000005: Access violation reading location 0x0000812B.


I am not going to show the whole code as the problem resides solely on the insert function:

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
 
void TheNode::insert(double num, int choice)
{
	int post = 0;
	MyNode *ptr = new MyNode;
	(*ptr).value = num;
	MyNode *previous = head;
	MyNode *current = head->next;

	while (current != NULL)
	{
		previous = current;
		current = current->next;
		post++;
		if (post == choice)
		{
			ptr = current;
			previous = current->next;
			delete current;
		}
	}
	
	
}

int main()
{
	int pos, val;
	val = 15;
	string index;
	
	TheNode one;
	one.add(10);
	one.add(20);
	one.add(30);
	one.display();
	one.addFront(0);
	one.display();

	cout << "Replace " << val << " at what node position: " << endl;
	cin >> pos;

	one.insert(val, pos);
	one.display();


	system("PAUSE");
	return 0;

	
}
1) How can it be a compiler error if it's happening at run-time?

2) Have you tried stepping through it in a debugger, to see exactly where the crash is happening and what the state of the memory is?
Topic archived. No new replies allowed.