每天打卡一小时 第三十三天

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

posted @ 2023-05-22 22:08  财神给你送元宝  阅读(10)  评论(0编辑  收藏  举报