实现doubly-linked list&&搞清楚了一个内存问题

    看到这周的作业题之后google了一下,然后果断是想多了,一直在考虑这个双向的list到底要怎么实现,啊  总之是想太多了,其实之前还想着头和尾遇到啊什么的你的prev指向我我的next再指向你,然后头和尾的prev、next要不要换方向什么的。。。结果= = 想多了。。。很自然的一个list。。双向的而已。。

    上学期指针学的太渣了,最近虽然作业一直都在,不过还是把这个写完了才理解的更清楚一点。。。

另外。。简直是血的教训深刻领悟了内存的问题。。。今天被两个大大嘲笑了T。T:“一年之后还是小白。。。。”。论NULL的必要性!!而且搞懂了内存,虚拟内存,硬盘啊什么的,其实我这种零基础的导论课本当初应该好好研究的。。T.T,只要一点击运行,我的编译器就开始卡,往死里卡,我在这期间写完了高数作业 = = 最后系统告诉我qq和xcode都被迫暂停了,重启之后我看我的硬盘的存储空间还有39G,再一运行就23G了。。之后问了下师兄,然后在点运行之后监测我的内存变化,惊呆了!几秒钟就变到2.03G,然后再一看硬盘是32G(重启之后从39G开始变化的),然后我才搞清楚。。。哦。。忘记说原因了。。。少了一行代码。。。没有把头的prev和尾的next设为NULL!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

题干里给的头文件Doulist.h

#ifndef SSCPP2014_DOULIST_A_H

#define SSCPP2014_DOULIST_A_H

#include <string>

 struct DouListNode {

    int elem;

    DouListNode *prev, *next;

    DouListNode(int e = 0, DouListNode *p = 0, DouListNode *n = 0) {

        elem = e;

        prev = p;

        next = n;

    }

};

class DouList {

private:

    DouListNode *m_head, *m_tail;

public:

    DouList();

    DouList(const DouList &src);

    ~DouList();

    void clear();

    bool empty() const;

    std::string to_str() const;

    int front() const;

    int back() const;

    void push_front(const int &e);

    void push_back(const int &e);

    void pop_front();

    void pop_back();

    void operator=(const DouList &other);

    friend std::ostream& operator<<(std::ostream &out,

                                    const DouList &list);

    // non-meaning static value

    staticint _error_sign;  // for illegal front()/back()

};

 

#endif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

题干里给的main函数

#include <iostream>

#include "DouList.h"

usingnamespacestd;

 

DouList list1, list2;

 

void display() {

    cout << list1.empty() << ':' << list1 << endl;

    cout << list2.empty() << ':' << list2.to_str() << endl;

}

 

int main() {

    display();

    list1.push_front(894);

    list2.push_back(2136);

    cout << list1.front() << ',' << list1.back() << endl;

    cout << list2.front() << ',' << list2.back() << endl;

    display();

    list1.push_back(214);

    list2.push_front(931);

    cout << list1.front() << ',' << list1.back() << endl;

    cout << list2.front() << ',' << list2.back() << endl;

    display();

    for (int i = 0; i < 10; ++i) {

        int t;

        cin >> t;

        list1.push_back(t);

        list2.push_front(t);

    }

    display();

    

    cout<<"###\n";

    for (int i = 0; i < 5; ++i) {

        list1.pop_front();

        list2.pop_back();

    }

    display();

    DouList list3(list1);

    list1 = list2;

    cout << list1 << endl;

    cout << list3 << endl;

    return 0;

}

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我忧伤的cpp。。。

 

 

#include "DouList.h"

#include <string>

#include<iostream>

usingnamespacestd;

int DouList:: _error_sign = -1;

DouList::DouList() {

    m_head = NULL;

    m_tail = NULL;

}

DouList::DouList(const DouList &src) {

    m_head = NULL;

    m_tail = NULL;

    DouListNode *temp  = src.m_head;

    DouListNode *p = NULL;

    if (temp != NULL) {

        m_head = new DouListNode(temp->elem, NULL, NULL);

        p = m_head;

        temp = temp->next;

    }

    while (temp != NULL) {

        p->next = new DouListNode(temp->elem, p, NULL);

        temp = temp->next;

        p = p->next;

    }

    m_tail = p;

   // *this = src;        ta给的标程一句话就好  T.T

}

DouList::~DouList() {

    clear();

}

void DouList::clear() {

    while (m_head != NULL) {

        DouListNode *a = m_head;

        m_head = m_head->next;

        delete a;

    }

    m_head = NULL;

    m_tail = NULL;

}

bool DouList::empty() const {

   if ((m_tail == m_head) &&(m_head == NULL)){

        return true;

    } else {

        returnfalse;

    }

}

string DouList::to_str() const {

    string output;

    if (this->empty() == true) {

        output = "[]";

    } else {

        output = "[";

        DouListNode *p = m_head->next;

        long long a = m_head->elem;  // ta说按c++11的标准,此处必须用long long !..

        output += to_string(a);

        while (p != NULL) {

            a = p->elem;

            output += ", " + to_string(a);

            p = p->next;

        }

        output += "]";

    }

    return output;

}

int DouList::front() const {

    returnm_head->elem;

}

int DouList::back() const {

    returnm_tail->elem;

}

void DouList::push_front(const int &e) {

    if (m_head == NULL) {

        m_head = new DouListNode(e, NULL, NULL);

        m_tail = m_head;

    } else {

        m_head->prev = newDouListNode( e, NULL, m_head);

        m_head = m_head->prev;

    }

}

void DouList:: push_back(const int &e) {

    if (m_tail == NULL) {

        m_tail = new DouListNode(e, NULL, NULL);

        m_head = m_tail;

    } else {

        m_tail->next = newDouListNode( e, m_tail, NULL);

        m_tail = m_tail->next;

    }

}

void DouList::pop_front() {

    if (m_head != NULL) {

        DouListNode *a = m_head;

        m_head = a->next;

        m_head->prev = NULL;  //  !!!!就是这

        delete a;

    }

}

void DouList::pop_back() {

    if(m_tail != NULL) {

        DouListNode *a = m_tail;

        m_tail = m_tail->prev;

        m_tail ->next = NULL;   // !!!还有这

        delete a;

    }

}

voidDouList::operator=(constDouList &other) {

    this->clear();

    m_head = m_tail = NULL;

    if (other.empty())

        return;

    m_head = newDouListNode(other.m_head->elem);

    DouListNode *p = m_head;

    DouListNode *q = other.m_head->next;

    while (q) {

        p->next = new DouListNode(q->elem, p);

        p = p->next;

        q = q->next;

    }

    m_tail = p;

}

ostream& operator<<(ostream &out,const DouList &list) {

    out << list.to_str();

    return out;

}

 

看硬盘刷刷刷就没了还是蛮好玩的哈哈哈哈。。。= = 。。还是挺有收获的。。写谢谢讲代码的室友和“嘲笑”我的大大们和讲解的大大。。。。。 

 

 

转载于:https://www.cnblogs.com/SunnyInSysu/p/3667712.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值