有序链表的建立 发表于 2017-10-08 | 分类于 C++ 这里的change函数是通过冒泡的方式来排序的两个for循环,在链表中实现代码如下: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970#include <stdio.h>#include <stdlib.h>#include <iostream>using namespace std;struct node{ int data; struct node *next;};void creat(int n,struct node *&head){ int i; struct node *tail,*p; head=(struct node *)malloc(sizeof(struct node )); head->next=NULL; tail=head; for(i=0;i<n;i++) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; }};void show(struct node *head){ struct node *p; p=head->next; while(p) { if(!p->next) { printf("%d\n",p->data); } else { printf("%d ",p->data); } p=p->next; }}void change(struct node *&head){ struct node *p,*q; for(p=head->next;p;p=p->next) { for(q=p->next;q;q=q->next) { if(p->data>q->data) { int t; t=p->data; p->data=q->data; q->data=t; } } }}int main(){ struct node *head; int n; scanf("%d",&n); creat(n,head); change(head); show(head);}