描述:一个节点内包含两个指针,一个指向上一个节点,另一个指向下一个节点。哨兵位指向的下一个节点为头节点,哨兵位的上一个指向尾节点。
结构优势:高效率找尾节点;高效率插入与删除;无需判断多种复杂情况,如尾节点、空节点等。
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include
#include
#include
typedef int DataType;
typedef struct ListNode {
struct ListNode* prev;
struct ListNode* next;
DataType data;
}ListNode;
ListNode* BuyListNode(DataType x);
ListNode* InitList();
void DestroyList(ListNode* phead);
void Print(ListNode* phead);
int CountSize(ListNode* phead);
void PushBack1(ListNode* phead, DataType x);
void PushBack2(ListNode* phead, DataType x);
void PopBack1(ListNode* phead);
void PopBack2(ListNode* phead);
void PushFront1(ListNode* phead, DataType x);
void PushFront2(ListNode* phead, DataType x);
void PopFront1(ListNode* phead);
void PopFront2(ListNode* phead);
void Insert(ListNode* pos, DataType x);
void Erase(ListNode* pos);
ListNode* BuyListNode(DataType x) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (node == NULL) {
perror("malloc fail");
exit(-1);
}
node->data = x;
node->prev = NULL;
node->next = NULL;
return node;
}
ListNode* InitList() {
ListNode* phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
void DestroyList(ListNode* phead) {
assert(phead);
ListNode* cur = phead->next;
while (cur != phead) {
ListNode* curnext = cur->next;
free(cur);
cur = curnext;
}
free(phead);
}
void Print(ListNode* phead) {
assert(phead);
printf("phead<=>");
ListNode* cur = phead->next;
while (cur != phead) {
printf("%d<=>", cur->data);
cur = cur->next;
}
printf("\n");
}
int CountSize(ListNode* phead) {
assert(phead);
int size = 0;
ListNode* cur = phead->next;
while (cur != phead) {
size++;
cur = cur->next;
}
return size;
}
//在pos前插入
void Insert(ListNode* pos, DataType x) {
assert(pos);
ListNode* posprev = pos->prev;
ListNode* newnode = BuyListNode(x);
posprev->next = newnode;
newnode->prev = posprev;
newnode->next = pos;
pos->prev = newnode;
}
void Erase(ListNode* pos) {
assert(pos);
ListNode* posprev = pos->prev;
ListNode* posnext = pos->next;
free(pos);
posprev->next = posnext;
posnext->prev = posprev;
}
void PushBack1(ListNode* phead, DataType x) {
ListNode* tail = phead->prev;
ListNode* newnode = BuyListNode(x);
newnode->next = phead;
phead->prev = newnode;
tail->next = newnode;
newnode->prev = tail;
}
void PushBack2(ListNode* phead, DataType x) {
//尾插就相当于在哨兵位head前插入
Insert(phead, x);
}
void PopBack1(ListNode* phead) {
assert(phead);
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* tailprev = tail->prev;
free(tail);
tailprev->next = phead;
phead->prev = tailprev;
}
void PopBack2(ListNode* phead) {
//尾节点就是phead的prev节点
Erase(phead->prev);
}
void PushFront1(ListNode* phead, DataType x) {
assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* pheadnext = phead->next;
newnode->next = pheadnext;
pheadnext->prev = newnode;
phead->next = newnode;
newnode->prev = phead;
}
void PushFront2(ListNode* phead, DataType x) {
//头插就相当于在phead后一个节点的前面插入
assert(phead);
Insert(phead->next, x);
}
void PopFront1(ListNode* phead) {
assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
free(first);
phead->next = second;
second->prev = phead;
}
void PopFront2(ListNode* phead) {
//头节点时哨兵位phead的下一个节点
Erase(phead->next);
}
#define _CRT_SECURE_NO_WARNINGS
#include"list.h"
void test_PushBack() {
ListNode* plist = InitList();
PushBack1(plist, 1);
PushBack1(plist, 2);
PushBack1(plist, 3);
PushBack2(plist, 1);
PushBack2(plist, 2);
PushBack2(plist, 3);
Print(plist);
DestroyList(plist);
}
void test_PopBack() {
ListNode* plist = InitList();
PushBack1(plist, 1);
PushBack1(plist, 2);
PushBack1(plist, 3);
PushBack2(plist, 1);
PushBack2(plist, 2);
PushBack2(plist, 3);
Print(plist);
PopBack1(plist);
Print(plist);
PopBack2(plist);
Print(plist);
DestroyList(plist);
}
void test_PushFront() {
ListNode* plist = InitList();
PushFront1(plist, 1);
PushFront1(plist, 2);
PushFront1(plist, 3);
PushFront2(plist, 1);
PushFront2(plist, 2);
PushFront2(plist, 3);
Print(plist);
DestroyList(plist);
}
void test_PopFront() {
ListNode* plist = InitList();
PushFront1(plist, 1);
PushFront1(plist, 2);
PushFront1(plist, 3);
PushFront2(plist, 1);
PushFront2(plist, 2);
PushFront2(plist, 3);
Print(plist);
PopFront1(plist);
Print(plist);
PopFront2(plist);
Print(plist);
DestroyList(plist);
}
int main() {
//测试尾插
test_PushBack();
//测试尾删
test_PopBack();
//测试头插
test_PushFront();
//测试头删
test_PopFront();
}