Linked List Implementing

I'm having a serious issue with implementing a node at user given position, and have to decide to implement it left or right.

EX)
lets say the linked list looks as follow:

10 -> 20 -> 30 -> NULL

user wants to implement the value 25, to the RIGHT of the 2nd NODE.

My program crashes when it reaches this stage.

I will not implement the whole code as it's pretty long but just the portion where the issue lies:


The linked list for my program, up until the portion where I have to add left or right at X node looks as such:

15 - > 0 -> 15 -> 10 -> 20 -> 30 -> 60 -> NULL

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
67
68
69
70
71
72
73
74
75
76
77
void TheNode::addbetween(double num, int choice, string choice2)
{
	MyNode *ptr = new MyNode;
	(*ptr).value = num;
	ptr->next = NULL;
	MyNode *current = head;
	MyNode *previous = new MyNode;
	previous = NULL;

	if (choice == 1 && (choice2 == "Left" || choice2 == "left"))
	{
		addFront(num);
	}
	else if (choice == 1 && (choice2 == "right" || choice2 == "Right"))
	{
		ptr->next = head->next;
		head->next = ptr;
	}
	for (int i = 0; i < choice - 1; i++)
	{
		current = current->next;
		previous->next = current;
		if (current->next == NULL)
		{
			break;
		}
		else if (choice2 == "left" || "Left")
			{
				ptr->next = current;
				ptr = previous->next;
			}
		else if (choice2 == "right" || "Right")
			{
				ptr->next = current->next;
				current->next = ptr;
				
			}
	}

	
	current = previous = ptr = NULL;
int main()
{
	int pos, val, val2;
	val = 15;
	val2 = 25;
	string index;
	
	TheNode one;
	one.add(10);
	one.add(20);
	one.add(30);
	one.display();
	one.addFront(0);
	one.addEnd(60);
	one.display();

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

	one.insert(val, pos);
	one.display();
	
	cout << "Insert " << val2 << " left or right: " << endl;
	cin >> index;
	cout << "At what node position: " << endl;
	cin >> pos;

	one.addbetween(val2, pos, index);
	one.display();

	system("PAUSE");
	return 0;

	
}
On line 7 you allocate memory and then on line 8 you immediately discard that memory forever. That memory will stay allocated until the program dies. This is called a memory leak, and preventing memory leaks is a good habit to learn from the beginning.

Is there missing code? I believe there is a missing } in the code that you posted.
I think the addFront, add, and addEnd functions are missing from what you posted.
It would be easier to help you with more of your code present.
Last edited on
I heard that I had to set previous = NULL as a method of safe keeping.

But in fact it is a dangling pointer? interesting
Also, I just saw this code current = previous = ptr = NULL;
A better way to write that would be using multiple lines of code. One assignment per line. IMO this line is unnecessary since these are all local variables within the addBetween function.
Hi Forum, It is very nice discussion about the linked list
for more information refer http://www.cpptutorials.com/2015/01/reversing-linked-list-in-cc-with.html#uds-search-results
Last edited on
Topic archived. No new replies allowed.