求解双向循环链表删除数据丢失数据(有注释)。
#include <stdio.h>#include <string.h>
#include <stdlib.h>
//双向循环链表
typedef int data_t;//数据类型
typedef struct list {
data_t data;//数据域
struct list* prev;//指针域上一个
struct list* next;//指针域下一个
}SQlist, * SQlink;
//创建头节点
SQlink Create_Head_Node(data_t Value) {
SQlink head = (SQlink)malloc(sizeof(SQlist));
if (!head)return (SQlink)-1;
memset(head, 0, sizeof(SQlist));
head->data = Value;
head->next = head;
head->prev = head;
return head;//返回头节点
}
//头插添加节点
int Add_Head_Node(SQlink head, data_t Value)
{
if (head == NULL)return -1;
SQlink add_node = Create_Head_Node(Value);
if (add_node == NULL)return -1;
add_node->next = head->next;
add_node->prev = head;
head->next = add_node;
head->prev = add_node;
return 0;
}
//尾插添加节点
int Add_Tail_Node(SQlink head, data_t Value)
{
if (head == NULL)return -1;
SQlink end_node = head;
while (end_node->next != head) {
end_node = end_node->next;
}
SQlink add_node = Create_Head_Node(Value);
if (add_node == (SQlink)-1)return -1;
add_node->next = head->next;
head->next->prev = add_node;
head->next = add_node;
add_node->prev = head;
return 0;
}
//显示链表
int Printf_List(SQlink head)
{
if (head == NULL)return -1;
if (head->next == NULL)return -1;
for (SQlink temp_node = head->next; temp_node != head; temp_node = temp_node->next) {
printf("%2d ", temp_node->data);
}
puts(" ");
return 0;
}
//指定查找
SQlink Find_List_Data(SQlink head, data_t Value)
{
int length = 0;
if (head == NULL && head->next == NULL)return -1;
SQlink find_node = NULL;
for (find_node = head->next; find_node != head; find_node = find_node->next) {
if (find_node->data == Value){
return find_node;
}
}
//for (find_node = head, length = 0; find_node != NULL; find_node = find_node->next, length++)
//if (find_node->data == Value)return length;
//if (find_node->data == Value)return find_node->data;
return (SQlink)-1;
}
//指定插入
int Anywhere_Insert_Node(SQlink head, int Pos, int Value)
{
if (head == NULL)return -1;
SQlink temp_node = NULL;
for (temp_node = head->next; temp_node != head; temp_node = temp_node->next)
if (temp_node->data == Pos)break;
if (temp_node == NULL) return 0;
SQlink new_node = Create_Head_Node(Value);
if (new_node == (SQlink)-1)return -1;
new_node->next = temp_node->next;
temp_node->next = new_node;
temp_node->next->prev = new_node;
new_node->prev = temp_node;
return 0;
}
//指定删除
SQlink Anywhere_Delete_Node(SQlink head, int Value)
{
if (head == NULL || head->next == NULL)return -1;
SQlink delete_node = Find_List_Data(head, Value);
if (delete_node == (SQlink)-1)return -1;
delete_node->prev->next = delete_node->next;
delete_node->next->prev = delete_node->prev;
delete_node->next = NULL;
delete_node->prev = NULL;
free(delete_node);
return 0;
}
//摧毁链表
int Free_List(SQlink head)
{
if (head == NULL)return -1;
if (head->next == NULL)free(head); return 0;//判断空链表
SQlink temp_node = head->next;
SQlink free_node = head->next;
while (temp_node != head) {
temp_node = temp_node->next;
free_node->data = -1;
free(free_node);
free_node = temp_node;
}
free(head);
head = NULL;
return 0;
}
//获取链表长度
int Get_List_Length(SQlink head)
{
if (head == NULL)return -1;
int length = 0;
SQlink temp = head->next;
while (temp != head) {
temp = temp->next;
length++;
}
return length;
}
//主函数
void main(void)
{
SQlink head = Create_Head_Node(0);
if (head == (SQlink)-1)return -1;
//头插数据
Add_Head_Node(head, 11);
Add_Head_Node(head, 12);
Add_Head_Node(head, 13);
Add_Head_Node(head, 14);
Add_Head_Node(head, 15);
Add_Head_Node(head, 16);
//尾插数据
Add_Tail_Node(head, 6);
Add_Tail_Node(head, 7);
Add_Tail_Node(head, 8);
Add_Tail_Node(head, 9);
Add_Tail_Node(head, 1);
Add_Tail_Node(head, 2);
Add_Tail_Node(head, 3);
Add_Tail_Node(head, 4);
Printf_List(head);
//指定插入数据
Anywhere_Insert_Node(head, 1, 6);
Printf_List(head);
//指定删除数据
//PS:如果单独的只删除头插入数据(注释尾插数据)或者只删除尾插入数据(注释头插数据)没有任何问题
//PS:如果头插数据和尾插数据都不注释,只删除后面尾插后的节点就会丢失头插入的数据
Anywhere_Delete_Node(head, 2);//丢失2之前的数据
Printf_List(head);
//获取链表长度
int length = Get_List_Length(head);
printf("链表数据长度为 %d\n", length);
//释放链表
Free_List(head);
}
页:
[1]