用链表实现插入排序

2019-07-14 10:20发布

#include #include struct PCB { int data; struct PCB* next; }; int number;//用于输入pcb.data的值 int num; struct PCB *start,*startHead,*temp,*newNode; int main() { printf("请输入进程数:"); scanf("%d",&num); start=(PCB*)malloc(sizeof(PCB)); startHead=(PCB*)malloc(sizeof(PCB)); start=NULL; startHead=start; temp=(PCB*)malloc(sizeof(PCB)); temp=startHead; /* for(int i=0;idata=i; newNode->next=NULL; if(i==0) { start=newNode; temp=start; } else { temp->next=newNode; temp=temp->next; } } for(startHead=start;startHead!=NULL;) { printf("%d",startHead->data); startHead=startHead->next; } */ for (int i=0;idata=number; newNode->next=NULL; if(i==0) { start=newNode; startHead=start; } else if (i==1) { if(start->data<=newNode->data) { startHead->next=newNode; } else { newNode->next=startHead; start=newNode; } } else { for(startHead=start;startHead!=NULL;startHead=startHead->next) { temp=startHead; if(start->data>newNode->data) { newNode->next=startHead; start=newNode; break; } else if(startHead->data<=newNode->data &&startHead->next!=NULL&& startHead->next->data>newNode->data) { newNode->next=startHead->next; temp->next=newNode; break; } else if(startHead->next==NULL) { temp->next=newNode; break; } } } } for(startHead=start;startHead!=NULL;) { printf("%d",startHead->data); startHead=startHead->next; } while(1); }
因为我要写一个FCFS算法,用链表实现插入排序是中间的一个重要步骤,所以我用一个简单的程序实现了这个功能。如有错误请指正,谢谢 另外大家也可以参考这篇文章:http://blog.chinaunix.net/uid-20791108-id-483290.html