📄 list.h.bak
字号:
# ifndef list_h
# define list_h
# include <iostream.h>
# include "listnode.h"
# include <assert.h>
# include <iomanip.h>
template <class nodetype>
class list
{
public:
list();
~list();
void insertfront(const nodetype &);
void insertback(const nodetype &);
bool removefront(nodetype &);
bool removeback(nodetype &);
nodetype firstvalue() {return firstptr->data;}
bool isempty() const;
void print() const;
private:
listnode<nodetype> *firstptr;
listnode<nodetype> *lastptr;
listnode<nodetype> *getnew(const nodetype &);
};
template <class nodetype>
list<nodetype>::list()
:firstptr(0),lastptr(0) {};
template <class nodetype>
list<nodetype>::~list()
{
if(!isempty())
{
cout<<"destroying nodes"<<endl;
listnode<nodetype> *currentptr=firstptr,*tempptr;
while(currentptr!=0)
{
tempptr=currentptr;
currentptr=currentptr->nextptr;
delete tempptr;
}
}
}
template <class nodetype>
void list<nodetype>::insertfront(const nodetype & value)
{
listnode<nodetype> *newptr=getnew(value);
if(isempty())
firstptr=lastptr=newptr;
else
{
newptr->nextptr=firstptr;
firstptr=newptr;
}
}
template <class nodetype>
void list<nodetype>::insertback(const nodetype & value)
{
listnode<nodetype> *newptr=getnew(value);
if(isempty())
firstptr=lastptr=newptr;
else
{
lastptr->nextptr=newptr;
lastptr=newptr;
newptr->nextptr=NULL;
}
}
template <class nodetype>
bool list<nodetype>::removefront( nodetype & value)
{
if(isempty())
return false;
else
{
listnode<nodetype> *tempptr;
value=firstptr->data;
tempptr=firstptr;
firstptr=firstptr->nextptr;
delete tempptr;
return true;
}
}
template <class nodetype>
bool list<nodetype>::removeback( nodetype & value)
{
if(isempty())
return false;
else
{
listnode<nodetype> *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;
}
}
template <class nodetype>
bool list<nodetype>::isempty() const
{
return firstptr==NULL;
}
template <class nodetype>
void list<nodetype>::print() const
{
listnode<nodetype> *currentptr=firstptr;
while(currentptr!=NULL)
{
cout<<currentptr->data<<setw(6);
currentptr=currentptr->nextptr;
}
cout<<endl;
}
template <class nodetype>
listnode<nodetype> *list<nodetype>::getnew(const nodetype & value)
{
listnode<nodetype> *ptrnew=new listnode<nodetype>(value);
assert(ptrnew);
return ptrnew;
}
# endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -