单链表学习完,完成一下双链表的练习,本人小白,还望大佬指正!
此图为选择排序第一次遍历完执行操作
此图为倒序第一次遍历完操作
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct NODE *prior;
struct NODE *next;
} Node, *List;
// 新建一个结点
List create_node(int);
// 释放一个结点
void free_node(List *);
// 创建一个双链表
List create_list(size_t);
// 计算双链表长度
size_t get_lenth(List);
// 打印双链表
void print_list(List);
// 删除一个双链表
void delete_list(List *);
// 选择位置插入
void choose_insert(List, int, int);
// 选择位置删除
void choose_delete(List, int);
// 双链表排序
void sort(List);
// 双链表倒序
void reverse(List);
int main()
{
List list = NULL;
printf("please enter the quality number of your list:\n");
size_t qua = 0;
scanf("%lu", &qua);
/********************************************
* 创建链表测试
* *****************************************/
list = create_list(qua);
printf("========================================\n");
printf("the original list is:\n");
print_list(list);
/*******************************************
* 删除链表测试
* *****************************************/
// printf("the list after delete is:\n");
// delete_list(&list);
// print_list(list);
/*******************************************
* 选择位置插入测试
* *****************************************/
printf("the list after choose-insert is:\n");
choose_insert(list, 2, 99);
print_list(list);
/*******************************************
* 选择位置删除测试
* *****************************************/
printf("the list after choose-delete is:\n");
choose_delete(list, 1);
print_list(list);
/*******************************************
* 排序测试
* *****************************************/
printf("the list after sort is:\n");
sort(list);
print_list(list);
/*******************************************
* 倒序测试
* *****************************************/
printf("the list after reserve is:\n");
reverse(list);
print_list(list);
return 0;
}
/*******************************************
* 创建一个节点
* *****************************************/
List create_node(int val)
{
List head = (List)malloc(sizeof(Node));
if (head == NULL)
{
perror("malloc");
return NULL;
}
head->data = val;
head->prior = NULL;
head->next = NULL;
return head;
}
/*******************************************
* 释放一个节点
* *****************************************/
void free_node(List *node)
{
if (node == NULL)
{
printf("empty node\n");
return;
}
free(*node);
*node = NULL;
node = NULL;
}
/*******************************************
* 创建一个双链表
* *****************************************/
List create_list(size_t size)
{
List head = create_node(0);
List list = head;
int val;
printf("please enter the nember:\n");
while (size--)
{
scanf("%d", &val);
List node = create_node(val);
list->next = node;
node->prior = list;
list = list->next;
}
list->next = NULL;
return head;
}
/*******************************************
* 计算链表长度
* *****************************************/
size_t get_lenth(List list)
{
if (list == NULL)
{
printf("list is NULL!\n");
return 0;
}
size_t count = 0;
while (list->next != NULL)
{
count++;
list = list->next;
}
return count;
}
/*******************************************
* 打印双链表
* *****************************************/
void print_list(List list)
{
if (list == NULL)
{
printf("list is NULL!\n");
printf("========================================\n");
return;
}
int lenth = (int)get_lenth(list);
list = list->next;
while (lenth--)
{
printf("%d ", list->data);
list = list->next;
}
printf("\n");
printf("========================================\n");
return;
}
/*******************************************
* 删除双链表
* *****************************************/
void delete_list(List *list)
{
List tmp = *list;
while (tmp->next != NULL)
{
tmp = (*list)->next;
free_node(list);
(*list) = tmp;
}
free_node(&tmp);
tmp = NULL;
*list = NULL;
list = NULL;
return;
}
/********************************************
* 选择位置插入
* *****************************************/
void choose_insert(List list, int pos, int val)
{
if (list == NULL)
{
printf("list is NULL!\n");
return;
}
int lenth = (int)get_lenth(list);
List new_node = create_node(val);
if (pos < 1 || pos > lenth)
printf("wrong position to insert ! list'll not change !\n");
else
{
pos -= 1;
while (pos--)
list = list->next;
List tmp = list->next;
list->next = new_node;
new_node->prior = list;
new_node->next = tmp;
tmp->prior = new_node;
}
return;
}
/*******************************************
* 选择位置删除
* *****************************************/
void choose_delete(List list, int pos)
{
if (list == NULL)
{
printf("list is NULL!\n");
return;
}
int lenth = (int)get_lenth(list);
if (pos < 1 || pos > lenth)
printf("wrong position to delete ! list'll not change !\n");
else
{
pos -= 1;
while (pos--)
list = list->next;
List delete_node = list->next;
list->next = delete_node->next;
delete_node->next->prior = list;
free_node(&delete_node);
}
return;
}
/*******************************************
* 双链表排序
* *****************************************/
void sort(List list)
{
if (list == NULL)
{
printf("list is NULL!\n");
return;
}
int lenth = (int)get_lenth(list);
List head = list;
List find = list;
list = list->next;
for (int i = 0; i < lenth; i++)
{
find = list;
while (list->next != NULL)
{
if (list->next != NULL && find->data < list->next->data)
find = list->next;
list = list->next;
}
List tmp = NULL;
List prior_node = NULL;
if (find != head->next)
{
tmp = head->next;
head->next = find;
prior_node = find->prior;
if (find->next != NULL)
find->next->prior = prior_node;
find->prior = head;
prior_node->next = find->next;
tmp->prior = find;
find->next = tmp;
}
head = head->next;
if (head)
list = head->next;
}
}
/*******************************************
* 双链表倒序
* *****************************************/
void reverse(List list)
{
if (list == NULL)
{
printf("list is NULL!\n");
return;
}
int lenth = (int)get_lenth(list);
List head = list;
for (int i = 0; i < lenth - 1; i++)
{
while (list->next)
list = list->next;
List list_prior = list->prior;
list_prior->next = list->next;
list->next = head->next;
list->prior = head;
head->next = list;
head = head->next;
}
return;
}