程式问题
我这个程式现在要按照从小到大的顺序排应该怎么弄呢? 现在它好像不能按顺序排struct nodetype
{
int adata;
struct nodetype *ptr;
};
int main()
{
struct nodetype *start, *lastentry, *entry, *newnode, *first, *second;
int info, akey, aflag;
start=new nodetype;
start->ptr=NULL;
lastentry=start;
cout<<"input an integer value, '999' to exit";
cout<<endl;
cin>>info;
cout<<endl;
while(info != 999)
{
newnode=new nodetype;
newnode->adata = info;
newnode->ptr = NULL;
lastentry->ptr = newnode;
lastentry= newnode;
cout<<"input an integer value, '999' to exit";
cout<<endl;
cin>>info;
cout<<endl;
}
entry=start->ptr;
cout<<"you just entered:\n";
while (entry!=NULL)
{
cout<<entry->adata<<endl;
entry=entry->ptr;
}
cout<<endl;
do // 这里就是我做排列顺序的地方, 帮我从新便一下, 我不知哪里不对
{
aflag=0;
first=start->ptr;
second=first->ptr;
cout<<"after sorting:\n";
if(first->adata>second->adata)
{
aflag++;
first=first->ptr;
second=second->ptr;
}
else if (first->adata<=second->adata)
{
first=first->ptr;
second=second->ptr;
}
}while((aflag==1)&&(second->ptr!=NULL));
entry=start->ptr;
while (entry!=NULL)
{
cout<<entry->adata<<endl;
entry=entry->ptr;
}
cout<<endl;
cout<<endl<<"Enter a number you want to search."<<endl;
cin>>akey;
entry=start->ptr;
while ((akey != entry->adata) && (entry->ptr != NULL))
{
entry=entry->ptr;
}
if (akey==entry->adata)
{
cout<<"found"<<endl;
}
else
{
cout<<"not found"<<endl;
}
system("PAUSE");
return 0;
}