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

rk611218 单选题 共 个子题
//区块(节点)
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;
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧