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]
- #ifndef SSCPP2014_DOULIST_A_H
- #define SSCPP2014_DOULIST_A_H
- #include<string>
- classDouList;
- structDouListNode{
- int elem;
- DouListNode*prev,*next;
- DouList*sublist;
- DouListNode(int e =0,DouListNode*p =0,DouListNode*n =0){
- elem = e;
- prev = p;
- next = n;
- sublist =0;
- }
- };
- classDouList{
- private:
- DouListNode*_head,*_tail;
- public:
- DouList();
- DouList(constDouList&src);
- ~DouList();
- void clear();
- bool empty()const;
- std::string to_str()const;
- DouList front()const;
- DouList back()const;
- void push_front(constDouListNode&e);
- void push_back(constDouListNode&e);
- void pop_front();
- void pop_back();
- voidoperator=(constDouList&other);
- operatorint();// consider why i define it.
- friend std::ostream&operator<<(std::ostream &out,
- constDouList&list);
- // non-meaning static value
- };
- #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;
- }
本文介绍了一种带有嵌套特性的链表DouList的实现方法,通过新增子链表的功能增强了传统链表的数据结构,并详细展示了DouList类的设计与核心成员函数的实现过程。
4029

被折叠的 条评论
为什么被折叠?



