zoukankan      html  css  js  c++  java
  • 双向循环链表栈队列 (list大杂烩~会模版就好咯)

    #include <iostream>
    
    using namespace std;
    
    //#define DEBUG 1
    #ifdef DEBUG
    
    #include <cstdio>
    
    int III = 0, iii;
    #define FUN_IN  for(iii = 0; iii < III; ++iii) 	\
    			printf("\t"); 	\
    		++III; 		   	\
    printf("[%s(): %d] INTO!!!\n", __func__, __LINE__);
    
    #define FUN_OUT --III; 		\
    	for(iii = 0; iii < III; ++iii) 	\
    		printf("\t"); 	\
    printf("[%s(): %d] OUT &&&\n", __func__, __LINE__);
    
    #else
    #define FUN_IN
    #define FUN_OUT 
    #endif
    
    
    #define print() cout << __LINE__ << ": " << __func__ << endl
    
    class list
    {
    public: 			//必需的
    	list();
    	list(const unsigned int count);
    	list(const unsigned int count, char c);
    	list(list &mylist); 							//list(const list &mylist);
    	list& operator=(list &mylist); 						//list& operator=(const list &mylist);
    // 	list& operator=(list *tmp); 						//c = a + b; or c = a - b;
    	~list();
    
    public: 			//非变动性重载	
    	operator char() const {return c;}   					//cout << l1 << endl;
    	void operator()(const char *s = " ") {cout << s << ": "; output();} 	// eg: l1();
    	bool operator==(list &rlist); 						//比较是长度和内容是否相同
    	bool operator!=(list &rlist) {return !(operator==(rlist));}
    	bool operator>(list &mylist)  {if(lenght() > mylist.lenght())return true; else return false;}   //比较长度
    	bool operator>=(list &mylist) {if(lenght() >= mylist.lenght())return true; else return false;}
    	bool operator<(list &mylist)  {return  !(operator>=(mylist));}
    	bool operator<=(list &mylist) {return !(operator>(mylist));}
    
    				//变动性重载	
    	list& operator+(); 							//从小到大排列
    	list& operator-(); 							//从大到小排列
    	list& operator!(); 							//反序 eg: l1:abcdefg -> !l1:gfedcba
    	list& operator-(list &rlist); 						//c = a - b;a,b不变 c 为 a - b 的效果
    	list& operator-=(list &rlist); 						//rlist不变,去掉*this里有list元素的节点
    	list& operator+(list &rlist); 						//a:123 b:abc c = a + b; c:123abc, a,b不变
    // 	list* operator+(list &rlist); 						//a:123 b:abc c = a + b; c:123abc, a,b不变
    	list& operator+=(list &rlist); 						//a:123 b:abc a+=b; a:123abc, b不变
    	list& operator*(); 							//去掉重复节点 eg:l1:112332 -> l1:123
    	list& operator~();                                            		//对称轴 eg: l1:abcde -> ~l1:abcdeedcba
    	char& operator[](unsigned int index); 					//随机访问 eg: list1[i] = c; a = list1[i];
    	list& operator<<(char c) {last_insert(c);  return *this;}  		//尾插     >> head->tail << 
    	list& operator>>(char c) {first_insert(c); return *this;} 		//头插 eg: l1 >> 'a' >> 'b' << 'c'; -> bac 
    	list& operator<<=(unsigned int i); 			                //向右滚动 eg: l1 <<= 3; abcdef -> defabc
    	list& operator>>=(unsigned int i); 		  			//向左滚动 eg: l1 >>= 2; defabc -> bcdefa
    
    public: 			//list非变动性操作                  
    	list& output();
    	list& output(list *p);
    	list& self();	
    	list* find(char c);
    	bool   empty();
    	unsigned int   lenght();
    	list* begin() {return next;}
    	list* end() {return front;}
    
    public: 			//list变动性操作                  
    	list& clean();
    	list& swap(list &rlist);						//头交换 eg:l1:123 l2:abc -> l1:abc l2:123
    
    	list* insert(list *p, char c);
    	list* insert(list *p, int n, char c);
    	list& first_insert(char c);
    	list& first_insert(int n, char c);
    	list& last_insert(char c);
    	list& last_insert(int n, char c);
    
    	list* modify(char c = '?');
    	list* modify(list *p, char c);
    	list* modify(list *begin, list *end, char c);
    	
    	list* remove();
    	list* remove(list *p);
    	list* remove(list *begin, list *end);
    	list* remove(char c);
    	list* remove(list *begin, list *end, char c);
    
    public: 			//stack
    	list top() {if(empty()){cout << "empty" << endl;return *this;}else return *front;}
    	list& push(char c) {last_insert(c); return *this;}
    	list& pop() {remove(front); return *this;}
    
    public: 			//queue
    	list head() {if(empty()){cout << "empty" << endl;return *this;}else return *front;}
    	list tail() {if(empty()){cout << "empty" << endl;return *this;}else return *next;}
    	list& head_into(char c) {push(c); return *this;}
    	list& head_out() {pop(); return *this;}
    	list& tail_into(char c){first_insert(c); return *this;}
    	list& tail_out(){remove(next); return *this;}
    
    private:
    	void insert(char c = '?');
    
    private:
    	char c;
    	list *front;
    	list *next;
    };
    
    /*
    list& list::operator=(list *tmp)
    {
    	clean();
    	if(tmp == NULL)
    		return *this;
    
    	next = tmp->next;
    	list *find_front;
    	for(find_front = next; find_front != NULL; find_front = find_front->next)
    		;
    	front = find_front;
    
    	return *this;		
    }
    */
    list& list::operator+()
    {
    	if(lenght() <= 1)
    		return *this;
    
    	list tmplist;
    	int len = lenght();
    	for(int i = 0; i != len; ++i)
    	{
    		list *mp = next;
    		for(list *tmp = next; tmp != this; tmp = tmp->next)
    		{
    			if(mp->c > tmp->c)
    				mp = tmp;
    		}
    
    		tmplist.last_insert(mp->c);
    
    		remove(mp);
    	}
    
    	next = tmplist.next;
    	front = tmplist.front;
    	tmplist.next->front = this;
    	tmplist.front->next = this;
    
    	return *this;
    }
    
    /*	
    list& list::operator+()
    {
    	if(lenght() <= 1)
    		return *this;
    	list *tmp = next;
    	next = NULL;
    	front = NULL;
    	for(; tmp != this; )
    	{
    cout << *tmp << endl;
    		list *mp = tmp;
    		for(list *p = tmp->next; p != this; p = p->next)//找出最小的节点
    		{
    			if(mp->c > p->c)
    				mp = p;
    		}
    cout << "min:" <<*mp << endl;
    	
    		//取出节点
    		if(mp->next == this && mp->front == this)
    		{
    //			tmp = tmp->next;
    cout << "IF 1" << endl;
    		}		
    		else if(mp->next != this && mp->front == this)
    		{
    //			tmp = tmp->next;
    cout << "IF 2" << endl;
    		}
    		else if(mp->next == this && mp->front != this)
    		{
    			list *tp = tmp;
    //			tmp = tmp->next;
    			tp->front->next = this;
    cout << "IF 3" << endl;
    		}
    		else
    		{
    //			tmp = tmp->next;
    			mp->front->next = mp->next;
    			mp->next->front = mp->front;
    cout << "IF 4" << endl;
    		}
    cout << "take:" <<*mp << endl;
    
    cout << "lenght:" << lenght() << endl;
    		//尾插
    		if(lenght() == 0)
    		{
    cout << "NULL" << endl;
    			next = mp;
    			front = mp;
    			mp->next = this;
    			mp->front = this;
    		}
    		else
    		{
    cout << "NO NULL" << endl;
    			next = mp;
    			front->next = mp;
    			mp->front = front;
    			front = mp;
    			mp->next = this;
    		}
    		
    	}
    
    	return *this;
    }
    */
    
    list& list::operator-() 						
    {
    	if(lenght() <= 1)
    		return *this;
    
    	+*this;
    	!*this;
    
    	return *this;
    }
    
    list& list::operator!()
    {
    	if(lenght() <= 1)
    		return *this;
    	list *p = next;
    	for(list *tmp = front; tmp != p; )
    	{
    		list *mp = tmp;
    		tmp = tmp->front;
    		
    		//insert(mp);
    		mp->front = p->front;
    		mp->front->next = mp;
    		mp->next = p;
    		p->front = mp;
    
    	}
    	front = p;
    	p->next = this;
    
    	return *this;
    }
    
    //*     警告很烦,暂时去掉
    list& list::operator-(list &rlist)
    {
    	list tlist(*this);
    	if(lenght() == 0 || rlist.lenght() == 0)
    		return *this;
    
    	for(list *rp = rlist.next; rp != &rlist; rp = rp->next)
    		for(list *tmp = tlist.next; tmp != &tlist; )
    			if(tmp->c == rp->c)
    			{
    				list *rtmp = tmp;
    				tmp = tmp->next;
    				remove(rtmp);
    			}
    			else
    				tmp = tmp->next;
    
    	return tlist;
    }
    
    list& list::operator-=(list &rlist)
    {
    	if(lenght() == 0 || rlist.lenght() == 0)
    		return *this;
    
    	for(list *rp = rlist.next; rp != &rlist; rp = rp->next)
    		for(list *tmp = next; tmp != this; )
    			if(tmp->c == rp->c)
    			{
    				list *rtmp = tmp;
    				tmp = tmp->next;
    				remove(rtmp);
    			}
    			else
    				tmp = tmp->next;
    
    	return *this;
    }
    
    list& list::operator*()
    {
    	if(lenght() == 0)
    		return *this;
    	for(list *tmp = next; tmp != this; tmp = tmp->next)
    		for(list *rp = tmp->next; rp != this; )
    			if(tmp->c == rp->c)
    			{
    				list *r = rp;
    				rp = rp->next;
    				remove(r);
    			}
    			else
    				rp = rp->next;
    }
    //*     警告很烦,暂时去掉
    list& list::operator+(list &rlist)
    {
    	list llist(*this);
    	if(rlist.lenght() == 0)
    		return llist;
    
    	list tlist(rlist);
    
    	if(lenght() == 0)
    	{
    		llist.front = tlist.front;
    		llist.front->next = &llist;
    		llist.next = tlist.next;
    		llist.next->front = &llist;
    		return llist;
    	}
    	
    	llist.front->next = tlist.next;
    	tlist.next->front = llist.front;
    
    	llist.front = tlist.front;
    	llist.front->next = &llist;
    
    	return llist;
    }
    /*
    list* list::operator+(list &rlist)
    {
    	list llist(*this);
    	if(rlist.lenght() == 0)
    	{
    		if(lenght() == 0)
    		{	
    			return NULL;
    		}
    		else
    		{
    			llist.front->next = NULL;
    			return llist.next;	
    		}
    	}
    	list tlist(rlist);
    	if(lenght() == 0)
    	{
    		llist.front = tlist.front;
    		llist.front->next = &llist;
    		llist.next = tlist.next;
    		llist.next->front = &llist;
    		llist.front->next = NULL; //为最后一个节点做记号
    		return llist.next;
    	}
    	
    	llist.front->next = tlist.next;
    	tlist.next->front = llist.front;
    
    	llist.front = tlist.front;
    	llist.front->next = &llist;
    	llist.front->next = NULL; //为最后一个节点做记号
    
    	return llist.next;
    }
    */
    list& list::operator+=(list &rlist)
    {
    	if(rlist.lenght() == 0)
    		return *this;
    
    	list tlist(rlist);	
    	if(lenght() == 0)
    	{
    		front = tlist.front;
    		front->next = this;
    		next = tlist.next;
    		next->front = this;
    		return *this;
    	}
    	
    	front->next = tlist.next;
    	tlist.next->front = front;
    
    	front = tlist.front;
    	front->next = this;
    
    	return *this;
    }
    
    list& list::operator<<=(unsigned int i) 			            
    {
    	if(lenght() == 0)
    		return *this;
    
    	unsigned int c = i % lenght();
    	if(c == 0)	
    		return *this;
    
    	list *tmp = front; 
    	next->front = front;
    	front->next = next;
    	for(int k = 1; k != c; ++k)
    		 tmp = tmp->front;
    
    	tmp->front->next = this;
    	front = tmp->front;
    	next = tmp;
    	tmp->front = this;
    
    	return *this;
    
    }
    
    list& list::operator>>=(unsigned int i) 		  
    {
    	if(lenght() == 0)
    		return *this;
    
    	unsigned int c = i % lenght();
    	if(c == 0)	
    		return *this;
    
    	list *tmp = next; 
    	next->front = front;
    	front->next = next;
    	for(int k = 0; k != c; ++k)
    		 tmp = tmp->next;
    
    	tmp->front->next = this;
    	front = tmp->front;
    	next = tmp;
    	tmp->front = this;
    
    	return *this;
    }
    
    bool list::operator==(list &rlist)
    {
    	if(lenght() != rlist.lenght())
    		return false;
    	if(lenght() == 0 && rlist.lenght() == 0)
    		return true;
    	else
    	{
    		for(list *pn = next, *rpn = rlist.next;
    		    pn != this && rpn != &rlist; 
    		    pn = pn->next, rpn = rpn->next)
    			if(pn->c != rpn->c)
    				return false;
    		return true;
    	}
    }
    
    list& list::operator~() 	                                   
    {
    	if(lenght() != 0)
    		for(list *tmp = front; tmp != this; tmp = tmp->front)
    			insert(this, tmp->c);	
    	return *this;	
    }
    
    list& list::swap(list &rlist)
    {
    	if(lenght() == 0 && rlist.lenght() == 0)
    		return *this;
    	else if(lenght() != 0 && rlist.lenght() == 0)
    	{
    		rlist.next = next; 
    		rlist.next->front = &rlist;
    		rlist.front = front;
    		rlist.front->next = &rlist;
    
    		next = NULL;
    		front = NULL;
    		return *this;
    	}
    	else if(lenght() == 0 && rlist.lenght() != 0)
    	{
    		next = rlist.next;
    		next->front = this;
    		front = rlist.front;
    		front->next = this;
    
    		rlist.next = NULL;
    		rlist.front = NULL;
    		return *this;
    	}
    	else 
    	{
    		list *pn = next, *pf = front;
    		next = rlist.next;
    		next->front = this;
    		front = rlist.front;
    		front->next = this;
    
    		rlist.next = pn;
    		pn->front = &rlist;
    		rlist.front = pf;
    		pf->next = &rlist;
    
    		return *this;
    	}
    }
    
    char& list::operator[](unsigned int index)
    {
    	if(index >= lenght())
    		return c;
    	else
    	{ 	
    		list *tmp = next;
    		for(int i = 0; (tmp != this)&&(i != index); ++i)
    			tmp = tmp->next;
    		return tmp->c;		
    	}
    }
    
    //list& list::operator=(const list &mylist) //又是这个错误
    //list.cpp: In member function ‘list& list::operator=(const list&)’:
    //list.cpp:99:19: error: passing ‘const list’ as ‘this’ argument of ‘unsigned int list::lenght()’ discards qualifiers
    list& list::operator=(list &mylist)
    {
    cout << "operator=" << endl;
    	if(!empty())
    		clean();
    
    	if(mylist.lenght() == 0)
    	{
    		return *this;
    	}
    	else
    	{
    		list *my = mylist.next;
    		list *tmp = new list;
    		front = tmp;
    		next = tmp;
    		tmp->c = my->c;
    		tmp->front = this;
    		tmp->next = this;
    		my = my->next;
    
    		for( ; my != &mylist; my = my->next)
    		{
    			tmp = new list;
    			tmp->c = my->c;
    			tmp->next = this;
    			tmp->front = front;
    			front->next = tmp;
    			front = tmp;
    		}
    		return *this;
    	}
    }
    
    list* list::remove(char c)
    {
    	remove(next, this, c);
    	return this;
    }
    
    list* list::remove(list *begin, list *end, char c)
    {
    	for(list *tmp = begin; tmp != end && !empty(); tmp = tmp->next)
    		if(tmp->c == c)
    			remove(tmp);
    	return end;
    	
    }
    
    list* list::remove()
    {
    	remove(next, this);
    	return this;
    }
    
    list* list::remove(list *begin, list *end)
    {
    	for(list *tmp = begin; tmp != end && !empty(); tmp = tmp->next)
    		remove(tmp);
    	return end;
    }
    
    list* list::remove(list *p)
    {
    	if(empty())
    	{
    		cout << "empty" << endl;	
    		return this;
    	}
    	else if(p == this)
    	{
    		cout << "It's head" << endl;
    		return this;
    	}
    	else
    	{
    		list *tmp = p;
    		p = p->next;
    		tmp->front->next = tmp->next;
    		tmp->next->front = tmp->front;
    		delete tmp;
    		return p;
    	}
    }
    
    list* list::modify(char c)
    {
    	modify(next, this, c);
    	return this;
    }
    
    list* list::modify(list *begin, list *end, char c)
    {
    	for(list *tmp = begin; tmp != end; tmp = tmp->next)	
    		modify(tmp, c);
    
    //		if(tmp != this)
    //			tmp->c = c;
    	return end;
    }
    
    list* list::modify(list *p, char c)
    {
    	//*p = c;//重载了char()
    	if(p != this)
    		p->c = c;
    	else
    		cout << "It's head" << endl;
    	return p;
    }
    
    void list::insert(char c)
    {
    	list *tmp = new list;
    	tmp->c = c;
    	next = tmp;
    	front = tmp;
    	tmp->next = this;
    	tmp->front = this;	
    }
    
    list& list::first_insert(int n, char c)
    {
    	for(int i = 0; i != n; ++i)	
    		first_insert(c);
    
    	return *this;
    }
    
    list& list::first_insert(char c)
    {
    	if(empty())
    	{
    		insert(c);
    		/*	
    		list *tmp = new list;
    		tmp->c = c;
    		next = tmp;
    		front = tmp;
    		tmp->next = this;
    		tmp->front = this;	
    		*/
    
    	}	
    	else
    		insert(next, c);
    
    	return *this;
    }
    
    list& list::last_insert(int n, char c)
    {
    	for(int i = 0; i != n; ++i)	
    		last_insert(c);
    
    	return *this;
    }
    
    list& list::last_insert(char c)
    {
    	if(empty())
    	{
    		insert(c);
    		/*
    		list *tmp = new list;
    		tmp->c = c;
    		next = tmp;
    		front = tmp;
    		tmp->next = this;
    		tmp->front = this;	
    		*/
    	}	
    	else
    	{
    		/*
    		tmp->c = c;
    		front->next = tmp;
    		tmp->front = front;
    		front = tmp;
    		tmp->next = this;
    		*/
    		insert(this, c);
    	}
    
    	return *this;
    }
    
    list* list::insert(list *p, int n, char c)
    {
    	if(empty())
    	{
    		insert(c);	
    		insert(begin(), n-1, c);
     		p = end();
    	}
    	else
    	{
    		for(int i = 0; i != n; ++i)	
    		{
    			insert(p, c);
    		}
    	}
    
    	return p;
    }
    
    list* list::insert(list *p, char c)
    {
    	if(empty())
    	{
    		insert(c);
    		/*
    		list *tmp = new list;
    		tmp->c = c;
    		next = tmp;
    		front = tmp;
    		tmp->next = this;
    		tmp->front = this;	
    		*/
    	}	
    	else
    	{
    		list *tmp = new list;
    		tmp->c = c;
    		p->front->next = tmp;
    		tmp->front = p->front;
    		tmp->next = p;
    		p->front = tmp;
    	}
    
    	return p;
    }
    
    unsigned int list::lenght()
    {
    	if(empty())
    		return 0;
    	else
    	{
    		unsigned int i = 0;
    		for(list *tmp = next; tmp != this; tmp = tmp->next)
    			++i;
    		return i;
    	}
    }
    
    bool list::empty()
    {
    	if(front == NULL && next == NULL)	
    	//if((next != NULL && front !=NULL) && (next != this && front != this))
    		return true;
    	else
    		return false;
    }
    
    list* list::find(char c)
    {
    	for(list *tmp = next; tmp != this; tmp = tmp->next)	
    		if(tmp->c == c)
    			return tmp;
    	cout << "no have, return this" << endl;
    	return this;
    }
    
    list& list::clean()
    {
    FUN_IN;
    	if(front != NULL && next != NULL)
    	{
    		if(next != NULL && front != NULL)
    		{
    			for(list *tmp_next, *tmp = next; tmp != front; tmp = tmp_next)
    			{
    				tmp_next = tmp->next;
    				delete tmp;
    			}
    			delete front;
    		}	
    
    		front = NULL;
    		next = NULL;
    		cout << "clean" << endl;
    	}
    	else
    		cout << "only head" << endl;
    
    FUN_OUT	
    	return *this;
    }
    
    list& list::self()
    {
    FUN_IN;
    /*
    	cout << "c = " << c << endl
    	     << "front = " << front << endl
    	     << "next  = " << next << endl;
    */
    	output(this);
    FUN_OUT;
    	return *this;
    }
    
    list& list::output(list *p)
    {
    FUN_IN;
    	cout << "c = " << p->c << endl
    	     << "front = " << p->front << endl
    	     << "next  = " << p->next << endl;
    FUN_OUT;
    	return *this;
    }
    
    list& list::output()
    {
    FUN_IN;
    	if((next != NULL && front !=NULL) && (next != this && front != this))
    		for(list *tmp = next; tmp != this; tmp = tmp->next)
    			cout << tmp->c << " ";
    		//	cout << *tmp << " ";//重载了char()
    	else
    		cout << "only head, output:empty";
    	cout << endl;
    FUN_OUT;
    	return *this;
    }
    
    list::list() : c('Z'), next(NULL), front(NULL) 
    {
    FUN_IN;
    /*
    	c = 'Z';
    	next = NULL;
    	front = NULL;
    */
    FUN_OUT;
    }
    
    
    //list::list(const list &mylist) : c('Z'), next(NULL), front(NULL) //按道理可以这样写的,怎么会报错~
    //list.cpp: In copy constructor ‘list::list(const list&)’:
    //list.cpp:400:41: error: passing ‘const list’ as ‘this’ argument of ‘unsigned int list::lenght()’ discards qualifiers
    list::list(list &mylist) : c('Z'), next(NULL), front(NULL)
    {
    	if(mylist.lenght() == 0)
    	{
    		return;
    	}
    	else
    	{
    		list *my = mylist.next;
    		list *tmp = new list;
    		front = tmp;
    		next = tmp;
    		tmp->c = my->c;
    		tmp->front = this;
    		tmp->next = this;
    		my = my->next;
    
    		for( ; my != &mylist; my = my->next)
    		{
    			tmp = new list;
    			tmp->c = my->c;
    			tmp->next = this;
    			tmp->front = front;
    			front->next = tmp;
    			front = tmp;
    		}
    	}
    	
    }
    
    list::list(const unsigned int count, char c) : c('Z'), next(NULL), front(NULL)
    {
    FUN_IN;
    	//list();  //在拷贝构造函数中调用的构造函数, 在此处调用构造函数的效果是产生一个临时对象紧接着又释放了.
    	           //因此被返回的临时对象没有被初始化. 所以结果是一个随机值,等于没构造.
    /*
    	this->c = 'Z';
    	next = NULL;
    	front = NULL;
    */
    	if(count == 0)
    		;
    	else
    	{
    		list *tmp = new list;
    		front = tmp;
    		next = tmp;
    		tmp->c = c;
    		tmp->front = this;
    		tmp->next = this;
    
    		for(unsigned int j = 1; j != count; ++j)
    		{
    			tmp = new list;
    			tmp->c = c;
    			tmp->next = this;
    			tmp->front = front;
    			front->next = tmp;
    			front = tmp;
    		}
    	}
    FUN_OUT;
    }
    
    //list::list(const unsigned int count) : list() //在构造函数初始化列表调用构造函数
    						//error: type ‘list’ is not a direct base of ‘list’
    list::list(const unsigned int count) : c('Z'), next(NULL), front(NULL)
    {
    FUN_IN;
    	//list();  //在拷贝构造函数中调用的构造函数, 在此处调用构造函数的效果是产生一个临时对象紧接着又释放了.
    	           //因此被返回的临时对象没有被初始化. 所以结果是一个随机值,等于没构造.
    /*
    	c = 'Z';
    	next = NULL;
    	front = NULL;
    */
    	if(count == 0)
    		;
    	else
    	{
    		int number = '0';
    		list *tmp = new list;
    		front = tmp;
    		next = tmp;
    		tmp->c = number++;
    		tmp->front = this;
    		tmp->next = this;
    
    		for(unsigned int j = 1; j != count; ++j)
    		{
    			tmp = new list;
    			tmp->c = number++;
    			tmp->next = this;
    			tmp->front = front;
    			front->next = tmp;
    			front = tmp;
    		}
    	}
    FUN_OUT;
    }
    
    list::~list()
    {
    FUN_IN;
    /*
    	if(next == NULL)
    		return;
    	if(next == this)
    		return;
    			cout << "---into---" << endl;
    
    		for(list *tmp_next, *tmp = next; tmp != this; tmp = tmp_next)
    		{
    			cout << "---for---" << endl;
    			tmp_next = tmp->next;
    			delete tmp;             //每次delete的时候又调用了~list析构函数,死循环
    
    			cout << "---end for---" << endl;
    		}
    
    			cout << "---out---" << endl;
    		front = NULL;
    		next = NULL;
    		cout << "clean" << endl;
    */	
    /*
    	if(front == NULL)
    		return;
    	if(front == this)
    		return;
    	cout << "---1" << endl;
    		pop();
    //	delete front;
    
    	cout << "---2" << endl;
    */
    FUN_OUT;
    }
    
    //-----------------------------------测试代码-------------------------------------
    /*
    int main()
    {
    FUN_IN;
    	list mylist(10);	
    	mylist.self().output().clean().self().output();
    
    	list mylist2;
    	mylist2.self().output();
    
    	list mylist3(10);
    	mylist3.self().output();
    	mylist3.output(mylist3.find('6'));
    	mylist3.output(mylist3.find('P'));
    	mylist3.output(mylist3.find('q'));
    cout << "-------------------------------" << endl;
    	list list1;
    	cout << list1.empty() << endl;
    	cout << list1.lenght() << endl;
    	list list2(10), list3(20), list4(1);
    	cout << list2.empty() << endl;
    	cout << list2.lenght() << endl;
    	cout << list3.empty() << endl;
    	cout << list3.lenght() << endl;
    	cout << list4.empty() << endl;
    	cout << list4.lenght() << endl;
    cout << "-------------------------------" << endl;
    	list list5;
    	list5.insert(NULL, 'A');
    	list5.insert(list5.find('A'), 'B');
    	list5.insert(list5.find('A'), 'C');
    	list5.insert(list5.find('C'), 'D');
    	list5.output();
    cout << "-------------------------------" << endl;
     	list list6, list7;
    	list6.first_insert('A').first_insert('B').first_insert('C').output();
    	list7.last_insert('X').last_insert('Y').last_insert('Z').output();
    	list6.insert(list6.find('B'), 10, '1');
    	list6.insert(list6.begin(), 10, '2');
    	list6.insert(list6.end(), 10, '3');
    
    		
    	list6.output();
    cout << "-------------------------------" << endl;
    	list list8;
    	list8.insert(NULL, 10, 'C');
    	list8.output();
    	list8.insert(list8.end(), 5, 'D');
    	list8.output().clean().output();
    
    	
    	list list9;
    	list9.first_insert(1, 'A');
    	list9.first_insert(10, 'C');
    	list9.first_insert(10, 'D');
    	list9.last_insert(0, '1');
    	list9.last_insert(5, '5');
    	list9.output();
    	list list10;
    	list10.last_insert(10, '0').first_insert(3, '3').last_insert(5, '1');
    	list10.output();
    cout << "-------------------------------" << endl;
    	list my(10, 'c');
    	my.output();
    	cout << my.lenght() << endl;
    	list you(5, '5');
    	you.output();
    	cout << you.lenght() << endl;
    	cout << my << endl;
    	cout << *my.begin() << endl;
    cout << "-------------------------------" << endl;
    	list my1(10);
    	my1.modify(my1.find('3'), my1.find('1'), 'A');
    	my1();
    	cout << my1 << endl;
    	my1.self();
    	my1.modify();
    	my1();
    	cout << my1 << endl;
    	my1.self();
    cout << "-------------------------------" << endl;
    	list haha(10);
    	haha();
    	haha.remove(haha.find('0'));
    	haha();
    	haha.remove(haha.find('8'));
    	haha();
    	haha.remove(haha.find('Z'));
    	haha();
    	haha.self();
    cout << "-------------------------------" << endl;
    	list ok(80), no;
    	ok();
    	//ok.remove(ok.find('B'), ok.find('Z'));
    	ok();
    	ok.remove(ok.find('n'), ok.find('3'));
    	ok();
    	ok.remove();
    //	ok.first_insert('U');
    	ok.self();
    	cout << "ok()====" << endl;
    	ok();
    	cout << "----no----" << endl;
    	no();
    	no.remove();
    	no();
    
    cout << "-------------------------------" << endl;
    	list len(10);
    	len();
    	len.modify(len.find('8'), len.find('3'), '1');
    	len();
    	//len.remove(len.begin(), len.end(), '1');
    	len.remove('1');
    	len();
    	len.self();
    	list l1, l2(10, 'a'), l3(20);
    	l1.self().output();
    	l2.self().output();
    	l3.self().output();
    	cout << "-----stack------" << endl;
    	list stack;
    	stack();
    	stack.pop();
    	cout << "--1--" << endl;
    	cout << stack.push('1').push('2').push('3').pop().top() << endl;
    	stack();
    	cout << "--2--" << endl;
    
    	stack.push(4).pop().pop().pop();
    	stack();
    	cout << "--3--" << endl;
    	stack.pop();
    	stack();
    
    	cout << "-----queue------" << endl;
    	list queue;
    	cout <<	queue.head() << endl;
    	cout <<	queue.tail() << endl;
    
    	queue.head_into('1').head_into('2').head_into('3');
    	queue();
    	cout <<	queue.head() << endl;
    	cout <<	queue.tail() << endl;
    
    	cout << "--1--" << endl;
    	
    	queue.head_out();
    	queue();
    	cout <<	queue.head() << endl;
    	cout <<	queue.tail() << endl;
    
    	cout << "--2--" << endl;
    	queue.tail_into('a').tail_into('b');
    	queue();
    	cout <<	queue.head() << endl;
    	cout <<	queue.tail() << endl;
    
    	cout << "--3--" << endl;
    	queue.tail_out().tail_out().tail_out();
    	queue();
    	cout <<	queue.head() << endl;
    	cout <<	queue.tail() << endl;
    	
    	cout << "--4--" << endl;
    	queue.tail_out().tail_out().tail_out();
    	queue();
    	cout <<	queue.head() << endl;
    	cout <<	queue.tail() << endl;
    
    		
    	
    	list l1;
    	list l2;
    	l2= l1;
    	l1("l1");
    	l2("l2");
    	list l3(10);
    	l1 = l3;
    	l1("l1");
    	l1 = l2;
    	l1("l1");
    	list l5(50);
    	l1 = l5;
    	l1();
    	
    	list l1, l2;
    	if(l1 > l2)
    		cout << "wrong1" << endl;
    	if(l1 < l2)
    		cout << "wrong2" << endl;
    	if(l1 >= l2)
    		cout << "true" << endl;
    	if(l1 <= l2)
    		cout << "true" << endl;
    
    
    	list l1(10), l2;
    	if(l1 > l2)
    		cout << "true1" << endl;
    	if(l1 < l2)
    		cout << "wrong2" << endl;
    	if(l1 >= l2)
    		cout << "true3" << endl;
    	if(l1 <= l2)
    		cout << "wrong4" << endl;
    
    
    	list l1(100), l2(20, 'c');
    	for(unsigned int i = 0; i < l1.lenght(); ++i)
    		cout << l1[i] << ' ';
    	cout << endl;
    	cout << l1[-1] << endl;
    	cout << l1[999] << endl;
    	cout << l1[l1.lenght()] << endl;
    	for(unsigned int i = 0; i < l2.lenght(); ++i)
    		cout << l2[i] << ' ';
    	cout << endl;
    	cout << l2[-1] << endl;
    	cout << l2[999] << endl;
    	cout << l2[l2.lenght()] << endl;
    
    	char c = 'k';
    	for(unsigned int i = 0; i < l2.lenght(); ++i)
    		l2[i] = c++;
    	for(unsigned int i = 0; i < l2.lenght(); ++i)
    		cout << l2[i] << ' ';
    	cout << endl;
    	cout << l2[-1] << endl;
    	cout << l2[999] << endl;
    	cout << l2[l2.lenght()] << endl;
    	list l1(10);
    //	l1 << "l1" << "cao" >> 'A' >> 'B' << "ok";
    	l1 << 'A' << 'B' >> 'C' >> 'D';
    	l1();
    
    	list l1;
    	cout << l1 << endl; 
    	list l1(10);
    l1();
    
    FUN_OUT;
    }
    */
    list* fun()
    {
    	list l1(10);
    	cout << "1" << endl;
    	list *p = l1.begin();
    	p->self();
    	cout << "2" << endl;
    	return p;
    }
    
    void test()
    {
    	list *p = fun();	
    	cout << "3" << endl;
    	p->self();
    	(*p)("*p");
    	cout << "4" << endl;
    }
    
    void test_swap()
    {
    	list l1,l2;
    	l1("l1");
    	l2("l2");
    	l1.swap(l2);
    	l1("l1");
    	l2("l2");
    	cout << endl;
    
    	list l3,l4(10);
    	l3("l3");
    	l4("l4");
    	l3.swap(l4);
    	l3("l3");
    	l4("l4");
    	cout << endl;
    
    	list l5(10),l6;
    	l5("l5");
    	l6("l6");
    	l5.swap(l6);
    	l5("l5");
    	l6("l6");
    	cout << endl;
    
    	list l7(20, 'c'),l8(10);
    	l7("l7");
    	l8("l8");
    	l7.swap(l8);
    	l7("l7");
    	l8("l8");
    	l7.swap(l8);
    	l7("l7");
    	l8("l8");
    	l7.swap(l8);
    	l7("l7");
    	l8("l8");
    }
    
    void test2()
    {
    	list l1, l2(3);
    	l1("l1");
    	l2("l2");
    	cout << endl;
    	~l1;
    	~l2;
    	l1("l1");
    	l2("l2");
    	cout << endl;
    	~l1;
    	~l2;
    	l1("l1");
    	l2("l2");
    	cout << endl;
    	~l1;
    	~l2;
    	l1("l1");
    	l2("l2");
    	cout << endl;
    
    	(~l1)("l1");
    	(~l1).output();
    
    
    }
    
    void test3()
    {
    	list l1,l2;
    	cout << (l1 == l2) << endl;//1
    	list l11(10),l21;
    	cout << (l11 == l21) << endl;//0
    	list l12(10),l22(10, 'c');
    	cout << (l12 == l22) << endl;//0
    	list l13(10),l23(10);
    	cout << (l13 == l23) << endl;//1
    	l13.push('c');
    	l23.push('c');
    	cout << (l13 == l23) << endl;//1
    	l13.push('a');
    	l23.push('b');
    	cout << (l13 == l23) << endl;//0
    }
    
    void test4()
    {
    	list l1,l2;
    	cout << (l1 != l2) << endl;//0
    	list l11(10),l21;
    	cout << (l11 != l21) << endl;//1
    	list l12(10),l22(10, 'c');
    	cout << (l12 != l22) << endl;//1
    	list l13(10),l23(10);
    	cout << (l13 != l23) << endl;//0
    	l13.push('c');
    	l23.push('c');
    	cout << (l13 != l23) << endl;//0
    	l13.push('a');
    	l23.push('b');
    	cout << (l13 != l23) << endl;//1
    }
    
    void test5()
    {
    	list l2;
    	l2 >>= 2;
    	l2("l2");
    
    	list l1(10);
    	l1("l1");
    	l1 >>= 0;
    	l1("l1");
    	l1 >>= 10;
    	l1("l1");
    	l1 >>= 1;
    	l1("l1");
    	l1 >>= 7;
    	l1("l1");
    	l1 >>= 3;
    	l1("l1");
    	l1 >>= 26;
    	l1("l1");
    }
    
    void test6()
    {
    	list l2;
    	l2 <<= 2;
    	l2("l2");
    
    	list l1(10);
    	l1("l1");
    	l1 <<= 0;
    	l1("l1");
    	l1 <<= 10;
    	l1("l1");
    	l1 <<= 4;
    	l1("l1");
    	l1 <<= 5;
    	l1("l1");
    	l1 <<= 1;
    	l1("l1");
    	l1 <<= 16;
    	l1("l1");
    }
    
    void test7()
    {
    	list l1(5, 'c');
    	list l2(3);
    	
    	l1("l1");
    	for(int i = 0; i < 5; ++i)
    	{
    		l1 += l2;
    		l1("l1");
    	}
    	list l3;
    	for(int i = 0; i < 5; ++i)
    	{
    		l3 += l2;
    		l3("l3");
    	}
    	list l5;
    	list l4;
    	for(int i = 0; i < 5; ++i)
    	{
    		l5 += l4;
    		l5("l5");
    	}
    	list l6(5);
    	list l7;
    	for(int i = 0; i < 5; ++i)
    	{
    		l6 += l7;
    		l6("l6");
    	}
    }
    
    void test8()
    {
    	list l1,l2,l3;
    	l3 = l1 + l2;
    	l3("l3");
    
    	list l4, l5(5, 'c'), l6(10), l7(10, 'a');
    	//l4 = l5 + l6 + l7;//跟设想的不一样
    	l4 = l5 + l6;
    	l4 = l4 + l7;
    	l4("l4");
    	l5("l5");
    	l6("l6");
    	l7("l7");
    }
    void test9()
    {
    	list l1(10);
    	l1("l1");
    
    	list *p = l1.find('5');
    	cout << *p << endl;
    	p = ++p;
    	cout << *p << endl;
    }
    
    void test10()
    {
    	list l0;
    	list l1(10, 'c');
    	list l2(10);
    
    	l0("l0");
    	*l0;
    	l0("l0");
    
    	l1("l1");
    	*l1;
    	l1("l1");
    
    	(~l2)("l2"); 
    	(~l2).output();
    	*l2;
    	l2("l2");
    
    
    }
    void test11()
    {
    	list l1, l2;
    	list l3(10);
    	~(~l3);
    	list l4(5);
    
    	l1("l1");
    	l2("l2");
    	l3("l3");
    	l4("l4");
    
    	l1 -= l2;
    	l3 -= l1;
    	l1 -= l3;
    	l1("l1");
    	l2("l2");
    	l3("l3");
    
    	l3 -= l4;
    	l3("l3");
    }
    
    /*
    void test12()
    {
    	list l1(10), l2(5), l3(10, 'c');
    	~~l1;
    	l1("l1");
    	l2("l2");
    	l3("l3");
    
    	l3 = l1 - l2;
    	l1("l1");
    	l2("l2");
    	l3("l3");
    	
    	list l33(10), l31(3), l32;
    	l32.push('7');
    	l31("l31");
    	l32("l32");
    	l33("l33");
    
    //	l1 = l33 - l31 - l32;//跟设想的不一样
    	l1("l1");
    
    }
    */
    
    void test13()
    {
    	list l1(30);
    	l1("l1");
    	!l1;
    	l1("l1");
    	!l1;
    	l1("l1");
    	!!l1;
    	l1("l1");
    }
    
    void test14()
    {
    	list l1(10);
    	(!l1)("l1");
    	(~l1)("l1");
    	cout << "+++++++++++" << endl;
    	+l1;
    	cout << "+++++++++++" << endl;
    	l1("l1");
    
    
    	(-l1)("l1");
    	
    	list l3(1), l2;
    	(+l3)("l3");
    	(-l3)("l3");
    	(+l2)("l2");
    	(-l2)("l2");
    }
    
    int main()
    {
    //	test();
    //	cout << "5" << endl;
    
    //test_swap();
    
    //	test2();
    //	test3();
    //	test4();
    //	test5();
    //	test6();
    //	test7();
    //test8();
    //test9();
    //test10();
    //test11();
    //test12();
    //test13();
    //test14();
    }
    

  • 相关阅读:
    Java怎样对一个属性设置set或get方法的快捷键
    小程序怎样控制rich-text中的<img>标签自适应
    Java中Arrys数组常用的方法
    Java 怎样实现调用其他方法
    Java保留两位小数
    解决ajax请求跨域
    rand(7) 到rand(10)
    c++生成随机数
    批量该文件名
    正则表达式(=)
  • 原文地址:https://www.cnblogs.com/coolulu/p/4084905.html
Copyright © 2011-2022 走看看