📄 list.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;
void sort();
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;
}
void clist::sort()// 按估价值排序
{
int num=0;
cnode *currptr=this->firstptr;
while(currptr!=NULL)
{
num++;
currptr=currptr->nextptr;
}
for(int i=0;i<num-1;i++)
{
currptr=this->firstptr;
cnode *lefptr=currptr,*temp=NULL,*rigptr=currptr->nextptr;
for(int j=0;j<num-1-i;j++)
{
if(currptr==firstptr && rigptr==lastptr)
{
if(currptr->getFn()>rigptr->getFn())
{
temp=lastptr;
rigptr->nextptr=currptr;
currptr->nextptr=NULL;
firstptr=temp;
lastptr=currptr;
continue;
}
}
else if(currptr==firstptr)
{
if(currptr->getFn()>rigptr->getFn())
{
temp=rigptr;
currptr->nextptr=rigptr->nextptr;
rigptr->nextptr=currptr;
firstptr=temp;
rigptr=currptr;
lefptr=currptr=temp;
}
rigptr=rigptr->nextptr;
currptr=currptr->nextptr;
}
else if(rigptr==lastptr)
{
if(currptr->getFn()>rigptr->getFn())
{
temp=rigptr;
rigptr->nextptr=currptr;
currptr->nextptr=NULL;
lefptr->nextptr=rigptr;
lastptr=currptr;
continue;
}
}
else
{
if(currptr->getFn()>rigptr->getFn())
{
temp=rigptr;
currptr->nextptr=rigptr->nextptr;
rigptr->nextptr=currptr;
lefptr->nextptr=rigptr;
rigptr=currptr;
currptr=temp;
}
rigptr=rigptr->nextptr;
currptr=currptr->nextptr;
lefptr=lefptr->nextptr;
}
}
}
}
# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -