前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 单链表

C++ 单链表

作者头像
用户6021899
发布2021-05-20 10:31:22
8100
发布2021-05-20 10:31:22
举报

C++ 实现单链表(类似python的list类型)。

涉及到的基础知识点有:

  • 结构体(指针做结构元素)
  • 类 (构造函数、拷贝构造函数)
  • 指针和引用
  • 链表的相关概念

目前我实现的功能有:链表的打印,尾部添加数据,中间任意位置插入数据,删除指定位置的数据 和 查找数据。源码如下:

代码语言:javascript
复制
#include <iostream>
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;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-05-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档