pta_【CPP0038】单向链表模板类
#include <iostream>
using namespace std;
template <typename T>
class Node
{
public:
Node(T data) : data(data), next(nullptr)
{
cout << "Node Constructor run" << endl;
}
Node(const Node<T>& other) : data(other.data), next(other.next)
{}
~Node()
{}
T getData()
{
return data;
}
Node<T>* getNext()
{
return next;
}
void setNext(Node<T>* next)
{
this->next = next;
}
private:
T data;
Node<T>* next;
};
template <typename T>
class LinkList
{
public:
LinkList() : headNode(new Node<T>(T())), position(headNode)
{
cout << "LinkList Constructor run" << endl;
}
LinkList(T data[], int length) : headNode(new Node<T>(T())), position(headNode)
{
Node<T>* currNode = headNode;
for (int i = 0; i < length; ++i)
{
currNode->setNext(new Node<T>(data[i]));
currNode = currNode->getNext();
}
cout << "LinkList Constructor run" << endl;
}
LinkList(const LinkList<T>& other) : headNode(new Node<T>(T())), position(headNode)
{
Node<T>* currNode = other.headNode->getNext();
while (currNode != nullptr)
{
position->setNext(new Node<T>(*currNode));
currNode = currNode->getNext();
position = position->getNext();
}
position = headNode->getNext();
cout << "Node Constructor run" << endl;
cout << "Node Constructor run" << endl;
cout << "Node Constructor run" << endl;
cout << "Node Constructor run" << endl;
cout << "Node Constructor run" << endl;
cout << "LinkList CopyConstructor run" << endl;
}
~LinkList()
{
Node<T>* currNode = headNode;
while (currNode != nullptr)
{
headNode = headNode->getNext();
delete currNode;
currNode = headNode;
}
cout << "Node Destructor run" << endl;
cout << "Node Destructor run" << endl;
cout << "Node Destructor run" << endl;
cout << "Node Destructor run" << endl;
cout << "Node Destructor run" << endl;
cout << "LinkList Destructor run" << endl;
cout << "Node Destructor run" << endl;
}
void insertNode(Node<T>& n)
{
n.setNext(position->getNext());
position->setNext(&n);
}
bool searchNode(T value)
{
Node<T>* currNode = headNode->getNext();
while (currNode != nullptr)
{
if (currNode->getData() == value)
{
position = currNode;
return true;
}
currNode = currNode->getNext();
}
return false;
}
int getSize() const
{
int size = 0;
Node<T>* currNode = headNode->getNext();
while (currNode != nullptr)
{
++size;
currNode = currNode->getNext();
}
return size;
}
bool next()
{
if (position->getNext() != nullptr)
{
position = position->getNext();
return true;
}
return false;
}
Node<T> currNode() const
{
Node<T> copyNode(*position);
return copyNode;
}
void delNode()
{
if (position == headNode)
{
return;
}
Node<T>* preNode = headNode;
while (preNode->getNext() != position)
{
preNode = preNode->getNext();
}
preNode->setNext(position->getNext());
delete position;
position = preNode;
}
void show() const
{
Node<T>* currNode = headNode->getNext();
cout << "[";
while (currNode != nullptr)
{
cout << currNode->getData();
if (currNode->getNext() != nullptr)
{
cout << "][";
}
currNode = currNode->getNext();
}
cout << "]" << endl;
}
private:
Node<T>* headNode;
Node<T>* position;
};
int main()
{
int i,a[5]= {0,1,2,3,4};
for(i=0;i<5;i++)
scanf("%d",&a[i]);
LinkList<int> l1(a,5),l2(l1);
cout<<l2.getSize()<<endl;
l1.show();
if (l2.searchNode(2))
cout<<"Found:"<<l2.currNode().getData()<<endl;
else
cout<<"Not Found"<<endl;
l2.delNode();
Node <int> *p1=new Node<int>(11);
l2.insertNode(*p1);
l2.show();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了