Help with doubly linked list

Hi! I'm having trouble with implementing the remove method of my doubly linked list. Here's my code:
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once

#include <cassert>

template<typename T>
struct LinkedNode
{
	
	T data;
	LinkedNode* next;
	LinkedNode* prev;

};





template<typename T>
class LinkedList
{
public:
	LinkedList(void);
	~LinkedList(void);
	LinkedList(const LinkedList& rhs);
	LinkedList& operator=(LinkedList& rhs);

	bool isEmpty();
	LinkedNode<T>* getFirst();
	LinkedNode<T>* getLast();

	void insertFirst(T data);
	void insertLast(T data);
	bool insertAfter(T key,T data);
	void insertBefore(T key,T data);
	void removeFirst();
	void removeLast();
	bool remove(T removalCandidate);
	void destroy();


private:
	LinkedNode<T>* mLast;
	LinkedNode<T>* mFirst;

	


};

template<typename T>
LinkedList<T>::LinkedList(void)
{
	mFirst=0;
	mLast=0;
}

template<typename T>
LinkedList<T>::~LinkedList(void)
{
}

template<typename T>
LinkedNode<T>* LinkedList<T>::getFirst()
{
	return mFirst;
}

template<typename T>
LinkedNode<T>* LinkedList<T>::getLast()
{
	return mlast;
}


template<typename T>
bool LinkedList<T>::isEmpty()
{
	if(!mLast && !mFirst) return true;

	return false;
}


template<typename T>
void LinkedList<T>::insertFirst(T data)
{
	LinkedNode<T>* newNode=new LinkedNode<T>;
	newNode->data=data;

	if(isEmpty())
	{
		mLast=newNode;
	}

	else
	{
		mFirst->prev=newNode;
	}
	newNode->next=mFirst;
	mFirst=newNode;
}

template<typename T>
bool LinkedList<T>::insertAfter(T key,T data)
{
	if(isEmpty()) return false;


	LinkedNode<T>* current=mFirst;

	while(current->data!=key)
	{

		current=current->next;


		if(current==0) return false;


	}

	LinkedNode<T>* newNode=new LinkedNode<T> ;
	newNode->data=data;

	if(current==mLast)
	{
		newNode->next=0;
		mLast=newNode;

	}

	else
	{
		newNode->next=current->next;
		current->next->prev=newNode;
	}



	newNode->prev=current;
	current->next=newNode;


}


template<typename T>
bool LinkedList<T>::remove(T removalCandidate)
{

	if(isEmpty())return false;

	LinkedNode<T>* current=mFirst;

	while(current->data!=removalCandidate)
	{

		current=current->next;


		if(current==0) return false;
	}

	if(current==mLast)
	{
		mLast=current->prev;
		current->next=0;

	}

	if(current==mFirst)
	{
		mFirst=current->next;
		current->prev=-0;
	}
	
		
	else
	{
		current->prev->next=current->next;
		current->next->prev=current->prev;
	}
	
		
	
	delete current;
	current=0;
	

	return true;

}


I'm getting something like:
First-chance exception at 0x011516b5 in STL.exe: 0xC0000005: Access violation reading location 0x00000000.




I'm getting this stuff from a book but I have no way to check my work. Thanks!
It looks like you never set the next and prev pointers to NULL (not zero btw), so Line 159 goes beyond your list and then tries to use it as its own.

I would change the Node struct, and also the code when you make new

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<typename T>
struct LinkedNode
{
  data;
  LinkedNode* next;
  LinkedNode* prev;

  LinkedNode(T data, LinkedNode* next = NULL, LinkedNode* prev = NULL)
  {
    this.data = data;
    this.next = next;
    this.prev = prev;
  }

};


Last edited on
Topic archived. No new replies allowed.