#include<iostream>
#include<malloc.h>
using namespace std;
int a=0,b=0;
struct node
{
int data;
struct node *next;
};
struct node *creat(int m)
{
struct node *head,*tail,*p;
int i;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=1;i<=m;i++)
{
p=(struct node *)malloc(sizeof (struct node ));
cin>>p->data;
p->next=NULL;
tail->next=p;
tail = p;
}
return head;
};
void sort(struct node *head)
{
struct node *p,*q;
int t;
for(p=head->next;p!=NULL;p=p->next)//将选择排序用指针表示
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data>q->data)
{
t=p->data;
p->data=q->data;
q->data=t;
}
}
}
}
void display(struct node *head1)
{
struct node *q;
q=head1->next;
while(q!=NULL)
{
cout << q->data;
if(q->next!=NULL)
cout<<" ";
q=q->next;
}
cout<<endl;
}
int main()
{
int n,h,m;
struct node *head1,*head2;
cin>>m;
head1=creat(m);
sort(head1);
display(head1);
return 0;
}
2121数据结构实验之链表六:有序链表的建立
最新推荐文章于 2018-10-11 21:05:30 发布