接口
//带头双向链表
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
typedef int datatype;
typedef struct ListNode{
datatype data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
typedef struct TwoList{
struct ListNode* head;
}TwoList;
//初始化
void TwoListInit(TwoList* pt);
//销毁
void TwoListDestroy(TwoList* pt);
//尾插
void TwoListPushBack(TwoList* pt, datatype x);
//头插
void TwoListPushFront(TwoList* pt, datatype x);
//尾删
void TwoListPopback(TwoList* pt);
//头删
void TwoListPopFront(TwoList* pt);
//查找
ListNode* TwoListFind(TwoList* pt, datatype x);
//在pos 前面插入
void TwoListInsert(ListNode* pos, datatype x);
//删除pos位置的元素
void TwoListErase(ListNode* pos);
//删除x
void TwoListRemove(TwoList* pt, datatype x);
void Printf(TwoList* pt);
实现
#include "TwoList.h"
//初始化
void TwoListInit(TwoList* pt){
assert(pt);
pt->head = (ListNode*)malloc(sizeof(ListNode));
pt->head->data = 0;
pt->head->next = pt->head;
pt->head->prev = pt->head;
}
//销毁
void TwoListDestroy(TwoList* pt){
assert(pt);
ListNode* head = pt->head;
ListNode* cur = head->next;
ListNode* next = cur->next;
while (cur != head){
free(cur);
cur = next;
next = next->next;
}
free(head);
pt->head = NULL;
}
//尾插
void TwoListPushBack(TwoList* pt, datatype x){
assert(pt);
ListNode* head = pt->head;
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = x;
head->prev->next = newNode;
newNode->prev = head->prev;
newNode->next = head;
head->prev = newNode;
}
//头插
void TwoListPushFront(TwoList* pt, datatype x){
assert(pt);
ListNode* head = pt->head;
ListNode* next = head->next;
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = x;
head->next = newNode;
next->prev = newNode;
newNode->next = next;
newNode->prev = head;
}
//尾删
void TwoListPopback(TwoList* pt){
assert(pt);
//链表为空
if (pt->head->next == pt->head){
return;
}
ListNode* cur = pt->head->prev;
ListNode* prev = cur->prev;
free(cur);
prev->next = pt->head;
pt->head->prev = prev;
}
//头删
void TwoListPopFront(TwoList* pt){
assert(pt);
ListNode* cur = pt->head->next;
ListNode* next = cur->next;
free(cur);
cur = NULL;
pt->head->next = next;
next->prev = pt->head;
}
//查找
ListNode* TwoListFind(TwoList* pt, datatype x){
assert(pt);
ListNode* cur, *next;
if (pt->head->next == pt->head){
return NULL;
}
cur = pt->head->next;
next = cur->next;
while (cur != pt->head){
if (cur->data = x){
return cur;
}
cur = next;
next = next->next;
}
return NULL;
}
//在pos 前面插入
void TwoListInsert(ListNode* pos, datatype x){
assert(pos);
ListNode* prev = pos->prev;
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = x;
prev->next = newNode;
pos->prev = newNode;
newNode->prev = prev;
newNode->next = pos;
}
//删除pos位置的元素
void TwoListErase(ListNode* pos){
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
free(pos);
pos = NULL;
prev->next = next;
next->prev = prev;
}
//删除x
void TwoListRemove(TwoList* pt, datatype x){
assert(pt);
ListNode* prev = pt->head;
ListNode* cur = pt->head->next;
ListNode* next = cur->next;
while (cur != pt->head){
if (cur->data == x){
free(cur);
cur = NULL;
prev->next = next;
next->prev = prev;
return;
}
prev = cur;
cur = next;
next = next->next;
}
}
void Printf(TwoList* pt){
assert(pt);
ListNode* head = pt->head;
ListNode* cur = head->next;
ListNode* next = cur->next;
while(cur != head){
printf("%d ", cur->data);
cur = next;
next = next->next;
}
}