C++链表,尽量使用C++11

原创,转载请注明出处。

为了熟悉vs环境、c++中的类写的链表

chainNode.h:

#pragma once
#include<memory>
#ifndef CHAINNODE_H
#define CHAINNODE_H
using std::make_shared;
using std::shared_ptr;

template <typename T>
struct chainNode
{
    T element;
    shared_ptr<chainNode> next;

    chainNode(const T &theElement)
    {
        element = theElement;
        next = nullptr;
    }

    chainNode(const T &theElement, const shared_ptr<chainNode<T>> theNext)
    {
        element = theElement;
        next = theNext;
    }
};
#endif // !CHAINNODE_H

chain.h:

#pragma once

#include"chainNode.h"
#include<memory>
#ifndef CHAIN_H
#define CHAIN_H
using std::shared_ptr;
using std::make_shared;

template<typename T>
class chain//下标从1开始
{
public:
    chain() {};
    chain(const T elementList[], const int leng);
    void insert(int pos, const T &theElement);
    void erase(const T &theElement);
    void output()const;
private:
    shared_ptr<chainNode<T>> firstNode; 
    int chainSize;
};


template<typename T>
chain<T>::chain(const T elementList[],const int leng)//链表长度至少为2
{
    firstNode = make_shared<chainNode<T>>(elementList[0]);
    chainSize = 1;
    auto pos(firstNode);
    for(int i = 1; i < leng; i++)
    {
        pos->next = make_shared<chainNode<T>>(elementList[i]);
        pos = pos->next;
        chainSize++;
    }
}

template<typename T>
void chain<T>::insert(int situ, const T &theElement)
{
    auto targetNode = make_shared<chainNode<T>>(theElement);
    if (situ == 1)
    {
        targetNode->next = firstNode;
        firstNode = targetNode;
        chainSize++;
        return;
    }
    situ-=2;
    auto fron(firstNode);
    while (situ && fron != nullptr)
    {
        fron = fron->next;
        situ--;
    }
    auto tail(fron->next);
    fron->next = targetNode;
    targetNode->next = tail;
    chainSize++;
    return;
}

template<typename T>
void chain<T>::erase(const T &theElement)
{
    if (firstNode->element == theElement)
    {
        firstNode = firstNode->next;
        chainSize--;
        return;
    }
    auto fron(firstNode);
    auto pos(fron->next);
    while (pos!=nullptr)
    {
        if (pos->element == theElement)
        {
            fron->next = pos->next;
            break;
        }
        fron = fron->next;
        pos = fron->next;
    }
    chainSize--;
}

template<typename T>
void chain<T>::output()const
{
    auto pos(firstNode);
    while (pos != nullptr)
    {
        cout << pos->element << ' ';
        pos = pos->next;
    }
    cout << endl;
}

#endif // !CHAIN_H

chain.cpp:

// chain2.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include"chain.h"
using std::cout;
using std::endl;



int main()
{
    double list[5] = { 2,4,6,8,10 };
    chain<double> chainList(list,5);//初始化
    chainList.output();
    chainList.insert(1, 1);
    chainList.insert(3, 3);
    chainList.output();
    chainList.erase(3);
    chainList.output();
    chainList.erase(1);
    chainList.output();

    getchar();
    return 0;
}

运行结果:

C++链表,尽量使用C++11

关键点

1.不要忘记考虑首尾特殊情况

2.区分清楚指针和指针所指对象

不足:

没有重载输出


原文链接: https://www.cnblogs.com/mu-ye/p/7634418.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/260918

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午1:59
下一篇 2023年2月14日 下午2:00

相关推荐