zsh669 2022-09-28 21:25 采纳率: 87.5%
浏览 19

C++书上链表的验证,在main函数运行所有功能

把书上的链表搬到电脑上时,想要在main中检验但是不知道怎么开始
用代码块功能插入代码,请勿粘贴截图
template <typename E> class Link {
public:
    E element;
    Link* next;
    Link(const E& elemval, Link* nextVal = NULL) {
        element = elemval;
        next = nextVal;
    }
    Link(Link* nextval = NULL) {
        next = nextval;
    }
};
template <typename E> class LList : public E::template  List<E>
{
private:
    /* data */
    Link<E>* head;
    Link<E>* tail;
    Link<E>* curr;
    int cnt;

    void init() {
        curr = tail = head = new Link<E>;
        cnt = 0;
    }

    void removeall() {
        while (head != NULL) {
            curr = head;
            head = head->next;
            delete curr;
        }
    }

public:
    LList(int size/* = defaultSize*/) {
        init();
    }

    ~LList() {
        removeall();
    }

    void print() const;
    void clear() {
        removeall();
        init();
    }

    void insert(const E& it) {
        curr->next = new Link<E>(it, curr->next);
        if (tail == curr) {
            tail = curr->next;
        }
        cnt++;
    }

    void append(const E& it) {
        tail = tail->next = new Link<E>(it, NULL);
        cnt++;
    }

    E remove() {
       // Assert(curr->next != NULL, "No element");
        E it = curr->next->element;
        Link<E>* ltemp = curr->next;
        if (tail == curr->next) {
            tail = curr;
        }
        curr->next = curr->next->next;
        delete ltemp;
        cnt--;
        return it;
    }

    void moveToStart() {
        curr = head;
    }

    void moveToEnd() {
        curr = tail;
    }

    void prev() {
        if (curr == head) {
            return;
        }
        Link<E>* temp = head;
        while (temp->next != curr) {
            temp = temp->next;
        }
        curr = temp;
    }

    void next() {
        if (curr != tail) {
            curr = curr->next;
        }
    }

    int length() const {
        return cnt;
    }

    int currPos() const {
        Link<E>* temp = head;
        int i;
        for (i = 0; curr != temp; i++) {
            temp = temp->next;
        }
        return i;
    }

    void moveToPos(int pos) {
        //assert((pos >= 0) && (pos <= cnt), "Position out of range");
        curr = head;
        for (int i = 0; i < pos; i++) {
            curr = curr->next;
        }
    }

    const E& getValue() const {
       // assert(curr->next != NULL, "No value");
        return curr->next->element;
    }

    void reserve() {
        int i = 0;
        Link<E>* pre = head;
        Link<E>* it = head->next;
        for (; it->next != NULL; it = it->next, pre = pre->next) {
            it = new Link<E>(it->element, pre);
        }
        Link<E>* temp = tail;
        tail->next = it;
        tail->element = head->elememt;
        head = tail;
    }
};


  • 写回答

4条回答 默认 最新

  • CSDN专家-link 2022-09-28 21:33
    关注

    没写main函数?要验证哪些功能?

    评论

报告相同问题?

问题事件

  • 创建了问题 9月28日

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP