#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 888
#define MIN 111
typedef struct NODE
{
int data;
struct NODE *Next;
} Node, *List;
// 创建新节点
List create_node(int);
// 释放一个节点
void free_node(List *);
// 创建一个所有结点为0的链表
List InitList(size_t);
// 求链表元素的个数
size_t List_count(List);
// 一个个的为链表赋值
void insert_oneByOne(List, size_t);
// 一次性为链表赋值
void insert_inOnetime(List, size_t);
// 释放一个链表
void freeList(List *);
// 打印一个链表
void printf_list(List);
// 尾插一个结点
List tailInsert(List, int);
// 头插一个结点
void headInsert(List, int);
// 随机插入
// 插入选中位置
void insert(List, int, int);
// 删除选中位置
List delete_node(List, int);
// 冒泡排序
List sort(List, size_t);
// 排序(选择)
void sort_choose(List, size_t);
// 逆序
List reserve(List);
// 返回最大或最小结点
List findPosition(List, int);
int main()
{
/*************************************************************
* 初始化链表测试
*************************************************************/
size_t size;
printf("please enter the number whitch is the quantity of list\n");
scanf("%lu", &size);
/*************************************************************
* 输入测试
*************************************************************/
List list = InitList(size);
printf("please enter the nuber one by one\n");
insert_oneByOne(list, size);
printf("the original list is\n");
printf_list(list);
/*************************************************************
* 头插测试
*************************************************************/
headInsert(list, 55);
headInsert(list, 66);
printf("the list after head insert is\n");
printf_list(list);
/*************************************************************
* 尾插测试
*************************************************************/
List head = list;
list = tailInsert(list, 77);
list = tailInsert(list, 88);
list = tailInsert(list, 99);
printf("the list after tail insert is\n");
printf_list(head);
/*************************************************************
* 插入指定位置测试
*************************************************************/
insert(head, 4, 33);
printf("the list after chose-position insert is\n");
printf_list(head);
/*************************************************************
* 删除测试
*************************************************************/
head = delete_node(head, 1);
printf("the list after chose-position delete is\n");
printf_list(head);
/*************************************************************
* 找位置测试
*************************************************************/
List node = findPosition(head, MAX);
printf("the node value witch you wanna find is %d\n", node->data);
printf("==========================================\n");
/*************************************************************
* 排序测试
*************************************************************/
size_t lenth = List_count(head);
printf("the list after bubble sort is\n");
head = sort(head, lenth);
printf_list(head);
/*************************************************************
* 逆序测试
*************************************************************/
head = reserve(head);
printf("the list after reserve is\n");
printf_list(head);
}
// 创建新节点
List create_node(int val)
{
List node = NULL;
node = (List)malloc(sizeof(Node));
if (node == NULL)
{
perror("malloc");
return NULL;
}
node->Next = NULL;
node->data = val;
return node;
}
// 释放一个节点
void free_node(List *node)
{
if (node == NULL) /* node是指向结构体的指针的地址,内容是指向结构体的指针的地址 */
return;
if (*node == NULL) /* *node是指向结构体的指针,内容是结构体的地址 */
return;
free(*node); /* 断开和结构体的绑定 */
*node = NULL;
return;
}
// 创建一个所有元素为0的单链表
List InitList(size_t size)
{
List head = create_node(-1);
if (head == NULL)
{
perror("create_node");
return NULL;
}
List temp = head; /*中间变量temp*/
while (size--) /*尾插创建*/
{
List new_node = create_node(0);
temp->Next = new_node;
temp = new_node;
}
temp = head->Next;
free_node(&head); /* 为什么释放:指向结构体指针的地址? */
return temp;
}
// 求链表元素个数
size_t List_count(List list)
{
int count = 0;
while (list)
{
count++;
list = list->Next;
}
return count;
}
// 一个个为链表赋值
void insert_oneByOne(List list, size_t size)
{
if (list == NULL)
{
perror("InitList");
return;
}
int i;
for (i = 0; i < (int)size; i++)
{
scanf("%d", &list->data);
list = list->Next;
}
return;
}
// 一次性为链表元素赋值
void insert_inOnetime(List list, size_t size)
{
if (list == NULL)
{
perror("InitList");
return;
}
int i;
int content[size];
for (i = 0; i < (int)size; i++)
{
scanf("%d", content + i);
}
for (i = 0; i < (int)size; i++)
{
list->data = content[i];
list = list->Next;
}
return;
}
// 释放一个链表
void freeList(List *list)
{
while (*list)
{
List pos = (*list)->Next;
free_node(list);
*list = pos;
}
}
// 打印一个链表
void printf_list(List list)
{
while (list)
{
printf("%d ", list->data);
list = list->Next;
}
printf("\n");
printf("==========================================\n");
return;
}
// 尾插一个节点
List tailInsert(List list, int val)
{
if (list == NULL)
{
perror("tailInsert");
return NULL;
}
List tail = list->Next;
List new_node = create_node(val);
List tmp = list;
tmp->Next = new_node;
tmp = new_node;
new_node->Next = tail;
return tmp;
}
// 头插一个节点
void headInsert(List list, int val)
{
if (list == NULL)
{
perror("headInsert");
return;
}
List new_node = create_node(val);
new_node->Next = list->Next;
list->Next = new_node;
return;
}
// 随机插入
void randInsert(List, int);
// 插入新节点到选中位置
void insert(List head, int position, int val)
{
List list = head;
position -= 2;
while (position--)
list = list->Next;
List new_node = create_node(val);
new_node->Next = list->Next;
list->Next = new_node;
return;
}
// 删除选中位置
List delete_node(List head, int position)
{
List list = head;
List temp = NULL;
if (position == 1)
temp = list->Next;
else
{
position -= 1;
while (position--)
{
temp = list;
list = list->Next;
}
temp->Next = list->Next;
}
free_node(&list);
return temp;
}
// 返回最大或最小值指针
List findPosition(List list, int find_value)
{
if (list == NULL)
{
printf("please enter the list witch has value");
return NULL;
}
List position = NULL;
if (find_value == MAX)
{
position = list;
while (list)
{
if (list->data > position->data)
position = list;
list = list->Next;
}
}
else if (find_value == MIN)
{
position = list;
while (list)
{
if (list->data < position->data)
position = list;
list = list->Next;
}
}
return position;
}
// 单链表排序(冒泡)
List sort(List list, size_t lenth)
{
List pos_tmp = NULL;
List pos = NULL;
List head = create_node(0);
head->Next = list;
pos_tmp = head;
for (int i = 0; i < (int)lenth - 1; i++)
{
for (int j = 0; j < (int)lenth - i; j++)
{
if (list->Next != NULL && list->data > list->Next->data)
{
pos = list->Next;
list->Next = pos->Next;
pos->Next = list;
pos_tmp->Next = pos;
List tmp = NULL;
tmp = pos;
pos = list->Next;
pos_tmp = tmp;
}
if (list->Next != NULL && list->data < list->Next->data)
{
list = list->Next;
pos = list->Next;
List tmp = NULL;
tmp = pos_tmp->Next;
pos_tmp = tmp;
}
}
list = head->Next;
pos = list->Next;
pos_tmp = head;
}
List ret_node = delete_node(head, 1);
return ret_node;
}
// 逆序
List reserve(List list)
{
size_t lenth = List_count(list);
List pos = NULL;
List pos_n = NULL;
int per_val = (int)lenth - 1;
while (per_val)
{
if (per_val == (int)lenth - 1)
{
pos = list->Next;
list->Next = NULL;
pos_n = pos->Next;
pos->Next = list;
}
if (pos_n->Next != NULL)
list = pos_n->Next;
else
{
pos_n->Next = pos;
return pos_n;
}
pos_n->Next = pos;
if (list->Next != NULL)
pos = list->Next;
else
{
list->Next = pos_n;
return list;
}
list->Next = pos_n;
if (pos->Next != NULL)
pos_n = pos->Next;
else
{
pos->Next = list;
return pos;
}
pos->Next = list;
per_val--;
}
return NULL;
}
此为冒泡排序流程图
上述代码包含链表相应基础操作,都是本人自己所作,望大家指正,因为写的时候还是有点困难,又搜不到比较好的代码,所以自己苦逼敲了许久,现在写完了,分享给大家...