程序代码如下所示,问题在注释中
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
#include<time.h>
typedef struct Node
{
int num;
struct Node* next;
}node;
int main()
{
node *head,*item;
srand(time(0));
head=(node*) malloc(sizeof(node));
head->num=0;
head->next=NULL;
node *temp=(node*) malloc(sizeof(node));
temp=head;
for(int i=0;i<2;i++)
{
item=(node*) malloc(sizeof(node));
item->num=rand()%100;
item->next=NULL;
temp->next=item;
temp=item;
}
free(item);
free(temp);
// node* item;
// item=head->next;
// while(item!=NULL)
// {
// printf("%d,",item->num);
// item=item->next;
// }
// printf("\n");
node* min=(node*) malloc(sizeof(node));
// node* temp=(node*) malloc(sizeof(node));
min=head->next;
item=head->next;
while(item->next)
{
free(temp);
for(temp=min;(temp->next)!=NULL;) //调试过程中运行到这一步temp变的很奇怪是什么原因?调试截图在下面
{
if(temp->next->num<min->num)
{
min=temp;
}
temp=temp->next;
}
temp=min->next;
min->next->next=temp->next;
temp->next=head->next;
head->next=temp;
min=head->next;
item=item->next;
}
item=head->next;
while(item!=NULL)
{
printf("%d,",item->num);
item=item->next;
}
}