链表的冒泡排序

来源:互联网 发布:苍神录java下载 编辑:程序博客网 时间:2024/05/22 12:03
#include<iostream>using namespace std;typedef struct head{int a;struct head *next;}NLode;void intial(NLode **head){*head=(NLode *)malloc(sizeof(NLode));(*head)->next=NULL;}void insert(NLode *head,int a){NLode *p,*q;p=head;q=(NLode*)malloc (sizeof(NLode));while(p->next!=NULL){p=p->next;}q->a=a;q->next=p->next;p->next=q;}void Nodesort(NLode *head1){NLode *p=head1->next;int n=0,i,j,temp;while(p!=NULL){p=p->next;n++;}for(i=0;i<n-1;i++){p=head1->next;for(j=0;j<n-1-i;j++){if(p->a>(p->next)->a){temp=p->a;p->a=p->next->a;p->next->a=temp;}p=p->next;}}}void printSL(NLode *head){NLode *p;p=head->next;while(p!=NULL){cout<<p->a<<"   ";p=p->next;}}void main(){NLode *head1,*head2,*head3,*head4; intial(&head1);int a,b,num=0,i;cin>>a;for(i=0;i<a;i++){cin>>num;insert(head1,num);} cout<<"A:"<<endl;printSL(head1);cout<<endl;cout<<"B:"<<endl;Nodesort(head1);printSL(head1);system("pause");}

0 0
原创粉丝点击