带头循环双向链表的实现
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct ListNode
{
SLTDataType data;
struct ListNode* next;
struct ListNode* prev;
}LTNode;
LTNode* ListInit(); //初始化
void ListDestroy(LTNode* phead);//销毁链表
void ListPrint(LTNode* phead);//打印
LTNode* BuySListNode(SLTDataType x);//开辟新节点
void ListPushBack(LTNode* phead, SLTDataType x); //尾插
void ListPopBack(LTNode* phead); //尾删
void ListPushFront(LTNode* phead, SLTDataType x); //头插
void ListPopFront(LTNode* phead); //头删
LTNode* ListFind(LTNode* phead,SLTDataType x);//找到X的位置
void ListInsert(LTNode* pos, SLTDataType x);//在pos之前插入数据X
void ListErase(LTNode* pos);//删除pos位置的数据
#include"List.h"
//初始化
LTNode* ListInit()
{
//哨兵位头节点
LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
phead->prev = phead;
phead->next = phead;
return phead;
}
//开辟新节点
LTNode* BuySListNode(SLTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
//打印
void ListPrint(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
//尾插
void ListPushBack(LTNode* phead, SLTDataType x)
{
assert(phead);
LTNode* tail = phead->prev;
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
newnode->data = x;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
//法二
//ListInsert(phead, x);
}
//尾删
void ListPopBack(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->prev;
LTNode* tailPrev = tail->prev;
free(tail);
tailPrev->next = phead;
phead->prev = tailPrev;
//法二
//ListErase(phead->prev);
}
//头插
void ListPushFront(LTNode* phead, SLTDataType x)
{
assert(phead);
LTNode* next = phead->next;
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
newnode->data = x;
phead->next = newnode;
newnode->prev = phead;
newnode->next = next;
next->prev = newnode;
//法二
//ListInsert(phead->next, x);
}
//头删
void ListPopFront(LTNode* phead)
{
assert(phead);
//链表为空
assert(phead->next != phead);
LTNode* next = phead->next;
LTNode* nextNext = next->next;
phead->next = nextNext;
nextNext->prev = phead;
free(next);
//法二
//ListErase(phead->next);
}
//找到X的位置
LTNode* ListFind(LTNode* phead, SLTDataType x)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos之前插入数据X
void ListInsert(LTNode* pos, SLTDataType x)
{
assert(pos);
LTNode* newnode = BuySListNode(x);
LTNode* posPrev = pos->prev;
posPrev->next = newnode;
newnode->prev = posPrev;
newnode->next = pos;
pos->prev = newnode;
}
//删除pos位置的数据
void ListErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
pos = NULL;
}
//销毁链表
void ListDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = (phead)->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
#include"List.h"
void TestList1()
{
LTNode* plist = ListInit();
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPrint(plist);
ListPopBack(plist);
ListPrint(plist);
}
void TestList2()
{
LTNode* plist = ListInit();
ListPushFront(plist, 1);
ListPushFront(plist, 2);
ListPushFront(plist, 3);
ListPushFront(plist, 4);
ListPrint(plist);
ListPopFront(plist);
ListPrint(plist);
LTNode* pos = ListFind(plist, 2);
if (pos != NULL)
{
ListErase(pos);
}
ListPrint(plist);
ListDestroy(plist);
plist = NULL;
}
int main()
{
//TestList1();
TestList2();
return 0;
}
641

被折叠的 条评论
为什么被折叠?



