头文件
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include<assert.h>
using std::endl;
using std::cout;
using std::cin;class LNode
{
public:LNode(int val = 0):_val(val), _next(nullptr), _prev(nullptr){}int _val;LNode* _next;LNode* _prev;
};class List
{
public: List():_head(nullptr),_tail(nullptr){}~List(){clear();}List(const List& l2):_head(nullptr),_tail(nullptr){if (l2._head == nullptr){return;}LNode* current = l2._head;do{PushBack(current->_val);current = current->_next;} while (current != l2._head);}List& operator=(const List& l2){//不知道怎么写赋值运算符重载}int GetLength();//获取链表长度int Front();// 访问首元素int Back();// 访问尾元素void PushFront(const int val);// 头插法void PushBack(const int val);// 尾插法void PopFront();// 头删法void PopBack(); // 尾删法LNode& FindByValue(int val);// 按值查找void Print();// 遍历打印bool isEmpty(); // 判断链表是否为空void clear();//清理内存// 反转链表// 清空链表// 排序(归并排序示例)// 移除重复元素private:LNode* _head;LNode* _tail;
};
源文件
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include "List.h"int List::GetLength()//获取链表长度
{if (isEmpty()){return 0;}else{int count = 0;LNode* start = _head;do {count++;start = start->_next;} while (start != _head); // 使用do-while确保至少执行一次return count;}
}
int List::Front()// 访问首元素
{assert(_head);return _head->_val;
}int List::Back()// 访问尾元素
{assert(_head);return _tail->_val;
}void List::PushFront(const int val)// 头插法
{LNode* newnode = new LNode(val);if (isEmpty()){_head = _tail = newnode;newnode->_next = newnode->_prev = newnode;}else{LNode* Oldhead = _head;newnode->_next = Oldhead;Oldhead->_prev = newnode;newnode->_prev = _tail;_tail->_next = newnode;_head = newnode;}return;
}
void List::PushBack(const int val)// 尾插法
{LNode* newnode = new LNode(val);if (isEmpty()){_head = _tail = newnode;_head->_next = newnode;_head->_prev = newnode;}else{_tail->_next = newnode;newnode->_prev = _tail;_tail = newnode;_tail->_next = _head;_head->_prev = _tail;}return;}void List::PopFront()// 头删法
{if (isEmpty()){cout << "当前链表为空" << endl;}else if (_head == _tail){delete _head;_head = _tail = nullptr;}else{LNode* Oldhead = _head;LNode* Newhead = Oldhead->_next;Newhead->_prev = _tail;_tail->_next = Newhead;_head = Newhead;delete Oldhead;}return;
}
void List::PopBack()// 尾删法
{if (isEmpty()){cout << "当前链表为空" << endl;}else if (_head == _tail){delete _head;_head = _tail = nullptr;}else{LNode* Oldtail = _head->_prev;LNode* Newtail = Oldtail->_prev;Newtail->_next = _head;_head->_prev = Newtail;_tail = Newtail;delete Oldtail;}return;
}LNode& List::FindByValue(int val)// 按值查找
{if (isEmpty()){throw std::runtime_error("链表为空");}LNode* start = _head;do{if (start->_val == val){return *start;}start = start->_next;} while (start != _head);throw std::runtime_error("没有找到值");}
void List::Print()// 遍历打印
{if (isEmpty()){cout << "当前链表为空" << endl;return;}LNode* start = _head;do{cout << start->_val << " -> ";start = start->_next;} while (start != _head);
}bool List::isEmpty() // 判断链表是否为空
{return _head == nullptr;
}// 反转链表
// 清空链表
// 排序(归并排序示例)
// 移除重复元素void List::clear()//清理内存
{while (!isEmpty()){PopFront();}
}