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>
class DouList ;
struct DouListNode {
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 ;
}
};
class DouList {
private :
DouListNode * _head , * _tail ;
public :
DouList ();
DouList ( const DouList & src );
~ DouList ();
void clear ();
bool empty () const ;
std :: string to_str () const ;
DouList front () const ;
DouList back () const ;
void push_front ( const DouListNode & e );
void push_back ( const DouListNode & e );
void pop_front ();
void pop_back ();
void operator =( const DouList & other );
operator int (); // consider why i define it.
friend std :: ostream & operator <<( std :: ostream & out ,
const DouList & list );
// non-meaning static value
};
#endif
#include "DouList.h"
DouList :: DouList () {
_head = _tail = 0 ;
}
DouList :: DouList ( const DouList & src ) {
_head = _tail = 0 ;
* this = src ;
}
DouList ::~ DouList () {
this -> clear ();
}
void DouList :: clear () {
DouListNode * t ;
while ( _head ) {
t = _head ;
if ( t -> sublist )
delete t -> sublist ;
_head = _head -> next ;
delete t ;
}
_head = _tail = 0 ;
}
bool DouList :: 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 (( long long int )( _head -> elem ));
}
while ( p ) {
if ( p -> sublist ) {
ret += ", " + p -> sublist -> to_str ();
} else {
ret += ", " + std :: to_string (( long long int )( p -> elem ));
}
p = p -> next ;
}
}
ret += "]" ;
return ret ;
}
DouList DouList :: front () const {
DouList ret ;
if ( this -> empty ())
return ret ;
if ( _head -> sublist )
return *( _head -> sublist );
else
ret . push_front (* _head );
return ret ;
}
DouList DouList :: back () const {
DouList ret ;
if ( this -> empty ())
return ret ;
if ( _tail -> sublist )
return *( _tail -> sublist );
else
ret . push_back (* _tail );
return ret ;
}
void DouList :: push_front ( const DouListNode & e ) {
if ( this -> empty ()) {
_head = _tail = new DouListNode ( e . elem );
} else {
_head -> prev = new DouListNode ( e . elem , 0 , _head );
_head = _head -> prev ;
if ( e . sublist ) {
_head -> sublist = new DouList (* e . sublist );
}
}
}
void DouList :: push_back ( const DouListNode & e ) {
if ( this -> empty ()) {
_head = _tail = new DouListNode ( e . elem );
} else {
_tail -> next = new DouListNode ( e . elem , _tail );
_tail = _tail -> next ;
if ( e . sublist ) {
_tail -> sublist = new DouList (* e . sublist );
}
}
}
void DouList :: 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 ;
}
}
void DouList :: 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 ;
}
}
void DouList :: operator =( const DouList & other ) {
this -> clear ();
_head = _tail = 0 ;
if ( other . empty ())
return ;
_head = new DouListNode ( other . _head -> elem );
if ( other . _head -> sublist ) {
_head -> sublist = new DouList (*( other . _head -> sublist ));
}
DouListNode * p = _head ;
DouListNode * q = other . _head -> next ;
while ( q ) {
p -> next = new DouListNode ( q -> elem , p );
if ( q -> sublist ) {
p -> next -> sublist = new DouList (*( q -> sublist ));
}
p = p -> next ;
q = q -> next ;
}
_tail = p ;
}
std :: ostream & operator <<( std :: ostream & out , const DouList & list ) {
out << list . to_str ();
return out ;
}
DouList :: operator int () {
if ( _head )
return _head -> elem ;
else
return 0 ;
}