List.h
#ifndef __LIST_H__
#define __LIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
typedef struct List
{
ListNode* _head;
}List;
void ListNodeInit(List* plist);
void ListNodeDestory(List* plist);
void ListNodePrint(List* plist);
void ListNodePushBack(List* plist, LTDataType x);
void ListNodePopBack(List* plist);
void ListNodePushFront(List* plist, LTDataType x);
void ListNodePopFront(List* plist);
ListNode* ListFind(List* plist, LTDataType x);
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(ListNode* pos);
void ListRemove(List* plist, LTDataType x);
void ReverseList(List* plist);
int ListSize(List* plist);
int ListEmpty(List* plist);
#endif
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 ListNodeInit(List* plist)
{
assert(plist != NULL);
ListNode* head = BuyListNode(0);
head->_next = head;
head->_prev = head;
plist->_head = head;
}
void ListNodePushBack(List* plist, LTDataType x)
{
assert(plist != NULL);
ListNode* newnode = BuyListNode(x);
ListNode* head = plist->_head;
ListNode* tail = head->_prev;
newnode->_prev = tail;
tail->_next = newnode;
newnode->_next = head;
head->_prev = newnode;
}
void ListNodePopBack(List* plist)
{
assert(plist != NULL && plist->_head->_next != plist->_head);
ListNode* head = plist->_head;
ListNode* tail = head->_prev;
ListNode* tailprev = tail->_prev;
free(tail);
tail = NULL;
tailprev->_next = head;
head->_prev = tailprev;
}
void ListNodePushFront(List* plist, LTDataType x)
{
assert(plist != NULL);
ListNode* newnode = BuyListNode(x);
ListNode* head = plist->_head;
ListNode* next = plist->_head->_next;
head->_next = newnode;
newnode->_prev = head;
newnode->_next = next;
next->_prev = newnode;
}
void ListNodePopFront(List* plist)
{
assert(plist != NULL);
ListNode* head = plist->_head;
ListNode* next = plist->_head->_next;
ListNode* nextnext = plist->_head->_next->_next;
free(next);
next = NULL;
head->_next = nextnext;
nextnext->_prev = head;
}
ListNode* ListFind(List* plist, LTDataType x)
{
assert(plist != NULL);
ListNode* cur = plist->_head->_next;
while (cur != plist->_head)
{
if (cur->_data == x)
{
return cur;
}
else
{
cur = cur->_next;
}
}
return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos != NULL);
ListNode* newnode = BuyListNode(x);
ListNode* prev = pos->_prev;
newnode->_prev = prev;
prev->_next = newnode;
newnode->_next = pos;
pos->_prev = newnode;
}
void ListErase(ListNode* pos)
{
assert(pos != NULL);
ListNode* prev = pos->_prev;
ListNode* next = pos->_next;
free(pos);
pos = NULL;
prev->_next = next;
next->_prev = prev;
}
void ListRemove(List* plist, LTDataType x)
{
assert(plist != NULL);
ListNode* cur = plist->_head->_next;
ListNode* prev = NULL;
ListNode* next = NULL;
while (cur != plist->_head)
{
if (cur->_data == x)
{
prev = cur->_prev;
next = cur->_next;
free(cur);
prev->_next = next;
next->_prev = prev;
cur = next;
}
else
{
cur = cur->_next;
}
}
}
void ReverseList(List* plist)
{
assert(plist != NULL);
ListNode* begin = plist->_head->_next;
ListNode* end = plist->_head->_prev;
while (begin != end && begin->_next != end)
{
LTDataType tmp = begin->_data;
begin->_data = end->_data;
end->_data = tmp;
begin = begin->_next;
end = end->_prev;
}
}
int ListSize(List* plist)
{
assert(plist != NULL);
ListNode* cur = plist->_head->_next;
int count = 0;
while (cur != plist->_head)
{
count++;
cur = cur->_next;
}
return count;
}
int ListEmpty(List* plist)
{
assert(plist != NULL);
return (plist->_head->_next == plist->_head ? 0 : 1);
}
void ListNodeDestory(List* plist)
{
assert(plist != NULL);
ListNode* cur = plist->_head->_next;
while (cur != plist->_head)
{
ListNode* next = cur->_next;
free(cur);
cur = next;
}
free(plist->_head);
plist->_head = NULL;
}
void ListNodePrint(List* plist)
{
assert(plist != NULL);
ListNode* cur = plist->_head->_next;
printf("<-head->");
while (cur != plist->_head)
{
printf("<-%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
}