Why does my C++ LinkedList cause a EXC_BAD_ACCESS?
- by Anthony Glyadchenko
When I call the cmremoveNode method in my LinkedList from outside code, I get an EXC_BAD_ACCESS.
/*
* LinkedList.h
* Lab 6
*
* Created by Anthony Glyadchenko on 3/22/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class ctNode {
friend class ctlinkList ; // friend class allowed to access private data
private:
string sfileWord ; // used to allocate and store input word
int iwordCnt ; // number of word occurrances
ctNode* ctpnext ; // point of Type Node, points to next link list element
};
class ctlinkList {
private:
ctNode* ctphead ; // initialized by constructor
public:
ctlinkList () { ctphead = NULL ; }
ctNode* gethead () { return ctphead ; }
string cminsertNode (string svalue) {
ctNode* ctptmpHead = ctphead ;
if ( ctphead == NULL ) { // allocate new and set head
ctptmpHead = ctphead = new ctNode ;
ctphead -> ctpnext = NULL ;
ctphead -> sfileWord = svalue ;
} else { //find last ctnode
do {
if ( ctptmpHead -> ctpnext != NULL ) ctptmpHead = ctptmpHead -> ctpnext ;
} while ( ctptmpHead -> ctpnext != NULL ) ; // fall thru found last node
ctptmpHead -> ctpnext = new ctNode ;
ctptmpHead = ctptmpHead -> ctpnext ;
ctptmpHead -> ctpnext = NULL ;
ctptmpHead -> sfileWord = svalue ;
}
return ctptmpHead -> sfileWord ;
}
string cmreturnNode (string svalue) {
return NULL;
}
string cmremoveNode (string svalue) {
if (ctphead == NULL) return NULL;
ctNode *tmpHead = ctphead;
while (tmpHead->sfileWord != svalue || tmpHead->ctpnext != NULL){
tmpHead = tmpHead->ctpnext;
}
if (tmpHead == NULL){
return NULL;
}
else {
while (tmpHead != NULL){
tmpHead = tmpHead->ctpnext;
}
}
return tmpHead->sfileWord;
}
string cmlistList () {
string tempList;
ctNode *tmpHead = ctphead;
if (ctphead == NULL){
return NULL;
}
else{
while (tmpHead != NULL){
cout << tmpHead->sfileWord << " ";
tempList += tmpHead->sfileWord;
tmpHead = tmpHead -> ctpnext;
}
}
return tempList;
}
};
Why is this happening?