zoukankan      html  css  js  c++  java
  • 嵌套链表的实现

    You guys must have seen nested list (嵌套链表) such as [1, 2, 3, [4, 5, [6, 7]], 8, [9, 0]].

    So your task is to improve DouList with nested feature. You can directly copy the codes submitted before and make changes on it. But pay attention to something which had been changed in header file.

    NOTICE 1:

    front() / back() now returns a DouList for example : a = [1, 2, 3, [4, 5, [6, 7]], 8, [9, 0]] a.front() returns [1] a.back() returns [9, 0]

                       
    1. #ifndef SSCPP2014_DOULIST_A_H
    2. #define SSCPP2014_DOULIST_A_H
    3.  
    4. #include<string>
    5.  
    6. classDouList;
    7.  
    8. structDouListNode{
    9. int elem;
    10. DouListNode*prev,*next;
    11. DouList*sublist;
    12. DouListNode(int e =0,DouListNode*p =0,DouListNode*n =0){
    13. elem = e;
    14. prev = p;
    15. next = n;
    16. sublist =0;
    17. }
    18. };
    19.  
    20. classDouList{
    21. private:
    22. DouListNode*_head,*_tail;
    23. public:
    24. DouList();
    25. DouList(constDouList&src);
    26. ~DouList();
    27. void clear();
    28. bool empty()const;
    29. std::string to_str()const;
    30. DouList front()const;
    31. DouList back()const;
    32. void push_front(constDouListNode&e);
    33. void push_back(constDouListNode&e);
    34. void pop_front();
    35. void pop_back();
    36. voidoperator=(constDouList&other);
    37. operatorint();// consider why i define it.
    38. friend std::ostream&operator<<(std::ostream &out,
    39. constDouList&list);
    40. // non-meaning static value
    41. };
    42.  
    43. #endif
               
    • #include"DouList.h"
    •  
    • DouList::DouList(){
    • _head = _tail =0;
    • }
    •  
    • DouList::DouList(constDouList&src){
    • _head = _tail =0;
    • *this= src;
    • }
    •  
    • DouList::~DouList(){
    • this->clear();
    • }
    •  
    • voidDouList::clear(){
    • DouListNode*t;
    • while(_head){
    • t = _head;
    • if(t->sublist)
    • delete t->sublist;
    • _head = _head->next;
    • delete t;
    • }
    • _head = _tail =0;
    • }
    •  
    • boolDouList::empty()const{
    • return _head ==0?true:false;
    • }
    •  
    • std::string DouList::to_str()const{
    • std::string ret ="[";
    • if(!this->empty()){
    • DouListNode*p = _head->next;
    • if(_head->sublist){
    • ret += _head->sublist->to_str();
    • }else{
    • ret += std::to_string((longlongint)(_head->elem));
    • }
    • while(p){
    • if(p->sublist){
    • ret +=", "+ p->sublist->to_str();
    • }else{
    • ret +=", "+ std::to_string((longlongint)(p->elem));
    • }
    • p = p->next;
    • }
    • }
    • ret +="]";
    • return ret;
    • }
    •  
    • DouListDouList::front()const{
    • DouList ret;
    • if(this->empty())
    • return ret;
    • if(_head->sublist)
    • return*(_head->sublist);
    • else
    • ret.push_front(*_head);
    • return ret;
    • }
    •  
    • DouListDouList::back()const{
    • DouList ret;
    • if(this->empty())
    • return ret;
    • if(_tail->sublist)
    • return*(_tail->sublist);
    • else
    • ret.push_back(*_tail);
    • return ret;
    • }
    •  
    • voidDouList::push_front(constDouListNode&e){
    • if(this->empty()){
    • _head = _tail =newDouListNode(e.elem);
    • }else{
    • _head->prev =newDouListNode(e.elem,0, _head);
    • _head = _head->prev;
    • if(e.sublist){
    • _head->sublist =newDouList(*e.sublist);
    • }
    • }
    • }
    •  
    • voidDouList::push_back(constDouListNode&e){
    • if(this->empty()){
    • _head = _tail =newDouListNode(e.elem);
    • }else{
    • _tail->next =newDouListNode(e.elem, _tail);
    • _tail = _tail->next;
    • if(e.sublist){
    • _tail->sublist =newDouList(*e.sublist);
    • }
    • }
    • }
    •  
    • voidDouList::pop_front(){
    • if(this->empty())
    • return;
    • if(_head == _tail){
    • this->clear();
    • return;
    • }else{
    • _head = _head->next;
    • if(_head->prev->sublist)
    • delete _head->prev->sublist;
    • delete _head->prev;
    • _head->prev =0;
    • }
    • }
    •  
    • voidDouList::pop_back(){
    • if(this->empty())
    • return;
    • if(_head == _tail){
    • this->clear();
    • }else{
    • _tail = _tail->prev;
    • if(_tail->next->sublist)
    • delete _tail->next->sublist;
    • delete _tail->next;
    • _tail->next =0;
    • }
    • }
    •  
    • voidDouList::operator=(constDouList&other){
    • this->clear();
    • _head = _tail =0;
    • if(other.empty())
    • return;
    • _head =newDouListNode(other._head->elem);
    • if(other._head->sublist){
    • _head->sublist =newDouList(*(other._head->sublist));
    • }
    • DouListNode*p = _head;
    • DouListNode*q = other._head->next;
    • while(q){
    • p->next =newDouListNode(q->elem, p);
    • if(q->sublist){
    • p->next->sublist =newDouList(*(q->sublist));
    • }
    • p = p->next;
    • q = q->next;
    • }
    • _tail = p;
    • }
    •  
    • std::ostream&operator<<(std::ostream &out,constDouList&list){
    • out <<list.to_str();
    • return out;
    • }
    •  
    • DouList::operatorint(){
    • if(_head)
    • return _head->elem;
    • else
    • return0;
    • }
    
    
  • 相关阅读:
    梯度下降优化算法
    【网站管理6】_一个网站SEO优化方案
    25条div+CSS编程提醒及小技巧整理
    100多个基础常用JS函数和语法集合大全
    Understanding Built-In User and Group Accounts in IIS 7
    【网站seo优化】SEO优化每天的工作内容是什么?
    如何实现织梦dedecms表单提交时发送邮箱功能【已解决】
    【织梦dedecms安全设置】dedecms如何防止被黑?dedecms被黑了怎么办?
    【dedecms网站安全】如何防止dedecms网站被DDos攻击
    【织梦dedecms系统安全】完善DEDECMS目录的权限安全设置
  • 原文地址:https://www.cnblogs.com/sysu-zhengwsh/p/3687551.html
Copyright © 2011-2022 走看看