Double Linked List, insert before current

I need to insert a node before the current pointer, however when I try to insert a node, it will produce an unhandled exception error @ line 117


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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>

using namespace std;
typedef int ItemType;
class NodeType
{
private:
	ItemType item;
	NodeType *next;
	NodeType *prev;
	friend class Double;
};
class Double
{
private:
	NodeType *head;
	NodeType *current;
	int position;
public:
	Double();
	void makeEmpty();
	bool isempty() const;
	bool isAtEnd(); 
	void goToStart();
	void goToNext();
	ItemType CurrentItem();
	void insert(ItemType);
	void deleteCurrentItem();
	int currentPosition();
};
Double::Double()
{
	head = NULL;
	current = NULL;
	position = 1;
}
void Double::makeEmpty()
{
	if (isempty())
	{
		cout << "List is empty!" << endl;
	}
	else
	{
		do
		{
			NodeType *garbage = current;
			current = current->next;
			current->prev = NULL;
			garbage->next = NULL;
			delete garbage;
			head = current;
			position--;
		} while (!isempty());
		//when list is empty
		position = 1;
	}
	current = NULL;
}
bool Double::isempty() const
{
	return (head == NULL); 
}
bool Double::isAtEnd()
{
	return (isempty() || current->next == NULL); //true if empty, or if next pointer is null
}
void Double::goToStart()
{
	if (isempty())
	{
		cout << "List is empty!" << endl;
	}
	else
	{
		current = head; //move to the start
		position = 1;
	}
}
void Double::goToNext()
{
	while (current)
	{
		if (!isAtEnd() && !isempty())
		{
			current = current->next;
			position++;
		}
		else if (isAtEnd())
		{
			return;
		}
	}
}
ItemType Double::CurrentItem()
{
	if (!isAtEnd() && !isempty())
	{
		return current->item;
	}
	
}
void Double::insert(ItemType newitem)
{

	NodeType *ptr = new NodeType;
	ptr->item = newitem;
	ptr->prev = NULL;
	ptr->next = NULL;
	if (isempty())
	{	
		head = ptr;
		goToStart();
	}
	else
	{
		(current->prev)->next = ptr;
		ptr->prev = current->prev;
		ptr->next = current;
		current->prev = ptr;
		position++;
	}
	ptr = NULL;
}
void Double::deleteCurrentItem()
{
	if (!isempty()) //if not empty and not at the end
	{
		NodeType *garbage = current;
		goToNext(); //moves current to next
		current->prev = garbage->prev;
		(garbage->prev)->next = current;
		garbage->prev = NULL;
		garbage->next = NULL;
		garbage = NULL;
		delete garbage;
		position--;
	}
	else
	{
		cout << "Empty list, nothing to delete!" << endl;
	}
}
int Double::currentPosition()
{
	return position;
}

void print(Double test);


int main()
{

	Double one;
	one.goToStart();
	cout << one.currentPosition() << endl; //to check if pointer works
	one.insert(5);
	one.insert(6);
	one.insert(10);
	one.insert(50);
	print(one);

	system("PAUSE");
	return 0;

}

void print(Double test)
{
	cout << "Current items in the list:" << endl;
	while (!test.isempty() || !test.isAtEnd())
	{
		test.goToStart();
		cout << test.CurrentItem() << endl;
		test.goToNext();
	}
}
Last edited on
current is not a good idea, because you don't know what it is.

You can find a reason for a crash on line 117. What happens if current->prev == NULL?

That happens when exactly one node exists and you try to add another.
Topic archived. No new replies allowed.