首页 / 客观题库

Q80538 - 线性表15

基础数据结构

题目( 单选题 )

区块链技术是比特币的基础。在区块链中,每个区块指向前一个区块,构成链式列表,新区块只能接在链尾,不允许在中间插入或删除。下面代码实现插入区块添加函数,则横线处填写( )。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

//区块(节点)

struct Block {

    int index; // 区块编号(高度)

    string data; // 区块里保存的数据

    Block* prev; // 指向前一个区块

    Block(int idx, const string& d, Block* p) : index(idx), data(d), prev(p) {}

};

// 区块链

struct Blockchain {

    Block* tail;

    // 初始化

    void init() {

       tail = new Block(0, "Genesis Block", nullptr);

    }

    // 插入新区块

    void addBlock(const string& data) {

       _______________________ //在此处填入代码

    }

    // 释放内存

    void clear() {

       Block* cur = tail;

       while (cur != nullptr) {

           Block* p = cur->prev;

           delete cur;

           cur = p;

        }

       tail = nullptr;

    }

};

A

Block* newBlock = new Block(tail->index + 1, data, tail);

tail = newBlock->prev;

B

Block* newBlock = new Block(tail->index + 1, data, tail);

tail = newBlock;

C

Block* newBlock = new Block(tail->index + 1, data, tail->prev);

tail = newBlock;

D

Block* newBlock = new Block(tail->index + 1, data, tail->prev);

tail = newBlock->prev;

意见反馈

    最多上传3张图片,格式为JPG、PNG、JPEG,单张不超过5MB

    注册

    发送验证码

    密码必须包含数字、字母和特殊字符

    找回密码

    发送验证码

    密码必须包含数字、字母和特殊字符

    运行 ID:67149

    • 测试点1:Accepted
    • 用时:0 ms
    • 内存:288 kb
    • 测试点2:Accepted
    • 用时:0 ms
    • 内存:288 kb
    输入
    203
    输出
    203

    test

    测评信息

    错误.in文件下载

    错误.out文件下载

    运行 ID:67149

    2019-01-24 15:06:36