一步裙 打版图:c++链表实现数据输入与排序。急用

来源:百度文库 编辑:中科新闻网 时间:2024/04/30 02:53:00
#include"iostream"
using namespace std;
struct Node
{ int num;
Node *next;
};
int main()
{
// input the numbers;
int n,count=1;
Node *first,*last;
first=last=new Node;
cout<<"please input a number(using -1 as the end):";
cin>>n;
if (n==-1)
{ cout<<"no number iuput!"<<endl;
return 0;
}
else first->num=n;
while(n!=-1)
{
cout<<"please input a number(using -1 as the end):";
cin>>n;
if(n==-1)
{cout<<"ÊäÈëÍê±Ï¡£";
break;
}
else
{Node *p=new Node;
p->num=n;
last->next=p;
last=p;
count++;}
}
last->next=0;

//sort the numbers;
Node *to_be_sorted, *have_been_sorted, *p, *temp;
p=have_been_sorted=first;
to_be_sorted=first->next;

while (to_be_sorted != last->next)
{
temp=to_be_sorted->next;
if(to_be_sorted->num<=p->num)
{
to_be_sorted->next=first;
first=to_be_sorted;
}
else
{
Node *q=p->next;
while ((to_be_sorted->num > q->num) && (p->next!=have_been_sorted))
{ p=p->next;}
if ((p->next==have_been_sorted)&&(to_be_sorted->num > q->num) )
{ have_been_sorted->next=to_be_sorted;
have_been_sorted=to_be_sorted;
}
else{
to_be_sorted=p->next;
p->next=to_be_sorted;
}
}
to_be_sorted=temp;
}

//output the new number sequence;
for(int m=1;m<=count;m++)
{
Node *p=first;
cout<<p->num<<' ';
p=p->next;
}
return 0;
}
程序中出现错误,请指正。
急用,谢谢

main函数前面是void吧,还有你的count有的地方写成了cout