main.c
文件
#define _CRT_SECURE_NO_WARNINGS
#include"List.h"
int main()
{
ListTest();
system("pause");
return 0;
}
List.h
文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//带头结点的双向循环链表
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode *_prev;
struct ListNode *_next;
}ListNode;
typedef struct List
{
ListNode *head;
}List;
void ListInit(List *plist);
void ListDestory(List *plist);
void ListPushBack(List *plist, LTDataType x);
void ListPopBack(List *plist);
void ListPushFront(List *plist, LTDataType x);
void ListPopFront(List *plist);
//pos位置之前插入
void ListInsert(ListNode *pos, LTDataType x);
//删除pos位置
void ListErase(ListNode *pos);
ListNode * ListFind(List *plist, LTDataType x);
void ListPrint(List *plist);
void ListTest();
List.c
文件
#include"List.h"
ListNode *BuyListNode(LTDataType x)
{
ListNode *newNode = (ListNode *)malloc(sizeof(ListNode));
assert(newNode);
newNode->_data = x;
newNode->_next = NULL;
newNode->_prev = NULL;
return newNode;
}
void ListInit(List *plist)
{
assert(plist);
plist->head = BuyListNode(0);
plist->head->_next = plist->head;
plist->head->_prev = plist->head;
}
void ListDestory(List *plist)
{
assert(plist);
ListNode *cur = plist->head->_next;
ListNode *next;
while (cur != plist->head)
{
next = cur->_next;
free(cur);
cur = next;
}
free(plist->head);
plist->head = NULL;
}
void ListPushBack(List *plist, LTDataType x)
{
assert(plist);
/*ListNode *newNode = BuyListNode(x);
ListNode *head = plist->head;
ListNode *tail = head->_prev;
tail->_next = newNode;
newNode->_prev = tail;
newNode->_next = head;
head->_prev = newNode;*/
ListInsert(plist->head, x);
}
void ListPopBack(List *plist)
{
assert(plist);
/*ListNode *head = plist->head;
ListNode *tail = head->_prev;
ListNode*prev = tail->_prev;
prev->_next = head;
head->_prev = prev;
free(tail);
tail = NULL;*/
ListErase(plist->head->_prev);
}
void ListPushFront(List *plist, LTDataType x)
{
assert(plist);
/*ListNode *head = plist->head;
ListNode *next = head->_next;
ListNode *newNode = BuyListNode(x);
head->_next = newNode;
newNode->_prev = head;
newNode->_next = next;
next->_prev = newNode;*/
ListInsert(plist->head->_next, x);
}
void ListPopFront(List *plist)
{
assert(plist);
/*ListNode *head = plist->head;
ListNode *next = head->_next;
ListNode *nextNext = next->_next;
head->_next = nextNext;
nextNext->_prev = head;
free(next);
next = NULL;*/
ListErase(plist->head->_next);
}
void ListInsert(ListNode *pos, LTDataType x)
{
assert(pos);
ListNode *prev = pos->_prev;
ListNode *newNode = BuyListNode(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);
pos = NULL;
}
ListNode * ListFind(List *plist, LTDataType x)
{
assert(plist);
ListNode *head = plist->head;
ListNode *cur = head->_next;
while (cur != head)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void ListPrint(List *plist)
{
ListNode *head = plist->head;
ListNode *cur = head->_next;
while (cur != head)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("<->\n");
}
void ListTest()
{
List lt;
ListInit(<);
ListPushBack(<, 1);
ListPushBack(<, 3);
ListPushBack(<, 4);
ListPopBack(<);
ListPopFront(<);
ListPrint(<);
}