Hi all,
I have a double link list program that compiles ok, and when I run it, I get a message box saying that windows needs to stop the program.
Here is the code:
#include <iostream>
#include <conio.h>
using namespace std;
class linklist
{
private:
struct dnode
{
dnode *prev;
int data;
dnode *next;
}*p;
public:
linklist();
~linklist();
void d_addatbeg(int num);
void d_append(int num);
void d_addafter(int loc,int num);
void d_delete(int num);
};
linklist::linklist()
{
p = NULL;
}
linklist::~linklist()
{
dnode *q;
while (q->next != NULL)
{
q = p->next;
delete p;
p = q;
}
}
void linklist::d_addatbeg(int num)
{
dnode *q;
q = new dnode;
q->prev = NULL;
q->data = num;
q->next = p;
p->prev = q;
p = q;
}
void linklist::d_append(int num)
{
dnode *q,*r;
q = p;
if (q == NULL)
{
q = new dnode;
q->prev = NULL;
q->data = num;
q->next = NULL;
p = q;
}
else
{
while (q->next != NULL)
q = q->next;
r = new dnode;
r->data = num;
r->next = NULL;
r->prev = q;
q->next = r;
}
}
void linklist::d_addafter(int loc,int num)
{
dnode *q;
q = p;
for (int i = 0;i < loc;i++)
{
q = q->next;
if (q == NULL)
{
cout << "There are less than " << loc << " elements";
return;
}
}
q = q->prev;
dnode *temp = new dnode;
temp->data = num;
temp->prev = q;
temp->next = q->next;
temp->next->prev = temp;
q->next = temp;
}
void linklist::d_delete(int num)
{
dnode *q = p;
while (q != NULL)
{
if (q->data == num)
if (q == p)
{
p = p->next;
p->prev = NULL;
}
else
{
if (q->next = NULL)
q->prev->next = NULL;
else
{
q->prev->next = q->next;
q->next->prev = q->prev;
}
delete q;
}
return;
}
q = q->next;
cout << "\n" << num << " not found";
}
int main()
{
linklist P;
cout << "Adding at beginning: " << endl;
P.d_addatbeg(33);
P.d_addatbeg(55);
getch();
return 0;
}//end main
I am not sure where I have gone wrong and would appreciate it if someone could give some advice?
Thanks in advance