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日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀