博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单向链表的冒泡排序
阅读量:4090 次
发布时间:2019-05-25

本文共 1044 字,大约阅读时间需要 3 分钟。

#include <stdio.h>
#include <malloc.h>
#include <iostream>
struct node {
int val;
struct node *next;
};
static void list_sort(struct node *head);
struct node *list_create(int arr[], int size)
{
struct node *head = NULL;
int i;
for (i = size - 1; i >= 0; --i) {
struct node *p = (struct node *)malloc(sizeof(struct node));
p->val = arr[i];
p->next = head;
head = p;
}
return head;
}
void list_sort(struct node *head)
{
node* head2 = head;
node* head1 = head;//此指针用于计数
int size_ = 1;
while (head1->next != NULL)
{
head1 = head1->next;
++size_;                     //输出的为节点个数
}//while
for (int i = size_; i >= 2; --i)
{
for (int j = 1; j<i; ++j)
{
if (head->val > head->next->val)
{
int temple;
temple = head->val;
head->val = head->next->val;
head->next->val = temple;
}//if
head = head->next;//指向下一个节点
}//for
head = head2;//指针重新指向头
}//for
}
int main(void)
{
int arr[] = { 4,3,45,2,3,4,3,2,4,3,3,3,45,3,1};
struct node* head = list_create(arr, 15);
list_sort(head);
for (int i = 0; i < 15; ++i)
{
std::cout << head->val << std::endl;
head = head->next;
}
return 0;
}

转载地址:http://dfdii.baihongyu.com/

你可能感兴趣的文章
Android 源码编译make的错误处理
查看>>
启用SELinux时遇到的问题
查看>>
No devices detected. Fatal server error: no screens found
查看>>
db db2 base / instance database tablespace container
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
Android 解决TextView设置文本和富文本SpannableString自动换行留空白问题
查看>>
Android自定义View实现商品评价星星评分控件
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
postgresql减少wal日志生成量的方法
查看>>
swift中单例的创建及销毁
查看>>
获取App Store中App的ipa包
查看>>
利用runtime给类别添加属性
查看>>
本地推送
查看>>
UIImage存为本地文件与UIImage转换为NSData
查看>>
[转]打印质数的各种算法
查看>>
[转]javascript with延伸的作用域是只读的吗?
查看>>
php的autoload与global
查看>>
IE不支持option的display:none属性
查看>>
[分享]mysql内置用于字符串型ip地址和整数型ip地址转换函数
查看>>
Https加密及攻防
查看>>