Hi, I have the following code which compiles absolutely fine..
using namespace std;
#include<iostream>
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
void add(struct node **,int);
void display(struct node*);
int count(struct node *);
int main(void)
{
struct node *p;
p=NULL;
add(&p,5);
add(&p,1);
add(&p,6);
add(&p,4);
add(&p,7);
display(p);
cout<<"\nNo of elements in linked list:"<<count(p);
getch();
return 0;
}
void add(struct node **q,int num)
{
struct node *r,*temp;
r=*q;
if(num<=r->data||r==NULL)
{
struct node *s;
s= new struct node;
s->link=*q;
s->data=num;
*q=s;
}
else
{
temp=new struct node;
temp->data=num;
while(r!=NULL)
{
if(r->data<=num&&(r->link->data>num||r->link==NULL))
{
temp->link=r->link;
r->link=temp;
return;
}
r=r->link;
}
}
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
cout<<temp->data;
cout<<"\n";
temp=temp->link;
}
}
int count(struct node *q)
{
struct node *e;
int n=0;
while(q!=NULL)
{
n++;
q=q->link;
}
return n;
}
However when I run the code, I get an error (LinkedList2.exe(name of my file) has encountered a problem and needs to close.. and there is no output..)
Please suggest some solution to this..I am using dev C++ as compiler