您的位置:首页 > 编程语言 > C语言/C++

单链表冒泡、选择排序的c语言实现

2017-03-10 16:28 375 查看
冒泡排序:

声明一个指针p,用于向后遍历,一个尾指针tial,用于向前遍历。一个记录指针next,用于记录p的相邻结点,交换数据。符合交合冒泡排序的交换条件时,只对链表进行指针指向内容的交换,不改变指针原有结构

实现代码:

/*
==========
冒泡排序
==========
*/
Node* BubbleSort(Node* List)
{
Node *p;//向后遍历的指针
Node *tail;//向前遍历的尾指针
Node *next;//记录p结点的下一个指针
int temp;

if (List->next->next == NULL)
{
return List;//链表中只有一个元素
}
//找到尾指针
for (p = List->next; p->next != NULL; p = p->next)
;
tail = p->next;

while (tail != List->next)//需要冒泡次数
{
for (p = List; p->next!=tail; p=p->next)//向前遍历
{
next = p->next;//记录下一位
if (p->score > next->score)//相邻两位比较
{
temp = p->score;
p->score = next->score;
next->score = temp;
}
}

tail = p;//因为p->next == tail,把tail向前移动一位
}
return List;

选择排序:
声明两个遍历的指针p、q,一个标记指针记录最小值pMin。具体实现和顺序表下实现选择排序的方法几乎一样

实现代码:

/*
==========
选择排序
==========
*/
Node* SelectSort(Node* List)
{
Node* p,*q;//遍历的两个指针
Node* pMin;//记录最小的数据
int temp=0;

if (List->next == NULL)
{
return List;
}

for (p=List; p->next != NULL; p=p->next)//遍历的次数
{
pMin = p;//记录最小的数值
for (q=p->next; q; q=q->next)//从排好的小值部分开始向后遍历
{
if (q->score < pMin->score)//发现比最小值更小的
{
pMin = q;//记录最小值
}
}
if (pMin != p)//不相等说明已经发现最小值
{
temp = pMin->score;
pMin->score = p->score;
p->score = temp;
}
}

return List;
}完整的测试代码:
// linklisttest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"

typedef struct stu//创建结构体
{
int score;
struct stu *next;
}Node;

Node* create_f();//头插法创建链表
Node* create_e();//尾插法创建链表
Node* delete_node(Node* head, int score);//删除结点
Node* SelectSort(Node* List);//选择排序
Node* BubbleSort(Node* List);//冒泡排序
void display(Node *link);//链表的输出

/*主函数*/
int main()
{

Node *list = create_e();
//display(BubbleSort(list));
display(SelectSort(list));
return 0;
}
/*
==========
选择排序
==========
*/
Node* SelectSort(Node* List)
{
Node* p,*q;//遍历的两个指针
Node* pMin;//记录最小的数据
int temp=0;

if (List->next == NULL)
{
return List;
}

for (p=List; p->next != NULL; p=p->next)//遍历的次数
{
pMin = p;//记录最小的数值
for (q=p->next; q; q=q->next)//从排好的小值部分开始向后遍历
{
if (q->score < pMin->score)//发现比最小值更小的
{
pMin = q;//记录最小值
}
}
if (pMin != p)//不相等说明已经发现最小值
{
temp = pMin->score;
pMin->score = p->score;
p->score = temp;
}
}

return List;
}

/*
==========
冒泡排序
==========
*/
Node* BubbleSort(Node* List)
{
Node *p;//向后遍历的指针
Node *tail;//向前遍历的尾指针
Node *next;//记录p结点的下一个指针
int temp;

if (List->next->next == NULL)
{
return List;//链表中只有一个元素
}
//找到尾指针
for (p = List->next; p->next != NULL; p = p->next)
;
tail = p->next;

while (tail != List->next)//需要冒泡次数
{
for (p = List; p->next!=tail; p=p->next)//向前遍历
{
next = p->next;//记录下一位
if (p->score > next->score)//相邻两位比较
{
temp = p->score;
p->score = next->score;
next->score = temp;
}
}

tail = p;//因为p->next == tail,把tail向前移动一位
}
return List;
}
/*
====================
功能:尾插法创建链表
返回:头部结点指针
====================
*/
Node* create_e()
{
Node* head;//头部
Node* pnew;
a6e2
//创建新的结点
Node* tail;//尾部
int score = 0;

printf("请输入学生成绩,成绩为负数时退出输入。\n");
head = (Node*)malloc(sizeof(Node));//先创建一个空链表

tail = head;//tail指向头部
scanf("%d",&score);
while (score >= 0)
{
pnew = (Node*)malloc(sizeof(Node));//创建新节点
pnew->score = score;
//***向后延伸生成链表****
tail->next = pnew;//第一个tail就是head,说明head中的数据并没初始化,所以返回next
tail = pnew;
scanf("%d",&score);
}
tail->next = NULL;//尾部结束时指向空
return head->next;//head中的数据并没初始化,所以返回next
}

/*
=============
删除结点
=============
*/
Node* delete_node(Node* head, int score)
{
Node *elem = head;//用于索引的指针
Node *temp = NULL;//用于记录删除结点的上一个结点

if (elem->score == score)//删除的正好是第一个结点
{
head = elem->next;
free(elem);
}
else
{
while (elem != NULL)//没有到尽头
{
temp = elem;//记录保存好
elem = elem->next;//往后索引

if (elem == NULL)//遍历完也没找到
{
printf("没有找到要删除的结点\n");
}
else if(elem->score == score)//找到
{
temp->next = elem->next;//删除结点的前一个结点指向删除结点的后一个结点
free(elem);//释放删除结点的内存
break;
}
else
{
printf("没有找到要删除的结点\n");
}

}
}

return head;

}

/*
=================
功能:输出链表
=================
*/
void display(Node *link)
{
Node *p = link;
while (p != NULL)
{
printf("%d\t",p->score);
p = p->next;
}
puts("\n");
}

/*
====================
功能:头插法创建链表
返回:链表头指针
====================
*/
Node* create_f()
{
Node *head;//链表头
Node *pnew;//用创建新的结点
int score = 0;

head = NULL;//头部置NULL
printf("请输入学生成绩,成绩为负时退出输入\n");
while (score >= 0 )
{
pnew = (Node*)malloc(sizeof(Node));//创建新的结点
scanf("%d",&score);
pnew->score = score;
//*******头插法的实现******
pnew->next = head;
head = pnew;

}
return head;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐