头文件
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTdAateType;
typedef struct ListNode
{
LTdAateType data;
struct ListNode *next;
struct ListNode *prev;
}ListNode;
ListNode *InitListNode();
void ListPushBack(ListNode* plist, LTdAateType x);
void ListPrint(ListNode* plist);
void ListPushFront(ListNode* plist, LTdAateType x);
void ListPopBack(ListNode* plist);
void ListPopFront(ListNode* plist);
ListNode* ListFind(ListNode* plist, LTdAateType x);
void ListInsert(ListNode* pos, LTdAateType x);
void ListErase(ListNode* pos);
实现
#include "List.h"
ListNode *BuyListNode(LTdAateType x)
{
ListNode *NewNode = (struct ListNode*)malloc(sizeof(struct ListNode));
NewNode->data = x;
NewNode->next = NULL;
NewNode->prev = NULL;
return NewNode;
}
ListNode *InitListNode()
{
ListNode *phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPushBack(ListNode* plist, LTdAateType x)
{
assert(plist);
ListNode *tail = plist->prev;
ListNode *Newnode = BuyListNode(x);
tail->next = Newnode;
Newnode->next = plist;
Newnode->prev = tail;
plist->prev = Newnode;
}
void ListPrint(ListNode* plist)
{
assert(plist);
ListNode *cur = plist->next;
while (cur != plist)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void ListPushFront(ListNode* plist, LTdAateType x)
{
assert(plist);
ListNode *first = plist->next;
ListNode *newnode = BuyListNode(x);
newnode->next = first;
newnode->prev = plist;
plist->next = newnode;
first->prev = newnode;
}
void ListPopBack(ListNode* plist)
{
assert(plist);
assert(plist->next != plist);
ListNode *tail = plist->prev;
plist->prev = tail->prev;
(tail->prev)->next = plist;
free(tail);
}
void ListPopFront(ListNode* plist)
{
assert(plist);
assert(plist->next != plist);
ListNode *del = plist->next;
ListNode *delnext = del->next;
delnext->prev = plist;
plist->next = delnext;
free(del);
}
ListNode* ListFind(ListNode* plist, LTdAateType x)
{
assert(plist);
ListNode *cur = plist->next;
while (cur != plist)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void ListInsert(ListNode* pos, LTdAateType x)
{
assert(pos);
ListNode *newnode = BuyListNode(x);
newnode->next = pos;
(pos->prev)->next = newnode;
newnode->prev = (pos->prev);
pos->prev = newnode;
}
void ListErase(ListNode* pos)
{
assert(pos);
ListNode *prev = pos->prev;
ListNode *next = pos->next;
prev->next = next;
next->prev = prev;
}
|