看到这周的作业题之后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

实现doubly-linked list搞清楚了一个内存问题相关推荐

  1. 双向链表(Doubly Linked List)

    双向链表(Doubly Linked List) 1. 双向链表的概念 1.1 双向链表的定义 双向链表又称为双链表,是链表的一种. 1.2 双向链表的结点结构 双向链表的结点包括三个部分:前驱指针域 ...

  2. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法

    题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...

  3. LeetCode 430. Flatten a Multilevel Doubly Linked List

    原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/ 题目: You a ...

  4. C++Doubly Linked List双向链表(附完整源码)

    C++AVL树自平衡二叉查找树 node结构体定义 实现了以下几个接口 Doubly Linked List双向链表算法的完整源码(定义,实现,main函数测试) node结构体定义 struct n ...

  5. C语言实现双向链表Doubly Linked List(附完整源码)

    双向链表Doubly Linked List list结构体 实现以下6个接口 双向链表Doubly Linked List完整源码(定义,实现,main函数测试) list结构体 typedef s ...

  6. linux编程 fmemopen函数打开一个内存流 使用FILE指针进行读写访问

    fmemopen()函数打开一个内存流,使你可以读取或写入由buf指定的缓冲区.其返回FILE*fp就是打开的内存流,虽然仍使用FILE指针进行访问,但其实并没有底层文件(并没有磁盘上的实际文件,因为 ...

  7. C#实现的一个内存Ini类

    正式用上C#了,写了一个多星期代码了,感觉上来说,总体还蛮顺手的,直接拿来就写了.只是写的过程中,总是想着对象释放,这个比较蛋疼,我看了一些网上的代码貌似都是有new了,但是后面都没有释放,俺们还是写 ...

  8. Memcheck:一个内存错误检测器

    目录 4.1.概观4.2.来自Memcheck的错误消息说明 4.2.1.非法读取/非法写入错误4.2.2.使用未初始化的值4.2.3.在系统调用中使用未初始化或不可寻址的值4.2.4.非法释放4.2 ...

  9. 面试必问:用 Java 写一个内存泄漏程序

    编译:ImportNew/唐尤华 原文链接:stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java 问题: 刚参加的一 ...

最新文章

  1. 禁止PHP警告性错误
  2. System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本?
  3. python作者龟叔_Python基础 — Python简介
  4. HBase 的存储结构
  5. WordPress调用自带jquery的方法 ,使 $ 生效
  6. 表正在被别的用户或进程使用_linux内核对进程的管理分为两个方面
  7. oracle cost小 比较慢,Oracle数据库中有关CBO优化的三个问题
  8. C#WinForm程序异常退出的捕获、继续执行与自动重启
  9. Javascript面向对象之私有静态变量
  10. 软考资料(系统集成管理工程师)无偿分享
  11. Windows界面编程:MFC
  12. 计算机设备管理中的其他设备,解决方法:计算机设备管理器中的“其他设备”驱动程序问题!...
  13. 优秀的 HTML5 网站设计案例欣赏
  14. 电容与电感串联直流电路系统分析
  15. 基于Python的IMDB电影评论文本分类
  16. Altium Designer之4层板基本规则设置
  17. 【linux命令学习】— cut 和 tr 命令学习
  18. vue使用 moment.js 格式化时间(获取当前日期的周一和周日)
  19. 国内怎么开通苹果Arcade订阅
  20. 从零开始的命令行CLI

热门文章

  1. 【C++】入门第一课
  2. android MVC,MVP,MVVM
  3. conda环境名称消失问题
  4. iOS block循环引用问题深究
  5. 好用的录音机软件有哪些?这些软件值得收藏
  6. oracle with as用法
  7. Hive 数据迁移与备份
  8. 网站服务器怎么屏蔽ip段,iis屏蔽ip段,网站禁止ip访问
  9. 基于MATLAB Simulink的光伏特性程序,改程序说明了太阳辐射强度、光伏电池温度
  10. 酷狗、QQ、天天动听——手机音乐播放器竞品对比