C++面向对象技术实现带头结点的单链表

文件结构 :

        

文件名字用途
CmakeList.txtcmake文件
how.md简述思路以及其他说明
main.cpp主测试程序
SingLinkedList.cpp核心实现文件
SingLinkedList.h核心头文件

 SingleLinkedList.cpp

//
// Created by A Luck Boy on 2023/1/13.
//
#include "SingleLinkedList.h"

LinkedList::LinkedList() {
    head = new Node(0);
    length = 0;
    cout << "创建成功!"<<endl;
}

LinkedList:: ~LinkedList(){
    Node *curr = head;
    while (curr != nullptr) {
        Node *next = curr->next;
        delete curr;
        curr = next;
    }
    cout <<"已经销毁"<<endl;
}

void LinkedList::append(int data)  {
    Node* newNode = new Node(data);
    Node* curr = head;
    while (curr->next != nullptr) {
        curr = curr->next;
    }
    curr->next = newNode;
    length++;
}

bool LinkedList::isEmpty()const {
    return length == 0;
}

int LinkedList::getLength() const{
    return length;
}

bool LinkedList::insert(int data, int location) {
    // attention : location <= len-1 and location should be no less than 0
    if (location < 0 || location > length-1) {
        cout << "插入索引不在范围,索引应该不小于0并且不大于" << length-1 << endl;
        return false;
    }
    Node * newNode = new Node(data);
    Node * curr = head;
    for (int i = 0; i < location; i++) {
        curr = curr->next;
    }
    newNode->next = curr->next;
    curr->next = newNode;
    length++;
    return true;
};

bool LinkedList::remove(int location) {
    // be similar to the insert method
    if (location < 0 ||location > length-1) {
        cout << "删除索引数据不在范围,索引应该不小于0并且不大于" << length-1 << endl;
        return false;
    }
    Node* curr = head;
    for (int i = 0; i <location; i++) {
        curr = curr->next;
    }
    Node* toDelete = curr->next;
    curr->next = toDelete->next;
    delete toDelete;
    length--;
    return true;
}

Node * LinkedList::find(int location) const{
    if (location < 0 || location > length-1) {
        cout << "删除索引数据不在范围,索引应该不小于0并且不大于" << length-1 << endl;
        return nullptr;
    }
    Node* curr = head->next;
    for (int i = 0; i < location; i++) {
        curr = curr->next;
    }
    return curr;
}

 SingleLinkedList.h

//
// Created by A Luck Boy on 2023/1/13.
//
// 结点类
#include <iostream>
using namespace std;
#define Waring cout << "这里代码有问题!" << endl;

class Node {
public:
    int data;
    Node* next;
    Node(int data) {
        this->data = data;
        this->next = nullptr;
    }
};

// 链表
class LinkedList {
public:
    Node *head;
    int length;

    // 创建
    LinkedList();

    // 销毁
    ~LinkedList();

    // 尾部添加
    void append(int data);

    // 判空
    bool isEmpty() const;

    // 表长
    int getLength() const;

    // 插入
    bool insert(int data, int location);

    // 删除
    bool remove(int location) ;

    // 查找
    Node* find(int location) const;

};

main.cpp

//
// Created by A Luck Boy on 2023/1/13.
//
#include "SingleLinkedList.h"

int main(){
    LinkedList list;
    // 判空试试
    if (list.isEmpty()){cout << "空的" << endl;}
    else {Waring}
    // 尾部添加数据
    for (int i=0;i<10;i++){
        list.append((1+i)*(i+2));
    }
    // 添加了十次,那么长度肯定是十,来确认一下
    cout << "Length is " << list.getLength()    <<endl;
    // 遍历试试
    Node *head = list.head;
    for (int i=0;i<list.getLength();i++){
        head = head->next;
        cout << "Index " << i << " is number " << head->data <<endl;
    }
    delete head;
    // 插入操作 : 我会操作三次,分别是 : 前两次检测溢出索引时插入的情况和索引范围内插入,当前最大索引是9
    // 另外,position参数是正常的顺序,即 索引+1 的数字
    if (list.insert(1000, -1) || list.insert(1000, 10))
    {
        Waring
    } else{
        cout << "检测出索引不在范围,程序正确!" << endl;
    }
    if (list.insert(1000, 5)){
        cout << "已经在索引5插入新数据,长度变成了11,下面直接打印所有证明\n";
        // 遍历试试
        Node *another_head = list.head;
        for (int i=0;i<list.getLength();i++){
            another_head = another_head->next;
            cout << "Index " << i << " is number " << another_head->data <<endl;
        }
        delete another_head;
    } else{
        Waring
    }
    // 操作三次,思路同上
    if (list.remove(-1) || list.remove(list.getLength())){
        Waring
    } else{
        cout << "检测出索引不在范围,程序正确!" << endl;
    }
    // 把索引四的30删掉,然后验证
    if (list.remove(4))
    {
        cout << "验证正确性 " <<endl;
        Node *another_head2 = list.head;
        for (int i=0;i<list.getLength();i++){
            another_head2 = another_head2->next;
            cout << "Index " << i << " is number " << another_head2->data <<endl;
        }
        delete another_head2;
    } else{Waring}
    // 查找操作,操作三次
    // 注意,我这里是查找的结点,函数返回值固然是一个结点类型
    // 前两次仍然测试溢出检查
    Node *check_mistake_Node = list.find(-1);
    Node *check_mistake_Node_2 = list.find(100);
    if ((check_mistake_Node == nullptr) or (check_mistake_Node_2 == nullptr)){
        cout << "验证了程序正确!"<<endl;
    } else{Waring}
    delete check_mistake_Node, check_mistake_Node_2;
    // 第三次,试图找索引5上面的结点,数据是42
    Node * myFindNode  = list.find(5);
    cout << "Index 5 number is " << myFindNode->data << endl;
    return 0;
}

终端运行结果

创建成功!
空的
Length is 10
Index 0 is number 2
Index 1 is number 6
Index 2 is number 12
Index 3 is number 20
Index 4 is number 30
Index 5 is number 42
Index 6 is number 56
Index 7 is number 72
Index 8 is number 90
Index 9 is number 110
插入索引不在范围,索引应该不小于0并且不大于9
插入索引不在范围,索引应该不小于0并且不大于9
检测出索引不在范围,程序正确!
已经在索引5插入新数据,长度变成了11,下面直接打印所有证明
Index 0 is number 2
Index 1 is number 6
Index 2 is number 12
Index 3 is number 20
Index 4 is number 30
Index 5 is number 1000
Index 6 is number 42
Index 7 is number 56
Index 8 is number 72
Index 9 is number 90
Index 10 is number 110
删除索引数据不在范围,索引应该不小于0并且不大于10
删除索引数据不在范围,索引应该不小于0并且不大于10
检测出索引不在范围,程序正确!
验证正确性 
Index 0 is number 2
Index 1 is number 6
Index 2 is number 12
Index 3 is number 20
Index 4 is number 1000
Index 5 is number 42
Index 6 is number 56
Index 7 is number 72
Index 8 is number 90
Index 9 is number 110
删除索引数据不在范围,索引应该不小于0并且不大于9
删除索引数据不在范围,索引应该不小于0并且不大于9
验证了程序正确!
Index 5 number is 42
已经销毁

源代码链接 : 

自己实现的数据结构与算法合集: 自己实现的数据结构与算法合集,使用C或者C++ - Gitee.com

无聊的备注 : 

此刻是2023.1.13,夜里22:32,夜大雨 ,天气预报写着6°,还有,王菲的《归途有风》很好听,循环播放着……

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PythonNotJava

若您有别的建议,请在评论区留言

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值