链表结构示意图
无论插入还是删除,建议将指定位置前面一个节点和后面一个节点都标记出来,然后就不用考虑次序问题,四句代码任意倒,就可以得出正确的代码
//头文件,包括节点结构体的定义和函数的声明
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
//函数实现的.c文件
#include"list.h"
ListNode* buynewnode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc fail\n");
}
newnode->_data = x;
newnode->_next = NULL;
newnode->_prev = NULL;
return newnode;
}
// 创建返回链表的头结点.
ListNode* ListCreate()
{
ListNode* headnode = (ListNode*)malloc(sizeof(ListNode));
if (headnode == NULL)
{
perror("malloc fail\n");
}
headnode->_data = 0;
headnode->_next = headnode;
headnode->_prev = headnode;
return headnode;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
ListNode* cur = pHead->_next;
while (cur!=pHead)
{
ListNode* next = cur->_next;
free(cur);
//释放当前节点,然后向下继续走
cur=next;
}
free(pHead);
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->_next;
while (cur != pHead)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListInsert(pHead, x);
}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListInsert(pHead->_next, x);
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
ListErase(pHead->_next);
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* cur = pHead->_next;
while (cur != pHead)
{
if (cur->_data == x)
{
printf("found success\n");
return cur;
}
cur = cur->_next;
}
printf("found fail\n");
return pHead;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* newnode = buynewnode(x);
ListNode* prev = pos->_prev;
ListNode* next = pos->_next;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = pos;
pos->_prev = newnode;
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->_prev;
ListNode* next = pos->_next;
prev->_next = next;
next->_prev = prev;
free(pos);
pos = NULL;
}
//测试结果是否正确的.c文件
#include"list.h"
int main()
{
ListNode* s;
int a = 0;
s = ListCreate();
ListPushBack(s, 5);
ListPushBack(s, 4);
ListPushBack(s, 3);
ListPushBack(s, 2);
ListPrint(s);
printf("\n");
ListPushFront(s, 1);
ListPushFront(s, 8);
ListPushFront(s, 9);
ListPrint(s);
printf("\n");
ListPopFront(s);
ListPopFront(s);
ListPopFront(s);
ListPopFront(s);
ListPrint(s);
printf("\n");
a=ListFind(s, 4)->_data;
printf("%d\n", a);
a=ListFind(s, 9)->_data;
printf("%d\n", a);
return 0;
}