Jun 21, 2012 at 5:31pm UTC
Plz check these codes ! there's no problem when i compile'em but it doesnt run :
#include<iostream.h>
class linklist
{
private:
struct node
{
int data;
node *link;
}*p;
public:
linklist();
void ins_at_end (int index);
void concat (linklist &l);
void show();
};
linklist::linklist()
{
p=NULL;
}
void linklist::ins_at_end(int index)
{
node *temp;
temp=p;
if(temp==NULL)
{
temp=new node;
p=temp;
}
else
{
while (temp->link!=NULL)
temp=temp->link;
temp->link=new node;
temp=temp->link;
}
temp->data=index;
temp->link=NULL;
}
void linklist::concat(linklist &l)
{
int i=0,j=0;
node *r1=NULL,*r2=NULL;
node *temp=NULL;
if(p==NULL)
p=l.p;
else
{
if(l.p!=NULL)
{
temp=p;
while(temp->link!=NULL)
temp=temp->link;
temp->link=l.p;
}
}
r1=p;
r2=l.p;
int n=0;
while(r1!=NULL)
{
r1=r1->link;
n++;
}
r1=p;
for(i=0;i<n/2;i++)
{
for(j=0;j<1;j++)
{
r1->link=r2;
if(r1->link!=l.p)
{r1++;}
else{
r1=NULL;
}
}
r2->link=r1;
if(r2->link!=NULL)
{r2++;}
else
{
r2=NULL;
}
}
}
void linklist::show()
{
cout<<endl;
node *temp=p;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->link;
}
}
int main()
{
linklist L;
L.ins_at_end(1);
L.ins_at_end(3);
L.ins_at_end(5);
L.ins_at_end(7);
linklist M;
M.ins_at_end(2);
M.ins_at_end(4);
M.ins_at_end(6);
M.ins_at_end(8);
L.concat(M);
L.show();
return 0;
cout<<endl;
}
Jun 21, 2012 at 6:34pm UTC
Please use code tags next time. Let's see...
1) You broke a rule: for every new there must be a delete .
2) A return exits the function. In your main() , cout << endl;
will never be reached.
Error is Segmentation Fault . This probably means somewhere you use a NULL pointer.
Edit: the problem seems to be in concat()
.
Please stand by, while Dr. Catfish II fixes your coding errors.
Last edited on Jun 21, 2012 at 6:37pm UTC
Jun 21, 2012 at 7:05pm UTC
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include<iostream>
using namespace std;
class linklist
{
private :
struct node
{
int data;
node *link;
~node() {
delete link;
link = NULL;
}
}*p;
public :
linklist();
void ins_at_end (int index);
void concat (linklist &l);
void show();
};
linklist::linklist()
{
p=NULL;
}
void linklist::ins_at_end(int index)
{
node *temp;
temp=p;
if (temp==NULL)
{
temp=new node;
p=temp;
}
else
{
while (temp->link!=NULL)
temp=temp->link;
temp->link=new node;
temp=temp->link;
}
temp->data=index;
temp->link=NULL;
}
void linklist::concat(linklist &l)
{
node *temp;
if (p==NULL)
p=l.p;
else
{
temp=p;
while (temp->link!=NULL)
temp=temp->link;
temp->link=l.p;
l.p=NULL;
}
}
void linklist::show()
{
cout<<endl;
node *temp=p;
while (temp!=NULL)
{
cout<<temp->data<<" " ;
temp=temp->link;
}
}
int main()
{
linklist L;
L.ins_at_end(1);
L.ins_at_end(3);
L.ins_at_end(5);
L.ins_at_end(7);
linklist M;
M.ins_at_end(2);
M.ins_at_end(4);
M.ins_at_end(6);
M.ins_at_end(8);
L.concat(M);
L.show();
cout<<endl;
return 0;
}
Edit: you're welcome.
Last edited on Jun 21, 2012 at 7:08pm UTC