鱼C论坛

 找回密码
 立即注册
查看: 3242|回复: 3

list求助

[复制链接]
发表于 2016-6-16 19:20:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 青云风力 于 2016-6-17 14:20 编辑

我自己写了一个链表,可是始终有一个bug,找不出来。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-6-16 19:21:09 | 显示全部楼层

有bug,找不出来。

本帖最后由 青云风力 于 2016-6-17 14:20 编辑

#pragma once


template <typename T>
class list_node {

typedef list_node<T>* link_type;
typedef list_node<T> node;

public:
        link_type next = nullptr;
        link_type prev = nullptr;
        T data;
};



template<typename T>
class list {
        typedef list_node<T>* link_type;
        typedef list_node<T> node;
private:
        link_type head = nullptr;
        link_type tail = nullptr;

public:
        list() {}
       
        list(const T& value) {
                link_type temp = (link_type)malloc(sizeof(node));
                temp->data = value;
                head = tail = temp;
        }

        list operator=(const list L){}

        ~list(){}

        void push_front(const T& value) {
                link_type temp = (link_type)malloc(sizeof(node));
                temp->data = value;
                temp->next = head;
                head->prev = temp;
                head = temp;
        }

        void push_back(const T& value) {
                link_type temp = (link_type) malloc(sizeof(node));
                temp->data = value;
                tail->next = temp;
                temp->prev = tail;
                tail = temp;
        }

        void pop_front() {
                link_type temp = head;
                head = head->next;
                delete *temp;
        }

        void pop_back() {
                link_type temp = tail;
                tail = tail->prev;
                delete *temp;
        }

        bool replace(T& x, const T& value) {
                link_type temp = head;
                while (temp->data != x && temp->next != nullptr) {
                        temp = temp->next;
                }
                if (temp->data == x) {
                        temp->data = value;
                        return true;
                }
                if (temp->next == nullptr)
                        return false;
        }

        void erase(T& value) {
                for (link_type temp = head; temp != tail; temp = temp->next) {
                        if (temp->data == value){
                                link_type x = temp;
                                temp->prev = (temp->next)->prev;
                                temp->next = (temp->prev)->next;
                                delete *x;
                        }
                }
        }

        void clean() {
                while (head != tail) {
                        link_type temp = head;
                        head = head->next;
                        delete *temp;
                }
                if (head == tail) {
                        delete *head;
                }
        }

         void PRINT() {
                 for (link_type temp = head; temp != tail; temp = temp->next)
                         std::cout << temp->data << std::endl;
         }
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-28 12:19:27 | 显示全部楼层
我来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-30 17:19:29 | 显示全部楼层
求问list怎么转为矩阵格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 22:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表