#pragma once
#include<iostream>
using namespace std;typedef int LTDataType;
//双向带头循环链表
class ListNode
{
public:
class Node
{
public:
LTDataType data;
Node* next;//后继
Node* prev;//后继
};
Node* head;//头结点
public:
Node* BuyListNode(LTDataType x);//开辟新结点
void ListInit();//初始化
void ListPrint();//打印
void ListDestroy();//销毁链表
void ListPushBack(LTDataType x);//尾插
void ListPushFront(LTDataType x);//头插
void ListPopBack();//尾删
void ListPopFront();//头删
Node* ListFind(LTDataType x);//查找
void ListInsert(Node* pos, LTDataType x);//在pos位置之前插入
void ListErase(Node* pos);//删除pos位置的值
bool ListEmpty();//判断链表是否为空
int ListSize();//链表长度
};
#include"List.h"
ListNode::Node* ListNode::BuyListNode(LTDataType x)//开辟新结点
{
Node* newnode = new Node;
newnode->data = x;
newnode->prev = nullptr;
newnode->next = nullptr;
return newnode;
}void ListNode::ListInit()//初始化
{
head = BuyListNode(0);//开辟头结点
head->next = head;
head->prev = head;
}void ListNode::ListPrint()//打印
{
Node* cur = head->next;//首元结点
while (cur != head)
{
cout << cur->data << "->";
cur = cur->next;
}
cout << "nullptr" << endl;
}void ListNode::ListDestroy()//销毁链表
{
Node* cur = head->next;
while (cur != head)
{
Node* next = cur->next;
delete cur;
cur = next;
}
delete head;
head = nullptr;
}void ListNode::ListPushBack(LTDataType x)//尾插
{
//Node* tail = head->prev;//尾结点
//Node* newnode = BuyListNode(x);//开辟新结点//tail->next = newnode;//尾结点后继指向新结点
//newnode->prev = tail;//新结点前驱指向尾结点
//newnode->next = head;//新结点后继指向头结点
//head->prev = newnode;//头结点前驱指向新结点ListInsert(head, x);
}void ListNode::ListPushFront(LTDataType x)//头插
{
//Node* first = head->next;//首元结点
//Node* newnode = BuyListNode(x);//开辟新结点
//
//head->next = newnode;//头结点后继指向新结点
//newnode->prev = head;//新结点前驱指向头结点
//newnode->next = first;//新结点后继指向首元结点
//first->prev = newnode;//首元结点前驱指向新结点ListInsert(head->next, x);
}void ListNode::ListPopBack()//尾删
{
//if (head->next == head)
//{
// return;
//}
//Node* tail = head->prev;//尾结点
//Node* prev = tail->prev;//尾结点的前驱
//prev->next = head;
//head->prev = prev;//delete tail;
//tail = nullptr;ListErase(head->prev);
}void ListNode::ListPopFront()//头删
{
/*if (head->next == head)
{
return;
}
Node* first = head->next;
Node* second = first->next;
head->next = second;
second->prev = head;
delete first;
first = nullptr;*/ListErase(head->next);
}ListNode::Node* ListNode::ListFind(LTDataType x)//查找
{
Node* cur = head->next;
while (cur != head)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return nullptr;
}void ListNode::ListInsert(Node* pos, LTDataType x)//在pos之前插入
{
if (pos == nullptr)
{
return;
}
Node* prev = pos->prev;
Node* newnode = BuyListNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}void ListNode::ListErase(Node* pos)//删除pos位置的值
{
if (pos == nullptr)
{
return;
}
Node* prev = pos->prev;
Node* next = pos->next;
prev->next = next;
next->prev = prev;
delete pos;
pos = nullptr;
}bool ListNode::ListEmpty()//判断链表是否为空
{
if (head->next == head)
{
return true;
}
return false;
}int ListNode::ListSize()//链表长度
{
int size = 0;
Node* cur = head->next;
while (cur != head)
{
size++;
cur = cur->next;
}
return size;
}
#include"List.h"
void test01()
{
ListNode list;
list.ListInit();
list.ListPushBack(1);
list.ListPushBack(2);
list.ListPushBack(3);
cout << "表长: " << list.ListSize() << endl;
list.ListPushBack(4);
list.ListPushFront(0);
list.ListPushFront(-1);
list.ListPrint();
if (list.ListEmpty())
{
cout << "链表为空" << endl;
}
else
{
cout << "链表不为空" << endl;
}
cout << "表长: " << list.ListSize() << endl;
list.ListPopFront();
list.ListPopFront();
list.ListPopFront();
list.ListPopBack();
list.ListPopBack();
list.ListPopBack();
list.ListPrint();
if (list.ListEmpty())
{
cout << "链表为空" << endl;
}
else
{
cout << "链表不为空" << endl;
}
cout << "表长: " << list.ListSize() << endl;
list.ListDestroy();
}void test02()
{
ListNode list;
list.ListInit();
list.ListPushBack(1);
list.ListPushBack(2);
list.ListPushBack(3);
list.ListPushBack(4);
list.ListPrint();ListNode::Node* pos = list.ListFind(3);
if (pos)
{
pos->data *= 10;
cout << "找到了,并且结点的值乘以10" << endl;
}
else
{
cout << "未找到" << endl;
}
list.ListPrint();list.ListInsert(pos, 300);
list.ListPrint();
list.ListErase(pos);
list.ListPrint();
}int main()
{
test01();
}