这是一个单向链表,每次在链表的头部加入新节点。并且加入了迭代器。
#ifndef _MYLIST_H
#define _MYLIST_H
template<class T> class Node;//Node,Mylist两个类相互依赖,需要做一个声明
template<class T> class MyList_Iterator;
/****************************************************************************/
template<class T>
class MyList
{
public:
void Add(const T& item);//向列表头部加入新元素
void Delete(const T& item);//删除第一个与item相等的元素
void Invert();//反转列表
void Conect(MyList<T>& OldList);//连接
bool IsEmpty();
Node<T>* Begin();
Node<T>* End();
MyList() : P_first(nullptr) {}
~MyList(){ }
void Show();//测试用,打印出所有的元素
private:
Node<T>* P_first;//指向列表的第一个元素
};
template<class T>
void MyList<T>::Add(const T& item)
{
Node<T>* P_new_first = new Node<T>(item);
P_new_first->P_next = P_first;
P_first = P_new_first;
}
template<class T>
void MyList<T>::Delete(const T& item)
{
Node<T>* P_previous, * P_current;
if (P_first->data == item)//判断需要删除的节点是否位于链首
{
P_current = (P_first->P_next);
P_first->~Node();
P_first = P_current;
}
else
{
P_previous = P_first;
P_current = P_first->P_next;
while (P_current->data != item && P_current->P_next != nullptr)
{ //找到第一个相同元素,或者遍历到最后一个节点
Node<T>* temp = P_current;
P_current = P_current->P_next;
P_previous = temp;
}
if (P_current->data == item)//相同元素
{
P_previous->P_next = P_current->P_next;
P_current->~Node();//析构掉删除的节点
}
}
}
template<class T>
void MyList<T>::Invert()
{
Node<T>* P_previous, * P_current, * P_temp;
P_previous = P_first;
P_current = P_first->P_next;
P_first->P_next = nullptr;
while (P_current->P_next != nullptr)
{
P_temp = P_current->P_next;
P_current->P_next = P_previous;
P_previous = P_current;
P_current = P_temp;
}
P_current->P_next = P_previous;
P_first = P_current;
}
template<class T>
void MyList<T>::Conect(MyList<T>& OldList)
{
if (OldList.P_first == nullptr)
{
return;
}
else if (this->P_first == nullptr)
{
this->P_first = OldList.P_first;
OldList.~MyList();
}
else
{
Node<T>* P_current = this->P_first;
while (P_current->P_next != nullptr)
{
P_current = P_current->P_next;
}
P_current->P_next = OldList.P_first;
OldList.~MyList();
}
}
template<class T>
void MyList<T>::Show()
{
for (Node<T>* it = P_first; it != nullptr; it = it->P_next)
{
std::cout << it->data << " -> ";
}
}
template<class T>
bool MyList<T>::IsEmpty()
{
}
template<class T>
Node<T>* MyList<T>::Begin()
{
return this->P_first;
}
template<class T>
Node<T>* MyList<T>::End()
{
Node<T>* P_current = this->P_first;
while (P_current->P_next != nullptr)
{
P_current = P_current->P_next;
}
return P_current;
}
/****************************************************************************/
template<class T>
class Node
{
public:
Node(const T& item):data(item), P_next(nullptr){ }
Node(){ }
~Node(){ }
friend class MyList<T>;
friend class MyList_Iterator<T>;
private:
T data;
Node<T>* P_next;
};
/****************************************************************************/
template<class T>
class MyList_Iterator
{
public:
MyList_Iterator( Node<T>* item) : P_node(item){ }
MyList_Iterator() : P_node(nullptr) { }
T& operator* ();//重载取地址
Node<T>* operator= (Node<T>* item);
MyList_Iterator<T> operator++ ();//自加一,由于是单向列表,无法实现自减一
private:
Node<T>* P_node;
};
template<class T>
T& MyList_Iterator<T>::operator* ()
{
return P_node->data;
}
template<class T>
Node<T>* MyList_Iterator<T>::operator= (Node<T>* item)
{
P_node = item;
return item;
}
template<class T>
MyList_Iterator<T> MyList_Iterator<T>::operator++ ()
{
this->P_node = (this->P_node)->P_next;
return MyList_Iterator<T>(this->P_node);
}
/****************************************************************************/
#endif
#include <iostream>
#include "Mylist.h"
int main()
{
MyList<int> a, b;
for (int i = 0; i < 10; ++i)
{
a.Add(i);
}
for (int i = 10; i < 20; ++i)
{
b.Add(i);
}
a.Invert();
b.Invert();
a.Conect(b);
a.Show();
std::cout << "Hello World!\n";
MyList_Iterator<int> it = a.Begin();
std::cout << *it << std::endl;
++it;//it++,这种写法错误
std::cout << *(++it) << std::endl;
MyList_Iterator<int> it_1 = a.End();
std::cout << *it_1 << std::endl;
return 0;
}
结果:
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15 -> 16 -> 17 -> 18 -> 19 -> Hello World!
0
2
19