#include
using namespace std;
class node{
public:
int data;
node next;
node(int d)
{
data=d;
next=NULL;
}
};
void insertathead(node&head,int data)
{
node n=new node(data);
n->next=head;
head=n;
}
int len(nodehead)
{
int l=0;
while(head!=NULL)
{
head=head->next;
l++;
}
return l;
}
void insertattail(node*&head,int data)
{
if(head==NULL)
{
head=new node(data);
return;
}
nodetail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next=new node(data);
return;
}
void insertatmiddle(node&head,int data,int p)
{
if((head==NULL)||(p==0))
insertathead(head,data);
else if(p>len(head))
insertattail(head,data);
else
{
int jump=1;
nodetemp=head;
while(jump<=p-1)
{
temp=temp->next;jump++;
}
node n=new node(data);
n->next=temp->next;
temp->next=n;
}
}
void print(node head)
{
while(head!=NULL)
{
cout<data<<" ";
head=head->next;
}
}
node midoflist(nodehead)
{
if(head==NULL||head->next==NULL)
return head;
int l=len(head);
nodeslow=head;
nodefast=head;
while(fast->next!=NULL&&fast!=NULL)
{
fast=fast->next->next;
slow=slow->next;
}
return slow;
}
node merge(nodea,nodeb)
{
if(a==NULL)
return b;
else if(b==NULL)
return a;
node *c;
if(a->data>b->data)
{
c=b;
c->next=merge(a,b->next);
}
else
{
c=a;
c->next=merge(a->next,b);
}
return c;
}
node mergesort(nodehead)
{
if(head==NULL||head->next==NULL)
return head;
node *mid=midoflist(head);
node *a=head;
node *b=mid->next;
mid->next=NULL;
a=mergesort(a);
b=mergesort(b);
node *c=merge(a,b);
return c;
}
int main()
{
node *head=NULL;
insertathead(head,1);
insertathead(head,2);
insertathead(head,3);
insertattail(head,6);
insertatmiddle(head,7,3);
print(head);
cout<<endl;
node *m=mergesort(head);
print(m);
return 0;
}
this code is giving a run error help me out