#报错 run: line 1: 3 Segmentation fault (core dumped) ./a.out
Exited with error status 139
#代码为
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* initList(){
Node* list = (Node*)malloc(sizeof(Node));
list -> data = 0;
list -> next = NULL;
return list;
}
void tailInsert(Node* list,int data){
Node* head = list;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
list = list->next;
while(list->next){
list = list -> next;
}
list -> next = node;
head -> data++;
}
void display(Node* list){
list = list -> next;
while(list){
printf("%d ",list -> data);
list = list->next;
}
printf("\n");
}
Node* findMin(Node* p){
int value;
Node* min = p;
value = p->data;
p = p->next;
while(p){
if(p->data<value){
min = p;
value = min->data;
}
p = p->next;
}
return min;
}
void simpleSort(Node* list){
Node* q;
Node* p;
q=p=list->next;
while(p){
q = findMin(q);
int temp = q->data;
q->data = p->data;
p->data = temp;
p=p->next;
q=p;
}
}
int main()
{
Node* list = initList();
tailInsert(list,12);
tailInsert(list,32);
tailInsert(list,4);
tailInsert(list,25);
tailInsert(list,13);
printf("排序前");
display(list);
simpleSort(list);
printf("排序后");
display(list);
}