没有合适的资源?快使用搜索试试~ 我知道了~
设head为单链表的头指针,将单链表中元素按递增顺序就地排列
5星 · 超过95%的资源 需积分: 47 34 下载量 91 浏览量
2009-05-21
19:55:05
上传
评论 4
收藏 1KB TXT 举报
温馨提示
试读
2页
设head为单链表的头指针,并设单链表带有头结点,编写算法将单链表中的数组元素按照其值递增有序的顺序进行就地排列
资源推荐
资源详情
资源评论
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct _Node
{ int data; struct _Node *next;};
typedef struct _Node Node;// 交换两个结点的数据
void SwapNodeData(Node *p1, Node *p2)
{ int temp = p1->data;
p1->data = p2->data;
p2->data= temp;}// 冒泡排序对链表进行排序
void BubbleSort(Node *head)
{ Node *pTemp;
int maxIdx, idx; // 计算链表长度
maxIdx = 0;
for (pTemp = head; pTemp != NULL; pTemp = pTemp->next)
++maxIdx;
idx = 0;
while (idx < maxIdx-1)
{ for (pTemp = head;
idx < maxIdx-1;
pTemp = pTemp->next, ++idx)
{ if (pTemp->data > pTemp->next->data)
SwapNodeData(pTemp, pTemp->next); }
idx = 0;
--maxIdx;
}
}int main(void)
{ Node *head = NULL, *temp = NULL, *p = NULL;
int i;
srand((unsigned int)time(NULL)); // 产生随机数链表
#include <stdlib.h>
#include <time.h>
struct _Node
{ int data; struct _Node *next;};
typedef struct _Node Node;// 交换两个结点的数据
void SwapNodeData(Node *p1, Node *p2)
{ int temp = p1->data;
p1->data = p2->data;
p2->data= temp;}// 冒泡排序对链表进行排序
void BubbleSort(Node *head)
{ Node *pTemp;
int maxIdx, idx; // 计算链表长度
maxIdx = 0;
for (pTemp = head; pTemp != NULL; pTemp = pTemp->next)
++maxIdx;
idx = 0;
while (idx < maxIdx-1)
{ for (pTemp = head;
idx < maxIdx-1;
pTemp = pTemp->next, ++idx)
{ if (pTemp->data > pTemp->next->data)
SwapNodeData(pTemp, pTemp->next); }
idx = 0;
--maxIdx;
}
}int main(void)
{ Node *head = NULL, *temp = NULL, *p = NULL;
int i;
srand((unsigned int)time(NULL)); // 产生随机数链表
资源评论
- 呵_呵呵_呵呵呵2014-10-19很好,对我有用
zangliyan
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功