📄 linklist.h
字号:
#include "iostream.h"
enum compare{BIG,SMALL};
template <class ElementType>
struct LinkListNode
{
ElementType value;
LinkListNode *next; //next node
LinkListNode *front;//front node
};
template <class Type>
class LHList
{
private:
int count;
LinkListNode<Type> *head;
LinkListNode<Type> *tail;
LinkListNode<Type> *t;
public:
LHList();
~LHList();
bool isEmpty(); //Judge the List is empty or not.
bool appendNode(Type item); //在后面追加
Type getLastNodeValue();
void clear();
int getCount();
Type getNextValue();
void printListorder();
void printListAthwartorder();
};
template <class Type>
LHList<Type>::LHList()
{
count=0;
head=NULL;
tail=NULL;
}
template <class Type>
LHList<Type>::~LHList()
{
clear();
}
template <class Type>
void LHList<Type>::clear()
{
LinkListNode<Type> *temp;
while(head!=NULL)
{
temp=head;
head=head->next;
delete temp;
}
count=0;
head=NULL;
tail=NULL;
}
template <class Type>
void LHList<Type>::printListorder()
{
LinkListNode<Type> *temp=head;
while(temp!=NULL)
{
cout<<temp->value<<"\t";
temp=temp->next;
}
}
template <class Type>
void LHList<Type>::printListAthwartorder()
{
LinkListNode<Type> *temp=tail;
int c;
int sum=1;
bool key=false;
while(temp!=NULL)
{
if(temp->value!=0)
{
cout<<temp->value;
if(temp->front!=NULL)
cout<<",";
key=true;
temp=temp->front;
break;
}
temp=temp->front;
}
while(temp!=NULL)
{
c=0;
if(temp->value<1000) //只要不是四位就进行判断,差几位补几个0
{
for(int i=1;i<4;i++)
{
if((temp->value/sum)<10)//判断是几位数
{
c=4-i;
break;
}
sum=sum*10;
}
}
switch(c)
{
case 0: cout<<temp->value;break; ///输出是判断是几位数 以四位为标准不够的前面添0
case 1: cout<<"0"<<temp->value;break;
case 2: cout<<"00"<<temp->value;break;
case 3: cout<<"000"<<temp->value;break;
}
temp=temp->front;
if(temp!=NULL)
cout<<",";
}
if(!key)
cout<<"0";
}
template <class Type>
bool LHList<Type>::appendNode(Type item)
{
LinkListNode<Type> *node=new LinkListNode<Type> ;
if(node!=NULL)
{
node->value=item;
node->next=NULL;
if(head==NULL)
{
head=node;
t=head;
head->front=NULL;
}
else
{
node->front=tail;
tail->next=node;
}
tail=node;
count++;
return true;
}
return false;
}
template <class Type>
Type LHList<Type>::getLastNodeValue()
{
Type templ;
if(tail!=NULL)
{
templ=tail->value;
tail=tail->front;
if(tail!=NULL)
{
delete tail->next;
count--;
tail->next=NULL;
}
else head=NULL;
}
else templ=0;
return templ;
}
template <class Type>
int LHList<Type>::getCount()
{
return count;
}
template <class Type>
Type LHList<Type>::getNextValue()
{
Type templ;
if(t!=NULL)
{
templ=t->value;
t=t->next;
return templ;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -