C++ 实现静态链表的简单实例

yizhihongxing

C++ 实现静态链表的简单实例

静态链表是一种用数组模拟链表的数据结构,它可以在不施加缩容操作的情况下随时增长,且可以通过给数组预分配较大的内存空间来节省插入和删除元素时的内存分配操作。下面将介绍如何使用 C++ 实现静态链表,并给出实例说明。

静态链表的实现思路

静态链表由两个数组组成:数据数组和结点数组。结点数组用于描述结点之间的链接关系,数据数组则存储实际的数据。由于静态链表是数组模拟链表,因此需要在结点数组中维护一个指向下一个结点的指针索引,这个指针索引的取值范围是 [0, 数组长度 - 1],其中当指针值为 0 时,表示链表的结尾。下面是静态链表的结点定义:

struct Node {
    int data; // 存储实际数据
    int next; // 存储下一个结点的索引
};

实现过程说明

下面是一个简单的使用 C++ 实现静态链表的过程:

  1. 头文件中定义结点结构体
// list.h
#ifndef LIST_H
#define LIST_H

struct Node {
    int data; // 存储实际数据
    int next; // 存储下一个结点的索引
};

#endif
  1. 实现静态链表类及其方法
// static_list.cpp
#include "list.h"

class StaticList {
public:
    StaticList(int max_size);
    ~StaticList();
    int insert(int index, int value);
    int remove(int index);
    int find(int value);
    void display();

private:
    Node *node_list_; // 结点数组
    int head_; // 链表头结点的索引
    int max_size_; // 结点数组的最大长度
    int size_; // 结点数组的当前长度
};

StaticList::StaticList(int max_size) {
    max_size_ = max_size;
    size_ = 0;
    node_list_ = new Node[max_size_];
    head_ = 0; // 链表初始为空,头结点的 next 指针为 0
    for (int i = 0; i < max_size_; i++) {
        node_list_[i].next = i + 1; // 每个结点的 next 指针指向下一个位置
    }
    node_list_[max_size_ - 1].next = 0; // 最后一个结点的 next 指针指向 0,表示链表结尾
}

StaticList::~StaticList() {
    if (node_list_ != nullptr) {
        delete[] node_list_;
        node_list_ = nullptr;
    }
}

int StaticList::insert(int index, int value) {
    // 链表已满,无法插入新结点
    if (size_ >= max_size_ - 1) {
        return -1;
    }
    // 插入位置不合法
    if (index < 0 || index > size_) {
        return -1;
    }
    int current_index = head_;
    int next_index = node_list_[current_index].next;
    int insert_index = 0;
    // 寻找要插入的结点的前一个结点 current_index 和 next_index
    for (int i = 0; i < index; i++) {
        current_index = next_index;
        next_index = node_list_[current_index].next;
    }
    // 获取一个新的结点插入到 current_index 和 next_index 之间
    insert_index = node_list_[max_size_ - 1].next;
    node_list_[max_size_ - 1].next = node_list_[insert_index].next;
    node_list_[insert_index].data = value;
    node_list_[insert_index].next = next_index;
    node_list_[current_index].next = insert_index;
    size_++;
    return 0;
}

int StaticList::remove(int index) {
    // 插入位置不合法
    if (index < 0 || index >= size_) {
        return -1;
    }
    int current_index = head_;
    int next_index = node_list_[current_index].next;
    // 寻找要删除的结点的前一个结点 current_index 和 next_index
    for (int i = 0; i < index; i++) {
        current_index = next_index;
        next_index = node_list_[current_index].next;
    }
    // 将要删除的结点从链表中去除,并将其放回缓存池中
    node_list_[current_index].next = node_list_[next_index].next;
    node_list_[next_index].next = node_list_[max_size_ - 1].next;
    node_list_[max_size_ - 1].next = next_index;
    size_--;
    return 0;
}

int StaticList::find(int value) {
    int current_index = head_;
    int next_index = node_list_[current_index].next;
    // 遍历整个链表,寻找值为 value 的结点
    while (next_index) {
        if (node_list_[next_index].data == value) {
            return next_index;
        }
        current_index = next_index;
        next_index = node_list_[current_index].next;
    }
    // 遍历完毕,未找到指定值的结点
    return 0;
}

void StaticList::display() {
    int current_index = head_;
    int next_index = node_list_[current_index].next;
    // 遍历整个链表,输出各个结点的值
    while (next_index) {
        std::cout << node_list_[next_index].data << " ";
        current_index = next_index;
        next_index = node_list_[current_index].next;
    }
    std::cout << std::endl;
}

示例说明

下面给出两个使用静态链表的实例:

  1. 求 1~1000000 中所有质数的个数
#include "static_list.h"

bool is_prime(int x) {
    for (int i = 2; i <= sqrt(x); i++) {
        if (x % i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    const int MAX_SIZE = 1000000;
    StaticList list(MAX_SIZE + 1);
    for (int i = 2; i <= MAX_SIZE; i++) {
        list.insert(i - 2, i);
    }
    int count = 0;
    for (int i = 2; i <= MAX_SIZE; i++) {
        int index = list.find(i);
        if (index) {
            count++;
            int current_index = index;
            int next_index = list.node_list_[current_index].next;
            while (next_index) {
                if (node_list_[next_index].data % i == 0) {
                    list.remove(next_index - 2);
                }
                current_index = next_index;
                next_index = list.node_list_[current_index].next;
            }
        }
    }
    std::cout << count << std::endl;
    return 0;
}
  1. N 个人围成一圈,每 M 个人中选出一个,最后剩下的人的编号
#include "static_list.h"

int last_man_standing(int n, int m) {
    StaticList list(n);
    for (int i = 0; i < n; i++) {
        list.insert(i, i + 1);
    }
    int current_index = 0;
    int next_index = list.node_list_[current_index].next;
    while (next_index != current_index) {
        int count = m % list.size_;
        for (int i = 0; i < count - 1; i++) {
            current_index = next_index;
            next_index = list.node_list_[current_index].next;
        }
        list.remove(next_index);
        current_index = next_index;
        next_index = list.node_list_[current_index].next;
    }
    return next_index + 1;
}

int main() {
    std::cout << last_man_standing(10, 3) << std::endl; // 4
    return 0;
}

以上就是 C++ 实现静态链表的简单实例和攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C++ 实现静态链表的简单实例 - Python技术站

(0)
上一篇 2023年6月26日
下一篇 2023年6月26日

相关文章

  • UML中类图的四种关系及其代码实现

    下面是“UML中类图的四种关系及其代码实现的完整攻略”,包括类图的基本介绍、四种关系的介绍、代码实现的步骤和两个示例说明。 类图的基本介绍 类图是UML中最常用的图之一,用于表示系统中的类、接口、关系和其它结构。类图可以帮助开发人员更好地理解系统的结构和设计,从而更好地进行开发和维护。 四种关系的介绍 在类图中,有四种基本的关系,分别是: 泛化关系(Gene…

    other 2023年5月5日
    00
  • checkbox选中触发事件

    checkbox选中触发事件 在Web开发中,checkbox是一种常用的表单元素,它可以让用户选择一个或多个选项。当用户选中或取消选中一个checkbox时,我们可以通过JavaScript来触发相应的事件。 步骤 以下是使用JavaScript来触发checkbox选中事件的步骤: 获取checkbox元素:我们需要获取要触发事件的checkbox元素。…

    other 2023年5月6日
    00
  • qt项目开发实例(含源码)

    以下是详细讲解“Qt项目开发实例(含源码)”的标准Markdown格式文本: Qt项目开发实例(含源码) Qt是一个跨平台的C++应用程序开发框架,可以用于发桌面应用程序、移动应用程序和嵌入式应用程序。本文将介绍Qt项目开发的实例,包括Qt项目创建、Qt项目的编译和Qt项目的运行,同时提供两个示例说明。 1. Qt项目的创建 可以使用Qt Creator创建…

    other 2023年5月9日
    00
  • jenkins 之 iOS 打包及上传至蒲公英

    Jenkins之iOS打包及上传至蒲公英的完整攻略 Jenkins是一款流行的自动化构建工具,可以帮助开发者自动化构建、测试和部署应用程序。本文将为您提供Jenkins之iOS打包及上传至蒲公英的完整攻略,包括Jenkins的安装、配置、iOS打包及上传至蒲公英等内容。 安装Jenkins 首先,我们需要安装Jenkins。可以按照以下步骤进行安装: 下载J…

    other 2023年5月6日
    00
  • HTML仿命令行界面具体实现

    HTML仿命令行界面可以使用HTML、CSS和JavaScript实现,下面我将分步骤介绍具体实现方法。 1. HTML布局 首先,我们需要准备一个HTML文件,其中需要定义一个输入框和一个显示框,可以使用一个div元素来充当整个界面,如下所示: <div class="terminal"> <div class=&qu…

    other 2023年6月26日
    00
  • C语言中字符串的存储方法

    在C语言中,字符串被视为是一串字符数组。字符串的存储方法有两种,分别是“字符数组存储”和“指针存储”。 一、字符数组存储 在C语言中,字符串可以用字符数组存储,字符数组中的最后一个元素一定是字符‘\0’。 例如: char str[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; printf("%s", str)…

    other 2023年6月20日
    00
  • 侠客风云传妹子男主结局是什么 侠客风云传全结局图文介绍

    侠客风云传妹子男主结局攻略 《侠客风云传》是一款受欢迎的角色扮演游戏,玩家在游戏中扮演男主角,与各种妹子展开互动,并最终决定与哪位妹子结局。以下是关于妹子男主结局的详细攻略。 1. 收集好感度 在游戏中,与每个妹子互动可以提高她们对男主角的好感度。好感度是影响结局的重要因素,因此玩家需要与妹子进行对话、完成任务、赠送礼物等方式来提高好感度。每个妹子都有不同的…

    other 2023年7月28日
    00
  • 华为v9怎么提速? 华为v9开发者模式的设置教程

    华为v9是一款优秀的智能手机,但是有时候会出现卡顿、慢等问题。如何提速呢?接下来我将为大家详细讲解华为v9的提速方法以及如何设置开发者模式。 华为v9的提速方法 关闭后台应用 后台应用是一个非常大的资源消耗器,关闭后台不使用的应用可以有效地提升手机的速度。方法如下: 1.进入手机的“设置”界面。 2.选择“应用管理”选项。 3.选择需要关闭的应用程序。 4.…

    other 2023年6月26日
    00
合作推广
合作推广
分享本页
返回顶部