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

来源:互联网 发布:软件安装助理 编辑:程序博客网 时间:2024/05/17 00:50

冒泡排序:

声明一个指针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;//创建新的结点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中的数据并没初始化,所以返回nexttail = 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;//头部置NULLprintf("请输入学生成绩,成绩为负时退出输入\n");while (score >= 0 ){pnew = (Node*)malloc(sizeof(Node));//创建新的结点scanf("%d",&score);pnew->score = score;//*******头插法的实现******pnew->next = head;head = pnew;}return head;}


1 0
原创粉丝点击