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;
    • }
    
    
  • 相关阅读:
    vue项目的骨架及常用组件介绍
    细谈最近上线的Vue2.0项目(一)
    【请求之密】payload和formData有什么不同?
    【19道XSS题目】不服来战!
    Hexo+Coding搭建免费博客之Hexo代码上传到Coding实现公网访问站点(三)
    Hexo+Coding搭建免费博客之Next主题设置(二)
    Hexo+Coding搭建免费博客之Hexo安装部署(一)
    Openstack-Queens详细安装教程
    ESXI安装报错,No Network adapters were detected...
    VMware ESXi 5.5组件安装过程记录
  • 原文地址:https://www.cnblogs.com/sysu-zhengwsh/p/3687551.html
Copyright © 2011-2022 走看看