Oct 4, 2012 at 10:25pm UTC
I'm not having any build errors, but every time I run it it says the program has stopped working. Any help would really be appreciated.
#include<iostream>
using namespace std;
struct node
{
node(int d):data(d),next(NULL){}
int data;
node* next;
};
class LList
{
public:
LList() : head(NULL){}
~LList();
void addNode(int d);
bool rmvNodeAt(int pos);
bool rmvFront();
bool rmvBack();
bool addNodeAt(int d , int pos);
void addBack(int d);
void traverse();
private:
node* head;
};
LList::~LList()
{
while(head!=NULL)
{
delete head;
head=head->next;
}
}
void LList::addNode(int d)
{
node* curr=head;
node* y=new node(d);
if(curr==head)
{
curr=curr->next;
head->next=y;
}
}
//REMOVING A NODE AT A CERTAIN POSITION. POSITIONS ARE NUMBERED STARTING AT "1" NOT "0"
bool LList::rmvNodeAt(int pos)
{
int cnt=0;
int loc=pos;
node* prev=NULL;
node* curr=head;
bool r=false;
while(curr && cnt!=loc-1)
{
cnt++;
prev=curr;
curr=curr->next;
}
if(curr!=NULL)
{
if(curr==head)
head=head->next;
else
prev->next=curr->next;
delete curr;
r=true;
}
return r;
}
bool LList::rmvFront()
{
node* curr=head;
bool r=false;
if(curr)
{
head=head->next;
delete curr;
r=true;
}
return r;
}
bool LList::rmvBack()
{
node* prev=NULL;
node* curr=head;
bool r=false;
if(curr)
{
while(curr!=NULL)
{
prev=curr;
curr=curr->next;
}
delete prev;
r=true;
}
return r;
}
bool LList::addNodeAt(int d, int pos)
{
int cnt=0;
int loc=pos;
node* prev=NULL;
node* curr=head;
node* y=new node(d);
bool r=false;
if(curr)
{
while(curr && cnt!=loc-1)
{
cnt++;
prev=curr;
curr=curr->next;
}
if(curr==head)
{
y->next=head;
head=y;
}
else
{
prev->next=y;
y->next=curr;
}
r=true;
}
return r;
}
void LList::addBack(int d)
{
node* prev=NULL;
node* curr=head;
node* y=new node(d);
while(curr)
{
prev=curr;
curr=curr->next;
}
if(curr==head)
{
y->next=head;
head=y;
}
else
{
prev->next=y;
y->next=curr;
}
}
void LList::traverse()
{
int cnt=0;
node* curr=head;
while(curr!=NULL)
{
cnt++;
curr=curr->next;
}
int* a=new int[cnt];
int i;
curr=head;
for(i=0;i<cnt;i++)
{
a[i]=curr->data;
curr=curr->next;
}
for(i=0;i<cnt-1;i++)
cout<<a[i]<<" --> ";
cout<<a[i]<<endl;
}
void main()
{
LList dList;
dList.~LList();
dList.addNode(7);
dList.addNode(8);
dList.addNode(12);
dList.addNode(20);
//OUTPUT LINKED LIST
dList.traverse();
//REMOVING A NODE AT A CERTAIN POSITION
dList.rmvNodeAt(4);
dList.traverse();
//ADDING A NODE AT A CERTAIN POSITION
dList.addNodeAt(21,2);
dList.traverse();
//REMOVE A NODE FROM THE BACK OF THE LINKED LIST
dList.rmvBack();
dList.traverse();
//REMOVE A NODE FROM THE FRONT OF THE LINKED LIST
dList.rmvFront();
dList.traverse();
//ADD A NODE TO THE BACK OF THE LINKED LIST
dList.addBack(30);
dList.traverse();
}
Oct 4, 2012 at 11:38pm UTC
Your destructor is wrong you are dereferencing a deleted pointer. Addnode has no case if head is NULL, rmvBack doesn't properly set the previous nodes next to NULL. Fix those and it should run, also why are you doing this? dList.~LList();
Oct 5, 2012 at 12:52am UTC
naraku,
thank you!!! I made those changes and it worked!! I appreciate the help. I'm still fairly new to C++, this is the second course I am taking so the whole "class" format I'm still getting the hang of.