### Help with doubly linked list

Hi! I'm having trouble with implementing the remove method of my doubly linked list. Here's my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193`` ``````#pragma once #include template struct LinkedNode { T data; LinkedNode* next; LinkedNode* prev; }; template class LinkedList { public: LinkedList(void); ~LinkedList(void); LinkedList(const LinkedList& rhs); LinkedList& operator=(LinkedList& rhs); bool isEmpty(); LinkedNode* getFirst(); LinkedNode* 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* mLast; LinkedNode* mFirst; }; template LinkedList::LinkedList(void) { mFirst=0; mLast=0; } template LinkedList::~LinkedList(void) { } template LinkedNode* LinkedList::getFirst() { return mFirst; } template LinkedNode* LinkedList::getLast() { return mlast; } template bool LinkedList::isEmpty() { if(!mLast && !mFirst) return true; return false; } template void LinkedList::insertFirst(T data) { LinkedNode* newNode=new LinkedNode; newNode->data=data; if(isEmpty()) { mLast=newNode; } else { mFirst->prev=newNode; } newNode->next=mFirst; mFirst=newNode; } template bool LinkedList::insertAfter(T key,T data) { if(isEmpty()) return false; LinkedNode* current=mFirst; while(current->data!=key) { current=current->next; if(current==0) return false; } LinkedNode* newNode=new LinkedNode ; 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 bool LinkedList::remove(T removalCandidate) { if(isEmpty())return false; LinkedNode* 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`

 ``123456789101112131415`` ``````template 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.