被头歌上的一些问题折磨了,这里提供一些头歌题目的解决方案,顺带挣一点辛苦费

本文提供了头歌C++程序设计基础2实验六中关于群体类和群体数据组织问题的解答,详细解析了第二关的代码实现。
摘要由CSDN通过智能技术生成

以下是头歌C++程序设计基础2中的实验六(群体类和群体数据的组织)第二关的答案,代码如下:

#include <iostream>
template <class T>
class Node {
private:
    T data;
public:
    Node* next;
    Node();
    Node(const T& data, Node<T>* nt = 0);
//  Node(T data, Node<T>* n = NULL);
    T& getData();
};

template <class T>
Node<T>::Node() {
    data = 0;
    next = NULL;
}

template <class T>
Node<T>::Node(const T& d, Node<T>* nt) {
    data = d;
    next = nt;
}

template <class T>
T& Node<T>::getData() {
    return data;
}

/*
* 在LinkedList的设计中,采用了附加指针front和rear,即链表的结构为front->a1->a2->...->rear
* 只有在析构函数中才删除这两个指针,其余的时间这两个指针都是存在的,其中的数据始终为0,不存储用户数据
*/
template <class T>
class LinkedList {
private:
    Node<T> *front, *rear; //表头和表尾指针
    Node<T> *prevPtr, *currPtr; //记录表当前遍历位置的指针,由插入和删除操作更新
    int size; //表中元素个数
    int position; //当前元素在表中的位置序号。由函数reset使用

    Node<T>* newNode(const T& item, Node<T> *ptrNext = NULL); //生成新节点,数据与为item,指针域为prtNext
    void freeNode(Node<T> *p); //释放节点

    void copy(const LinkedList<T>& L); //将链表L复制到当前表(假设当前表为空),被复制构造函数和operator=调用

public:
    LinkedList(); //构造函数
    LinkedList(const LinkedList<T>& L); //复制构造函数
    ~LinkedList(); //析构函数
    LinkedList<T>& operator = (const LinkedList<T>& L); //重载赋值运算符

    int getSize() const; //返回链表中元素个数
    bool isEmpty() const; //链表是否为空

    void reset(int pos = 0); //初始化游标的位置
    void next(); //使游标移动到下一个节点
    bool endOfList() const; //游标是否移动到链尾
    int c
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值