Mar 7, 2011 at 7:10pm UTC
Hi ,
My Application is crashing , i am not able to see why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class CLinkList
{
private :
struct Node
{
char name[20];
int age ;
float height;
Node* link ;
} *p;
public :
CLinkList();
virtual ~CLinkList();
public :
void add_first( const char * n , int a , float h) ;
void display();
};
and the cpp file is
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
CLinkList::CLinkList()
{
p = NULL;
}
CLinkList::~CLinkList()
{
if ( p == NULL )
return ;
Node* q;
while ( p->link)
{
q = p->link;
delete q;
p = q;
}
}
void CLinkList::add_first( const char * n , int a , float h)
{
Node* q;
q = new Node;
strcpy( p->name , n);
p->age = a;
p->height = height;
p = q;
}
void CLinkList::display()
{
Node* q;
for ( q = p ; q != NULL; q = q->link )
{
cout<<"\n" ;
cout<<"\t" <<q->name <<"\t" <<q->age<<"\t" <<q->height<<endl;
}
}
and the main file is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "LinkList.h"
int main(int argc, char * argv[])
{
CLinkList list ;
list.add_first("Bill" , 30 , 4.5);
list.add_first("Jack" , 35 , 5.5);
list.add_first("Peter" , 40 , 9.5);
list.add_first("Milly" , 90 , 14.5);
list.display();
return 0;
}
please let me know why ..thanks in advance.
Last edited on Mar 7, 2011 at 7:14pm UTC
Mar 7, 2011 at 7:38pm UTC
Your add_first is wrong. First you create a new node (note that since it does not have a constructor, link is not null) then you set the attributes of the old node (which is null, by the way) and then assign the head of the list to the uninitialized node q.
What you should be doing is create a new node, set its attributes, set its link to point to the old head, and make head point to that node.
Mar 8, 2011 at 6:35am UTC
Thanks ..it solved my problem .