//DoubleList.h
#pragma once
typedef int Type;
typedef struct Node {
struct Node* next;
Type val;
struct Node* pre;
}Node;
typedef struct List {
struct Node* header;
};
Node* CreateNode(Type val);
void InitList(List* list);
void ListPushBack(List* list,Type val);
void ListPopBack(List* list);
void ListPushFront(List* list,Type val);
void ListPopFront(List* list);
void ListRemoveNode(Node* node);
void ListInsertNode(Node* node,Type val);
void DestroyList(List* list);
Node* ListFind(List* list, Type val);
void PrintList(List* list);
list.cpp
#include"DoubleList .h"
#include<stdio.h>
#include<stdlib.h>
Node* CreateNode(Type val)
{
Node* node = (Node*)malloc(sizeof(Node));
node->next = NULL;
node->pre = NULL;
node->val = val;
return node;
}
void InitList(List* list)
{
list->header = CreateNode(0);
list->header->next = list->header;
list->header->pre = list->header;
}
void ListPushBack(List* list, Type val)
{
Node* last = list->header->pre;
Node* node = CreateNode(val);
last->next = node;
node->pre = last;
node->next = list->header;
list->header->pre = node;
}
void ListPopBack(List* list)
{
if (list->header == list->header->next)
return;
Node* last = list->header->pre;
list->header->pre = last->pre;
last->pre->next = list->header;
free(last);
}
void ListPushFront(List* list, Type val)
{
Node* node = CreateNode(val);
node->next = list->header->next;
node->pre = list->header;
list->header->next->pre = node;
list->header->next = node;
}
void ListPopFront(List* list)
{
if (list->header == list->header->next)
return;
Node* del = list->header->next;
list->header->next = del->next;
del->next->pre = list->header;
free(del);
}
void ListRemoveNode(Node* node)
{
if (node->next == node)
return;
Node* pre = node->pre;
Node* next = node->next;
pre->next = next;
next->pre = pre;
free(node);
}
void ListInsertNode(Node* node, Type val)
{
Node* nd = CreateNode(val);
Node* pre = node->pre;
nd->pre = pre;
nd->next = node;
node->pre = nd;
pre->next = nd;
}
void DestroyList(List* list)
{
Node* head = list->header->next;
Node* next = NULL;
while (head != list->header)
{
next = head->next;
free(head);
head = head->next;
}
free(list->header);
list->header = NULL;
}
Node* ListFind(List* list,Type val)
{
if (list->header->next == list->header)
return NULL;
Node* begin = list->header->next;
while (begin != list->header)
{
if (begin->val == val)
return begin;
begin = begin->next;
}
return NULL;
}
void PrintList(List* list)
{
Node* head = list->header->next;
while (head != list->header)
{
printf("%d “, head->val);
head = head->next;
}
printf(”\n");
}
main.cpp
#include<stdio.h>
#include<stdlib.h>
#include"DoubleList .h"
void test()
{
List list;
InitList(&list);
ListPushBack(&list, 1);
PrintList(&list);
ListPushBack(&list, 2);
PrintList(&list);
ListPushBack(&list, 3);
PrintList(&list);
ListPopBack(&list);
PrintList(&list);
ListPopBack(&list);
PrintList(&list);
ListPopBack(&list);
PrintList(&list);
ListPopBack(&list);
PrintList(&list);
ListPopBack(&list);
PrintList(&list);
ListPushFront(&list, 3);
PrintList(&list);
ListPushFront(&list, 2);
PrintList(&list);
ListPushFront(&list, 1);
PrintList(&list);
ListPopFront(&list);
PrintList(&list);
ListPopFront(&list);
PrintList(&list);
ListPopFront(&list);
PrintList(&list);
ListPopFront(&list);
PrintList(&list);
ListPopFront(&list);
PrintList(&list);
Node* node = list.header;
ListInsertNode(node,1);
PrintList(&list);
ListRemoveNode(node->next);
PrintList(&list);
ListPushBack(&list, 1);
PrintList(&list);
ListPushBack(&list, 2);
PrintList(&list);
ListPushBack(&list, 3);
PrintList(&list);
ListPushBack(&list, 4);
PrintList(&list);
Node* find = ListFind(&list, 1);
if (find == NULL)
{
printf("未找到!\n");
}
else
{
printf("%p\n", find);
}
find = ListFind(&list, 2);
if (find == NULL)
{
printf("未找到!\n");
}
else
{
printf("%p\n", find);
}
find = ListFind(&list, 3);
if (find == NULL)
{
printf("未找到!\n");
}
else
{
printf("%p\n", find);
}
find = ListFind(&list, 4);
if (find == NULL)
{
printf("未找到!\n");
}
else
{
printf("%p\n", find);
}
find = ListFind(&list,5);
if (find == NULL)
{
printf("未找到!\n");
}
else
{
printf("%p\n", find);
}
}
int main(void)
{
test();
return 0;
}