首页 > 编程知识 正文

c语言链表详解,用C语言在链表下对字符串进行排序

时间:2023-05-06 01:33:19 阅读:51308 作者:1634

在c语言中使用链表时,链表中的数据可能会进行排序。 介绍使用链表时可以使用的排序方法、气泡排序、选择排序。

此链表的排序只对链表中的数据进行排序,要对整个结构进行排序,即利用数据顺序调整节点中的信息,需要更换节点,但与此方法不同的是,请期待读者的鲤鱼卡发行。

要测试排序代码,请首先参阅下面完整的测试代码。

编程环境: Visual C 6.0。

气泡排序node*bubblesort(node*head ) { NODE *pfirst=NULL,*psecond=NULL,*pend=NULL; pfirst=Head; psecond=Head; int temp; while (首选!=pend外循环{ //pfirst!=pend是有趣的while(pfirst-next!=pend(/内循环(if ) pfirst-datepfirst-next-date ) ) { temp=pfirst-date; pfirst-date=pfirst-next-date; pfirst-next-date=temp; } pfirst=pfirst-next; (} pend=pfirst; //减少最后排列的循环pfirst=head; }返回头; }选择排序/*链表选择排序*/node*selectsort(node*head ) {NODE *pfirst=NULL,*psecond=NULL,*pend=NULL; pfirst=head; psecond=head; int temp; while (首选!=pend外循环{while(pfirst-next!=pend(/内循环(if ) psecond-datepfirst-next-date ) ) { temp=psecond-date; p第二天=pfirst-next-date; pfirst-next-date=temp; } pfirst=pfirst-next; }psecond=psecond-next; pfirst=psecond; }返回头; }测试代码/* *链表排序writenbyyu */# include stdio.h # include stdlib.h/*结果输出显示*/voidendscan(({printf ) (n单击闪存(stdin; getchar (; }/*结构体* /结构节点{ int date; 结构节点*下一步; (; 类型结构节点; 将结构节点定义为节点输入计数=0; /*创建链表,然后单击*/NODE* creat () { NODE *pHead=NULL,*pNew,*pEnd; printf ('输入数据,在输入0时停止(n ); pnew=(node* ) malloc ) sizeof (node ); 扫描(“% d”,新建日期); wile (新日期!=0) { count; if(count==1) /对于头部节点({ pNew-next=NULL; pEnd=pNew; pHead=pNew; (}如果不是}else //第一个节点) { pNew-next=NULL; pEnd-next=pNew; pEnd=pNew; (pnew=(node* ) malloc ) sizeof (node ); 扫描(“% d”,新建日期); (free ) pnew; 返回头; }/*链表*/voidprint(node*head ) { NODE* p=Head; while(p!=null(printf ) ' %d ',p-date ); p=p-next; }/*链表的气泡排序*/node*bubblesort(node*head ) { NODE *pfirst=NULL,*psecond=NULL,*pend=NULL; pfirst=Head; psecond=Head; int temp; while (首选!=pend外循环{ //pfirst!=pend是有趣的while(pfirst-next!=pend(/内循环(if ) pfirst-datepfirst-next-date ) ) { temp=pfirst-date; pfirst-date=pfirst-next-date; pfirst-next-date=temp; } pfirst=pfirst-next; (} pend=pfirst; //减少最后排列的循环pfirst=head; }返回头; }/*链表的选择顺序*/node*selectsort(node*head ) {NODE *pfirst=NULL,*psecond=NULL,*pend=NULL; pfirst=head; psecond=head; int temp; while (首选!=pend外循环{while(pfirst-next!=pend(/内循环(if ) psecond-datepfirst-next-date ) ) { temp=psecond-date; p第二天=pfirst-next-date; pfirst-next-date=temp; } pfirst=pfirst-next; }psecond=psecond-next; pfirst=psecond; }返回头; } int main () { NODE* pHead=NULL; pHead=creat (; printf (排序前(n ); 打印(phead; //phead=bubblesort(phead ); //气泡排序phead=selectsort(phead ); //排序选择printf (n排序后(n ); 打印(phead; endscan (; 返回0; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。