#include <iostream>
using namespace std;
template<class T>
struct Node
{
public:
Node() { }
Node(T t, Node* next);
public:
T data;
Node* next;
};
template<class T>
class List
{
public:
List();
~List();
List<T>& operator=(const List& list);
List(const List& list);
int size();
bool is_empty();
T get(int index);
T get_first();
T get_last();
void insert(int index, T t);
void insert_first(T t);
void insert_last(T t);
void del(int index);
void delect_first();
void delect_last();
private:
Node<T>* get_node(int index);
private:
Node<T>* head;
int count;
};
template<class T>
Node<T>::Node(T t, Node* next)
{
this->data = t;
this->next = next;
}
template<class T>
List<T>::List() : count(0)
{
head = new Node<T>();
head->next = NULL;
}
template<class T>
List<T>::~List()
{
Node<T>* ptmp;
Node<T>* phead = head->next;
if (phead)
{
while (phead != head)
{
ptmp = phead;
phead = phead->next;
delete ptmp;
}
}
delete head;
head = NULL;
}
template<class T>
List<T>& List<T>::operator=(const List& list)
{
if (this != &list)
{
head = new Node<T>();
head->next = NULL;
Node<T>* phead = head;
Node<T>* ptmp = list.head->next;
if (ptmp != NULL)
{
do
{
Node<T>* pnode = new Node<T>(ptmp->data, NULL);
phead->next = pnode;
phead = pnode;
ptmp = ptmp->next;
} while (ptmp != list.head);
phead->next = head;
}
}
return *this;
}
template<class T>
List<T>::List(const List& list)
{
head = new Node<T>();
head->next = NULL;
if (list.head->next == NULL)
{
return;
}
Node<T>* ptmp = list.head->next;
do
{
head->next = ptmp;
ptmp = ptmp->next;
} while (ptmp != list.head);
}
template<class T>
int List<T>::size()
{
return count;
}
template<class T>
bool List<T>::is_empty()
{
return count == 0;
}
template<class T>
Node<T>* List<T>::get_node(int index)
{
if (index < 0 || index >= count)
{
cout << "get node failed! the index in out of bound!" << endl;
return NULL;
}
int i = 0;
Node<T>* pindex = head->next;
while (i++ < index)
{
pindex = pindex->next;
}
return pindex;
}
template<class T>
T List<T>::get(int index)
{
return get_node(index)->data;
}
template<class T>
T List<T>::get_first()
{
return get_node(0)->data;
}
template<class T>
T List<T>::get_last()
{
return get_node(count - 1)->data;
}
template<class T>
void List<T>::insert(int index, T t)
{
if (index == 0)
{
insert_first(t);
return;
}
Node<T>* pindex = get_node(index - 1);
if (pindex != NULL)
{
Node<T>* pnode = new Node<T>(t, pindex->next);
pindex->next = pnode;
count++;
}
}
template<class T>
void List<T>::insert_first(T t)
{
Node<T>* pnode = new Node<T>(t, NULL);
Node<T>* phead = head->next;
if (phead == NULL)
{
head->next = pnode;
pnode->next = head;
}
else
{
pnode->next = phead;
head->next = pnode;
}
count++;
}
template<class T>
void List<T>::insert_last(T t)
{
Node<T>* pnode = new Node<T>(t, head);
Node<T>* phead = head->next;
if (phead == NULL)
{
head->next = pnode;
}
else
{
Node<T>* ptmp = NULL;
while (phead != head)
{
ptmp = phead;
phead = phead->next;
}
ptmp->next = pnode;
}
count++;
}
template<class T>
void List<T>::del(int index)
{
if (index == 0)
{
head->next = head->next->next;
}
else
{
Node<T>* pindex = get_node(index);
Node<T>* ptmp = get_node(index - 1);
ptmp->next = pindex->next;
delete pindex;
}
count--;
}
template<class T>
void List<T>::delect_first()
{
del(0);
}
template<class T>
void List<T>::delect_last()
{
del(count - 1);
}
class Person
{
public:
Person() { }
Person(string name, int age)
{
this->name = name;
this->age = age;
}
public:
string name;
int age;
};
void person_test()
{
Person p1("张三", 19);
Person p2("李四", 14);
Person p3("王五", 15);
Person p4("赵六", 16);
Person p5("机器", 16);
List<Person>* list = new List<Person>();
list->insert_last(p1);
list->insert_last(p2);
list->insert_last(p3);
list->insert_last(p4);
list->insert_first(p5);
for (int i = 0; i < list->size(); ++i)
{
cout << "姓名:" << list->get(i).name << " 年龄:" << list->get(i).age << endl;
}
cout << endl;
}
int main()
{
person_test();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具