首页 > 其他分享 >pta_【CPP0038】单向链表模板类

pta_【CPP0038】单向链表模板类

时间:2023-05-23 22:12:00浏览次数:47  
标签:Node CPP0038 cout currNode headNode pta 链表 getNext position

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

 

 

标签:Node,CPP0038,cout,currNode,headNode,pta,链表,getNext,position
From: https://www.cnblogs.com/atrue/p/17426543.html

相关文章

  • 每日打卡,pta题目
    给定一个长度不超过 104 的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按PATest的顺序打印,直到所有字符都被输出。输入格式:输入在一......
  • 链表应用 III
    目录链表应用应用1:Leetocde.21题目分析代码实现方法一:迭代实现方法一:递归实现应用2:Leetocde.23题目分析方法一:分治方法二:优先级队列代码实现方法一:分治方法二:优先级队列应用3:Leetocde.141题目分析代码实现应用4:Leetocde.142题目分析代码实现应用5:Leetocde.876题目分析代码实现应用......
  • 图解LeetCode——剑指 Offer 36. 二叉搜索树与双向链表
    一、题目输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。二、示例为了让您更好地理解问题,以下面的二叉搜索树为例:我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对......
  • 复杂链表的复制
       ......
  • 周一(PTA简单题)例
    #include<iostream>#include<iomanip>usingnamespacestd;intmain(){intN,i,sex,b=0,g=0;floatscore,aver1,aver2,aver3,b1=0.0,g1=0.0;cin>>N;for(i=0;i<N;i++){cin>>sex;cin>>score;if(sex==1){b+=1;b1+=s......
  • BLOG-1 (PTA 1~3)
    前言在本阶段的三次题目集中,我们主要涉及了菜单计价程序的设计与实现。这些题目涵盖了点菜订单的处理、菜品价格计算、代点菜功能以及总价的计算等方面的内容。通过完成这些题目,我们可以加深对Java编程语言的理解,熟悉面向对象的设计思想,并应用这些知识解决实际问题。设计与分析......
  • 5-21打卡:双循环链表(无哨兵)练习
    #include<iostream>usingnamespacestd;typedefstructNode{intdata;Node*next;Node*pre;}Node;Node*initlist(intdata){Node*node=newNode;node->data=data;node->next=node;node->pre=node;......
  • 图解LeetCode——19. 删除链表的倒数第 N 个结点
    一、题目给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。二、示例2.1>示例1:【输入】head=[1,2,3,4,5],n=2【输出】[1,2,3,5]2.2>示例2:【输入】head=[1],n=1【输出】[]2.3>示例3:【输入】head=[1,2],n=1【输出】[1]提示:链......
  • #yyds干货盘点# LeetCode程序员面试金典:有序链表转换二叉搜索树
    题目:给定一个单链表的头节点 head ,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差不超过1。 示例1:输入:head=[-10,-3,0,5,9]输出:[0,-3,9,-10,null,5]解释:一个可能的答案是[0,-3,9,-1......
  • 每天打卡一小时 第三十一天 PTA520钻石 争霸赛
    第一题  源代码#include<iostream>usingnamespacestd;intmain(){intn;cin>>n;cout<<"520"<<n<<"Times!";}第一题,简简单单打印输出第二题 源代码#include<iostream>usingnamespacestd;intmain(......