嵌套链表的实现

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;
  • }
 

转载于:https://www.cnblogs.com/sysu-zhengwsh/p/3687551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值