创建单链表,然后进行递增排序

来源:互联网 发布:开发一套软件多少钱 编辑:程序博客网 时间:2024/06/06 04:51

创建单链表,然后进行递增排序

实现代码:
#include <stdio.h>#include <stdlib.h>typedef struct LNode{int data;struct LNode *next;}LNode;void CreateLink(LNode *&h,int a[],int n){int i;LNode *s,*t;h=(LNode *)malloc(sizeof(LNode));h->next=NULL;t=h;for(i=0;i<n;++i){s=(LNode *)malloc(sizeof(LNode));s->data=a[i];t->next=s;t=t->next;}t->next=NULL;}void Sort(LNode *h){LNode *p,*p1,*q,*pre;if(h->next!=NULL){p=h->next->next;h->next->next=NULL;while(p!=NULL){pre=h;q=pre->next;while(q!=NULL&&q->data<p->data){pre=q;q=q->next;}p1=p->next;p->next=pre->next;pre->next=p;p=p1; }}}int main(int argc, char *argv[]){struct LNode *h;int i,a[7]={49,38,65,97,76,13,27};CreateLink(h,a,7);Sort(h); while(h->next!=NULL){printf("%d ",h->next->data);h=h->next;}return 0;}
输出结果:
13 27 38 49 65 76 97 请按任意键继续. . .



原创粉丝点击