pta_【CPP0038】单向链表模板类

发布时间 2023-05-23 22:08:05作者: a_true

#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;
}