⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.h

📁 人工智能中的深度搜索的九宫问题 用VC实现
💻 H
字号:
# ifndef clist_h
# define clist_h
# include <iostream>
# include "node.h"
# include <assert.h>
# include <iomanip>
# include <string>
using namespace std;
class clist
{
	friend class cjgwt;
public:
	clist();
	~clist();
	void insertfront(const string &);
	void insertfront(cnode *);
	void insertback(const string &);
	void insertback(cnode *);
	cnode *removefront();
	bool removeback(string &);
	string firstvalue()  {return firstptr->data;}
	bool isempty() const;
	void print() const;
private:
	cnode *firstptr;
	cnode *lastptr;
	cnode *getnew(const string &);
};
clist::clist()
:firstptr(0),lastptr(0) {};
clist::~clist()
{
	if(!isempty())
	{
		//cout<<"destroying nodes"<<endl;
		cnode *currentptr=firstptr,*tempptr;
		while(currentptr!=0)
		{
			tempptr=currentptr;
			currentptr=currentptr->nextptr;
			delete tempptr;
		}

	}
}
void clist::insertfront(const string & value)
{
	cnode *newptr=getnew(value);
	if(isempty())
		firstptr=lastptr=newptr;
	else
	{
		newptr->nextptr=firstptr;
		firstptr=newptr;
	}
}
void clist::insertfront(cnode * value)
{
	if(isempty())
		firstptr=lastptr=value;
	else
	{
		value->nextptr=firstptr;
		firstptr=value;
	}
}
void clist::insertback(const string & value)
{
	cnode *newptr=getnew(value);
	if(isempty())
		firstptr=lastptr=newptr;
	else
	{
		lastptr->nextptr=newptr;
		lastptr=newptr;
		newptr->nextptr=NULL;
	}
}
void clist::insertback(cnode *value)
{
	if(isempty())
		firstptr=lastptr=value;
	else
	{
		lastptr->nextptr=value;
		lastptr=value;
		value->nextptr=NULL;
	}
}
cnode *clist::removefront()
{
	if(isempty())
		//return false;
	{}
	else
	{
		cnode *tempptr;
		//value=firstptr->data;
		tempptr=firstptr;
		firstptr=firstptr->nextptr;
		//delete tempptr;
		//return true;
		return tempptr;
	}
}
bool clist::removeback( string & value)
{
	if(isempty())
		return false;
	else
	{
		cnode *currentptr=firstptr,*tempptr=lastptr;
		if(firstptr==lastptr)
			firstptr=lastptr=NULL;
		else{
		while(currentptr->nextptr!=lastptr)
			currentptr=currentptr->nextptr;
        lastptr=currentptr;
		currentptr->nextptr=NULL;
		}
		value=tempptr->data;
		delete tempptr;
		return true;
	}
}
bool clist::isempty() const
{
	return firstptr==NULL;
}
void clist::print() const
{
	cnode *currentptr=firstptr;
	while(currentptr!=NULL)
	{
		cout<<currentptr->data<<setw(6);
		currentptr=currentptr->nextptr;
	}
	cout<<endl;
}
cnode *clist::getnew(const string & value)
{
	cnode *ptrnew=new cnode(value);
	assert(ptrnew);
	return ptrnew;
}
# endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -