#include<iostream>
using namespace std;
namespace my {
//定义单链表节点
template<class T>
struct ListNode {
ListNode(const T& x = T())
:data(x)
, next(nullptr)
{}
T data;
ListNode<T>* next;
};
//封装迭代器
template<class T>
class ListNodeIterator {
public:
typedef ListNodeIterator<T> Self;
ListNodeIterator( ListNode<T>* l)
:_node(l)
{}
T& operator*() {
return _node->data;
}
T* operator->() {
return &(operator*());
}
Self& operator++() {
_node = _node->next;
return *this;
}
Self& operator++(int) {
Self& temp = this;
_node = _node->next;
return *temp;
}
bool operator==(const Self& s) {
return _node == s._node;
}
bool operator!=(const Self& s) {
return _node != s._node;
}
private:
ListNode<T>* _node;
};
//链表类
template<class T>
class List {
public:
typedef ListNodeIterator<T> iterator;
public:
List() {
_head = new ListNode<T>;
_size = 0;
}
iterator begin() {
return iterator(_head->next);
}
iterator end() {
return iterator(Get_the_most_last()->next);
}
void push_back(const T& val) {
//使用尾插法
ListNode<T>* Last = Get_the_most_last();//得到链表最后一个元素
ListNode<T>* newnode = new ListNode<T>(val);//定义新节点
Last->next = newnode;//将新节点挂入链表之中
_size++;//修改链表中元素个数
}
void Push_front(const T& val) {
//使用头插法
ListNode<T>* newnode = new ListNode<T>(val);//定义新节点
if (_head->next == nullptr) {
//链表为空的情况
_head->next = newnode;
}
else {
newnode->next = _head->next;
_head->next = newnode;
}
_size++;
}
void pop_back() {
//尾删
ListNode<T>* last = Get_the_most_last();//得到链表最后最后一个节点
ListNode<T>* cur = _head;
while (cur->next != last) {
//查找链表待删除节点的前一个节点
cur = cur->next;
}
delete last;
cur->next = nullptr;
_size--;
}
void pop_front() {
//头删
ListNode<T>* cur = _head->next;
_head->next = cur->next;
delete cur;
cur = nullptr;
_size--;
}
void pop_listbyval(const T& val) {
//删除链表中值为val元素
//待删除节点为首元节点
if (_head->next->data == val) {
pop_front();
return;
}
//链表为空,直接退出
if (_head->next == nullptr)
return;
//查找待删除节点位置
ListNode<T>* cur = _head->next;
while (cur->next && cur->next->data != val) {
cur = cur->next;
}
//删除
ListNode<T>* del = cur->next;
cur->next = cur->next->next;
delete del;
_size--;
}
void Show_List() {
//打印链表
ListNode<T>* cur = _head->next;
while (cur) {
cout << cur->data << " ";
cur = cur->next;
}
}
size_t size() {
//链表中插入元素个数
return _size;
}
size_t find_val(const T& val) {
//按值查找,找到返回所在位置
ListNode<T>* cur = _head->next;
size_t pos = 1;
while (cur) {
if (cur->data == val)
return pos;
pos++;
cur = cur->next;
}
return -1;
}
void modificy_list(const T& desc, const T& data) {
//修改链表中元素
ListNode<T>* cur = _head->next;
while (cur) {
if (cur->data == desc)
cur->data = data;
cur = cur->next;
}
}
void List_reverse() {
//逆置链表
ListNode<T>* cur = _head->next;
ListNode<T>* next = _head->next->next;
cur->next = nullptr;
while (next) {
cur = next;
next = next->next;
cur->next = _head->next;
_head->next = cur;
}
}
~List()
{
clear();
}
private:
ListNode<T>* _head;
size_t _size;
private:
ListNode<T>* Get_the_most_last() {
//返回链表中最后一个元素
ListNode<T>* cur = _head;
while (cur->next) {
cur = cur->next;
}
return cur;
}
void clear() {
//回收链表
ListNode<T>* cur = _head->next;
while (cur) {
_head->next = cur->next;
delete cur;
cur = _head->next;
}
delete _head;
_head = nullptr;
}
};
}
带头节点的单项链表最全功能实现以及其中迭代器实现
最新推荐文章于 2024-07-18 21:59:46 发布