首先,我们明确需要实现的功能:
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
// 创建 结点.
ListNode* ListCreate(LTDataType x);
//初始化
ListNode* ListInit();
// 双向链表销毁
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);
//判断
bool ListEmpty(ListNode* phead);
//链表数
int ListSize(ListNode* phead);
实现功能的代码如下:
#include"ListNode.h"
//判断是否为空
bool ListEmpty(ListNode* phead) {
assert(phead);
return phead->next == phead;
}
//打印
void ListPrint(ListNode* phead) {
assert(phead);
ListNode* cur = phead->next;
while (cur != phead) {
printf("%d ", cur->data);
cur = cur->next;
}
}
//创建
ListNode* ListCreate(LTDataType x) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (node == NULL) {
perror("malloc fail");
exit(-1);
}
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}
//头节点
ListNode* ListInit() {
ListNode* phead = ListCreate(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
//尾插
void ListPushBack(ListNode* phead, LTDataType x) {
assert(phead);
//ListNode* newnode = ListCreate(x);
//ListNode* tail = phead->prev;
//tail->next = newnode;
//newnode->prev = tail;
//newnode->next = phead;
//phead->prev = newnode;
ListInsert(phead,x);
}
//头插
void ListPushFront(ListNode* phead, LTDataType x) {
assert(phead);
//ListNode* newnode = ListCreate(x);
//ListNode* head = phead->next;
//phead->next = newnode;
//newnode->prev = phead;
//newnode->next = head;
//head->prev = newnode;
ListInsert(phead->next, x);
}
//尾删
void ListPopBack(ListNode* phead) {
assert(phead);
assert(!ListEmpty(phead));
ListNode* tail = phead->prev;
ListNode* tailprev = tail->prev;
free(tail);
tailprev->next = phead;
phead->prev = tailprev;
}
//头删
void ListPopFront(ListNode* phead) {
assert(phead);
assert(!ListEmpty(phead));
ListNode* head = phead->next;
ListNode* headnext = head->next;
free(head);
phead->next = headnext;
headnext->prev = phead;
}
//指定位置前一位插入
void ListInsert(ListNode* pos, LTDataType x) {
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = ListCreate(x);
prev->next = newnode;
newnode ->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//指定位置删除
void ListErase(ListNode* pos) {
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
//计数
int ListSize(ListNode* phead) {
assert(phead);
ListNode* cur = phead->next;
int size = 0;
while (cur) {
cur = cur->next;
size++;
}
return size;
}
//销毁
void ListDestory(ListNode* phead) {
assert(phead);
ListNode* cur = phead->next;
int size = 0;
while (cur != phead) {
ListNode* next = cur->next;
ListErase(cur);
cur = next;
}
free(phead);
}
这里面有一个细节是,删除操作与插入操作是可以使用其他功能的代码来实现的。这里可以减少面试过程中,实现代码的实现。