c++实现list类(第一次用类) —— new对象需delete

一:起因(1)拿出自己年初实现的list,第一次用c++类实现list或者说第一次写一个工程上用到的list类,在和如今实现的list对比,心情无比复杂;(2)说明了一点,我也曾经幼稚过,现在还比较幼稚,但我相信,只要坚持最初梦想的人,终究会走向成熟;(3)更加详细的比较全的list   请看   STL list类源代码实现   (4)再次感谢自己能坚持最初的梦,相信你也有一个类似的梦;不要羡慕别人天赋,别人的成功是无法复制的,别人的辉煌是不会重演的;唯有我们自己脚踏实地的走好每一步,播下我们自己梦想的种子,大步向前。二:详细代码如下:以下是代码和一些个人心得,欢迎大家指点~ (1)LinkList类的简单实现 [cpp] view plaincopyprint? struct Node   {       int data;       Node *next;   };   class LinkedList   {       public:           LinkedList(const int &n);// n 链表长度            LinkedList(const LinkedList &aplist);           ~LinkedList();           void display();           void insert_before(const int &key,const int &toinsert);           void del(const int &todelete);           Node* find_key(const int &key);           void distroy();           int getLength();       private:           Node *head;           Node *cur;   };// 类的后面和 结构体一样,必须加 分号 (;)          LinkedList::LinkedList(const int &n)   {       head = new Node;       head->data = -1;// 此元素是一个标示符        head->next = NULL;// init head        Node *p = head;// p in the left of cur        int i;       cout << "请输入链表序列:" << endl;       for(i=0;i<n;i++)       {           cur = new Node;           cin >> cur->data;           cur->next = NULL;           p->next = cur;           p = cur;       }   }   LinkedList::~LinkedList()   {       cout << "***";       distroy();   }   void LinkedList::display()   {       if(head == NULL)       {           cout << "此链表不存在!" << endl;           return;       }       cur = head->next;       if(cur == NULL)       {           cout << "此链表为空!" << endl;           return;       }       while(cur != NULL)       {           cout << cur->data << " ";           cur = cur->next;       }       cout << endl;// 多一个空格的    }         void LinkedList::del(const int &todelete)   {       if(head == NULL)       {           cout << "此链表不存在!" << endl;           return;       }       Node *p = head;       cur = head->next;       if(cur == NULL)       {           cout << "此链表为空,删除失败!" << endl;           return;       }       while(cur!=NULL && cur->data!=todelete)       {           p = cur;           cur = cur->next;       }       if(cur == NULL)           cout << "此元素不存在,删除失败!" << endl;// 当然也可能此链表为空        else       {           cout << "成功删除元素:" << cur->data << endl;           p->next = cur->next;           delete cur;       }   }   void LinkedList::insert_before(const int &key,const int &toinsert)   {       if(head == NULL)       {           cout << "此链表不存在!" << endl;           return;       }       Node *p,*tmp;       p = head;       cur = head->next;       if(cur == NULL)       {           cout << "此链表为空,插入失败!" << endl;// 当然也可能此链表为空            return;       }       while(cur!=NULL && cur->data!=key)       {           p = cur;           cur = cur->next;       }       if(cur == NULL)           cout << key << ":此元素不存在,插入失败!" << endl;       else       {          tmp = new Node;          tmp->data = toinsert;          tmp->next = NULL;          p->next = tmp;          tmp->next = cur;          cout << "成功插入元素:" << toinsert << endl;       }   }   Node* LinkedList::find_key(const int &key)   {       if(head == NULL)       {           cout << "此链表不存在!" << endl;           return NULL;       }       cur = head->next;       if(cur == NULL)       {           cout << "此链表为空,查找失败!" << endl;           return head;       }       while(cur!=NULL && cur->data!=key)           cur = cur->next;       if(cur == NULL)       {           cout << key << "此元素不在链表中,查找失败!" << endl;           return head;       }       else       {           cout << "成功查找到元素:" << cur->data << endl;           return cur;       }   }         void LinkedList::distroy()   {       if(head == NULL)       {           cout << "此链表不存在!" << endl;           return;       }       cur = head->next;       if(cur == NULL)       {           cout << "此链表已经为空,无法再次删除!" << endl;           delete head;//销毁head节点            head = NULL;// 用于后面的判断            return;       }       while(cur != NULL)       {           head->next = cur->next;           delete cur;           cur = head->next;       }       if(head != NULL)       {           delete head;           head = NULL;       }   }   int LinkedList::getLength()   {       int cp = 0;       if(head == NULL)       {           cout << "此链表不存在!" << endl;           return cp;       }       cur = head->next;       if(cur == NULL)       {           cout << "此链表为空!" << endl;           return cp;       }       while(cur != NULL)       {           cp ++;           cur = cur->next;       }       return cp;   }  struct Node { int data; Node *next; }; class LinkedList { public: LinkedList(const int &n);// n 链表长度 LinkedList(const LinkedList &aplist); ~LinkedList(); void display(); void insert_before(const int &key,const int &toinsert); void del(const int &todelete); Node* find_key(const int &key); void distroy(); int getLength(); private: Node *head; Node *cur; };// 类的后面和 结构体一样,必须加 分号 (;) LinkedList::LinkedList(const int &n) { head = new Node; head->data = -1;// 此元素是一个标示符 head->next = NULL;// init head Node *p = head;// p in the left of cur int i; cout << "请输入链表序列:" << endl; for(i=0;i<n;i++) { cur = new Node; cin >> cur->data; cur->next = NULL; p->next = cur; p = cur; } } LinkedList::~LinkedList() { cout << "***"; distroy(); } void LinkedList::display() { if(head == NULL) { cout << "此链表不存在!" << endl; return; } cur = head->next; if(cur == NULL) { cout << "此链表为空!" << endl; return; } while(cur != NULL) { cout << cur->data << " "; cur = cur->next; } cout << endl;// 多一个空格的 } void LinkedList::del(const int &todelete) { if(head == NULL) { cout << "此链表不存在!" << endl; return; } Node *p = head; cur = head->next; if(cur == NULL) { cout << "此链表为空,删除失败!" << endl; return; } while(cur!=NULL && cur->data!=todelete) { p = cur; cur = cur->next; } if(cur == NULL) cout << "此元素不存在,删除失败!" << endl;// 当然也可能此链表为空 else { cout << "成功删除元素:" << cur->data << endl; p->next = cur->next; delete cur; } } void LinkedList::insert_before(const int &key,const int &toinsert) { if(head == NULL) { cout << "此链表不存在!" << endl; return; } Node *p,*tmp; p = head; cur = head->next; if(cur == NULL) { cout << "此链表为空,插入失败!" << endl;// 当然也可能此链表为空 return; } while(cur!=NULL && cur->data!=key) { p = cur; cur = cur->next; } if(cur == NULL) cout << key << ":此元素不存在,插入失败!" << endl; else { tmp = new Node; tmp->data = toinsert; tmp->next = NULL; p->next = tmp; tmp->next = cur; cout << "成功插入元素:" << toinsert << endl; } } Node* LinkedList::find_key(const int &key) { if(head == NULL) { cout << "此链表不存在!" << endl; return NULL; } cur = head->next; if(cur == NULL) { cout << "此链表为空,查找失败!" << endl; return head; } while(cur!=NULL && cur->data!=key) cur = cur->next; if(cur == NULL) { cout << key << "此元素不在链表中,查找失败!" << endl; return head; } else { cout << "成功查找到元素:" << cur->data << endl; return cur; } } void LinkedList::distroy() { if(head == NULL) { cout << "此链表不存在!" << endl; return; } cur = head->next; if(cur == NULL) { cout << "此链表已经为空,无法再次删除!" << endl; delete head;//销毁head节点 head = NULL;// 用于后面的判断 return; } while(cur != NULL) { head->next = cur->next; delete cur; cur = head->next; } if(head != NULL) { delete head; head = NULL; } } int LinkedList::getLength() { int cp = 0; if(head == NULL) { cout << "此链表不存在!" << endl; return cp; } cur = head->next; if(cur == NULL) { cout << "此链表为空!" << endl; return cp; } while(cur != NULL) { cp ++; cur = cur->next; } return cp; }(2)main函数的测试 [cpp] view plaincopyprint? #include <iostream>    using namespace std;      // 主函数开始,main()    int main()   {       int len,todelete,key,toinsert,tofind;// 当然也可以把一些变量同意命名为opt_key        Node *tmp;       int cp;       cout << "请输入所要建立链表的长度:" << endl;       cin >> len;       LinkedList *list = new LinkedList(len);// new 出来的对象,用指针指向        list->display();// 不能用list.display()的        //LinkedList list = LinkedList(len);        //list.display();// 注释的语句也是对的,但是必须都得改为 list.xxx;        //而且new的对象(在堆上)必须手动delete,否则不调用析构函数,非new出来的对象,自动调用析构函数              cout << "请输入所要删除的元素:" << endl;       cin >> todelete;       list->del(todelete);       list->display();             cout << "请输入所要查找的元素:" << endl;       cin >> tofind;       tmp = list->find_key(tofind);       cout << "查找后返回main()的结果:" << tmp->data << endl;       list->display();             cout << "请输入所要插入的元素和其右侧元素:" << endl;       cin >> toinsert >> key;       list->insert_before(key,toinsert);       list->display();             cout << "销毁前的链表长度:";       cout << list->getLength() << endl;       list->distroy();       cp = list->getLength();       cout << "销毁后的链表长度:";       cout << cp << endl;       //list->~LinkedList();// 本以为是析构函数是不可以自动调用的,        //其实不然,课本上明明说析构函数是可以自动调用的,但是有一个前提:对象的生存期即将结束的时        //在函数结束时调用(栈内存即将销毁时),堆内存中对象是在delete时调用        delete list;       return 0;   }  #include <iostream> using namespace std; // 主函数开始,main() int main() { int len,todelete,key,toinsert,tofind;// 当然也可以把一些变量同意命名为opt_key Node *tmp; int cp; cout << "请输入所要建立链表的长度:" << endl; cin >> len; LinkedList *list = new LinkedList(len);// new 出来的对象,用指针指向 list->display();// 不能用list.display()的 //LinkedList list = LinkedList(len); //list.display();// 注释的语句也是对的,但是必须都得改为 list.xxx; //而且new的对象(在堆上)必须手动delete,否则不调用析构函数,非new出来的对象,自动调用析构函数 cout << "请输入所要删除的元素:" << endl; cin >> todelete; list->del(todelete); list->display(); cout << "请输入所要查找的元素:" << endl; cin >> tofind; tmp = list->find_key(tofind); cout << "查找后返回main()的结果:" << tmp->data << endl; list->display(); cout << "请输入所要插入的元素和其右侧元素:" << endl; cin >> toinsert >> key; list->insert_before(key,toinsert); list->display(); cout << "销毁前的链表长度:"; cout << list->getLength() << endl; list->distroy(); cp = list->getLength(); cout << "销毁后的链表长度:"; cout << cp << endl; //list->~LinkedList();// 本以为是析构函数是不可以自动调用的, //其实不然,课本上明明说析构函数是可以自动调用的,但是有一个前提:对象的生存期即将结束的时 //在函数结束时调用(栈内存即将销毁时),堆内存中对象是在delete时调用 delete list; return 0; }三:心得和体会 (1)在C++中,普通的对象离开它的作用域之后,它的析构函数会被自动调用,从而销毁这个对象,释放它所占的内存,不会发生内存泄露的问题。(2)new命令可以为对象在内存中动态分配一定的空间,并返回分配空间的首地址;如果在程序运行结束之前没有用delete来销毁这些对象,释放它们所占用的空间也会发生内存泄露。(3)看到自己上面写的两条心得和体会,自己也扑哧笑了;再看自己写的代码 —— main函数和LinkList类竟然在一起;看来我当初真的幼稚了很多;(4)再看看现在写的代码,完全不一样了,不过我还是感谢当初的我,那时的我选择的路;成熟是建立在幼稚的基础上的。(5)最后摘抄的一句话,与大家共享 : 当你的才华撑不起你的野心时,只有静下心来好好学习! 纵使命运注定是个打酱油的,也要打一瓶与别人不一样的酱油!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值