C++ 单链表
Posted Python可视化编程机器学习OpenCV
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 单链表相关的知识,希望对你有一定的参考价值。
C++ 实现单链表(类似python的list类型)。
涉及到的基础知识点有:
结构体(指针做结构元素)
类 (构造函数、拷贝构造函数)
指针和引用
链表的相关概念
目前我实现的功能有:链表的打印,尾部添加数据,中间任意位置插入数据,删除指定位置的数据 和 查找数据。源码如下:
using namespace std;
struct Node //结构体做节点
{
//每个节点包含一个数据(可以改成复合类型的数据)和指向下一个节点的指针
float v;
struct Node *p;
};
class List
{
private:
struct Node *headP; //指向头节点的指针
struct Node *tailP; //指向尾巴节点的指针
public:
int length;
List()
{
cout<< "List无参构造函数调用"<< endl;
headP = NULL;
tailP = NULL;
length = 0;
};
List(List& source)
{
cout<< "List 拷贝函数调用"<<endl;
if(source.headP==NULL)
{
this->headP = NULL;
this->tailP = NULL;
this->length = 0;
return;
}
//创建新节点指针数组
struct Node *NP[source.length];
for(int i =0; i < source.length; i++)
{
NP[i] = new struct Node;
}
this->headP = NP[0];
struct Node sourceNode = *(source.headP);
int i;
for(i =0; i< source.length-1; i++)
{
(*NP[i]).v = sourceNode.v;
(*NP[i]).p = NP[i+1];
sourceNode = *(sourceNode.p);
}
(*NP[i]).v = sourceNode.v;
(*NP[i]).p = NULL;
tailP = NP[i];
this->length = source.length;
}
//~List()
//{
// cout<< "List析构函数调用"<< endl;
//};
List & append(float value)//添加元素
{
//创建新节点指针
struct Node *newP = new struct Node;
*newP ={value, NULL};
if(this->length == 0)
{
headP = newP;
}
else
{
(*tailP).p = newP;
}
tailP = newP;
this->length ++;
return *this;
}
List & insert(int index, float value)//插入元素
{
if(index > this->length)
{
cout <<"Error, the index is out of range, ignored!" << endl;
return *this;
}
if(index == this->length)
{
this->append(value);
return *this;
}
//创建新节点指针
struct Node *newP = new struct Node;
*newP ={value, NULL};
struct Node * nodeP = this->headP;
if(index == 0)
{
(*newP).p = this->headP;
this->headP = newP;
this->length ++;
return *this;
}
for(int i =0; i<index-1; i++)
{
nodeP = (*nodeP).p;
}
(*newP).p = (*nodeP).p;
(*nodeP).p = newP;
this->length ++;
return *this;
}
float pop(int index)//弹出指定位置元素
{
if(index >= this->length || index <0 )
{
cout <<"Error, the index is out of range, ignored! ";
return -1.0f;
}
struct Node * nodeP = this->headP;
struct Node * pop_nodeP, *next_nodeP;
if(index == 0)
{
float value = (*nodeP).v;
nodeP = (*nodeP).p;
this->headP = nodeP;
this->length --;
return value;
}
for(int i =0; i<index-1; i++)
{
nodeP = (*nodeP).p;
}
pop_nodeP = (*nodeP).p;
next_nodeP = (*pop_nodeP).p;
(*nodeP).p = next_nodeP;
this->length --;
//cout<<this->tailP->v<<endl;
return (*pop_nodeP).v;
}
int index(float value, float precision = 1.0e-10)//查找元素
{
//查找给定值第一次出现的位置
//-1 代表找不到
if(this->headP == NULL)
{
cout <<"NULL"<<endl;
return -1;
}
struct Node node = *(this->headP);
int i = 0;
while(node.p)
{
float delta = node.v -value;
if(delta >= -precision and delta <= precision)//因浮点数精度问题,这里改成范围
{
return i;
}
node = *(node.p);
i++;
}
float delta = node.v -value;
if(delta >= -precision and delta <= precision)//因浮点数精度问题,这里改成范围
{
return i;
}
return -1;
}
void print()//完全打印链表结果(含指针和数据)
{
if(this->headP == NULL)
{
cout <<"NULL"<<endl;
return;
}
struct Node node = *(this->headP);
cout<<this->headP<<"(head)-->";
while(node.p)
{
cout<<node.v<<","<<node.p<<"-->";
node = *(node.p);
}
cout<<node.v<<", "<<node.p<<"--|"<<endl;//尾巴节点
//cout<<(*(this->tailP)).v<<";"<<(*(this->tailP)).p<<endl;
}
void print_PyStyle()//python风格打印链表结果(仅含数据)
{
if(this->headP == NULL)
{
cout <<"[]"<<endl;
return;
}
struct Node node = *(this->headP);
cout<<"[";
while(node.p)
{
cout<<node.v<<",";
node = *(node.p);
}
cout<<node.v<<"]"<<endl;//尾巴节点
}
};
int main()
{
//测试
List l1 ;
l1.append(1.0f).append(2.0f).append(3.0f).append(99.f);
//l1.print();
l1.print_PyStyle();
cout<< l1.length<<endl;
List l2 = l1;
l2.print();
l2.print_PyStyle();
cout<< l2.length<<endl;
l2.insert(2,88);
l2.insert(1,7);
cout<< l2.length<<endl;
l2.insert(7,66);
l2.print();
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(2)<<endl;
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(0)<<endl;
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(3)<<endl;
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(2)<<endl;
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(1)<<endl;
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(0)<<endl;
l2.print_PyStyle();
cout<<"pop:"<<l2.pop(0)<<endl;
l2.print_PyStyle();
cout<<"l2 length: "<<l2.length<<endl;
cout<<"l1 length: "<<l1.length<<endl;;
l2.insert(0,555.0f);
l2.append(1).append(2).append(3);
l2.print_PyStyle();
cout<<"index= "<<l2.index(3.0)<<endl;
cout<<"index= "<<l2.index(2.8)<<endl;
return 0;
}
以上是关于C++ 单链表的主要内容,如果未能解决你的问题,请参考以下文章