the specific problem is that there are no errors in the prob but it is giving segmentation fault.plz check it

#include<iostream.h>
#include<conio.h>
void main()
{
struct mylist{
mylist * nxt;
int val;
};
int a,b,c,d=1,i,j;mylist * t;
clrscr();
cout<<"Enter the no&the total no of numbers";
cin>>a>>b;
mylist * head=new mylist;
head=NULL;
mylist * newnode=new mylist;
newnode->val=a;
newnode->nxt=head;
head=newnode;
for(i=1;i<b;i++)
{
d=1;
cout<<"Enter the next number";
cin>>c;
mylist * newnode=new mylist;
newnode->val=c;
for(j=1;j<=i;j++)
{
if(newnode->val<head->val)
{


if(d==1)
{
newnode->nxt=head;
head=newnode;
}
if(d==b)
{
newnode->nxt=NULL;
t->nxt=newnode;
}
if(1<d<b)
{
newnode->nxt=head;
t->nxt=newnode;
}
}
else
{
t=head;
head=head->nxt;
d=d+1;
}
}
}
cout<<"the new order is:\n ";
while(head)
{
cout<<head->val<<" ";
head=head->nxt;
}
getch();
}

Recommended Answers

All 2 Replies

Try to show your code like this:

it's easy :cool:

#include<iostream.h>
#include<conio.h>
void main()
{
struct mylist{
  mylist * nxt;
  int val;
  };
  int a,b,c,d=1,i,j;mylist * t;
  clrscr();
  cout<<"Enter the no&the total no of numbers";
  cin>>a>>b;
  mylist * head=new mylist;
  head=NULL;
  mylist * newnode=new mylist;
  newnode->val=a;
  newnode->nxt=head;
  head=newnode;
  for(i=1;i<b;i++)
  {
     d=1;
     cout<<"Enter the next number";
     cin>>c;
     mylist * newnode=new mylist;
     newnode->val=c;
     for(j=1;j<=i;j++)
     {
       if(newnode->val<head->val)
       {

	 if(d==1)
	 {
	 newnode->nxt=head;
	 head=newnode;
       }
	 if(d==b)
	 {
	   newnode->nxt=NULL;
	  t->nxt=newnode;
	   }
	 if(1<d<b)
	 {
	   newnode->nxt=head;
	   t->nxt=newnode;
	  }
       }
       else
	{
	  t=head;
	  head=head->nxt;
	  d=d+1;
	 }
	 }
	 }
       cout<<"the new order is:\n ";
       while(head)
       {
	cout<<head->val<<" ";
	head=head->nxt;
	}
	getch();
   }

Remember : every time you use the word "new" - make sure you also use the word "delete" (or better still, create a class which handles all the dynamic memory stuff).. at the moment, your program has got memory leaks!

Also, change "void main()" into "int main()" (and, unless you're on an ancient compiler, change <iostream.h> to <iostream> )

if(1<d<b)

What do you intend this bit of code to do? What it is actually doing could be rewritten as ( (1<d) < b)
In other words - the result of 1<d will either be true or false (0 or 1)... this result is then compared with b - probably not exactly what you intended!

Never rely on operator precedence, even if it appears at first to do what you intend. use this instead:

if ( (1<d) && (d<b) )

There may be more errors, but those are all I've found so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.