定义一个单向链表的类模板,实现节点的增、删、改、查。

#ifndef __T_link_H

#define _T_link_H

using namespace std;

template
typedef struct linkq
{
T Data;
struct linkq *Next;
}link;

template
class linkqueue
{
private:
link *head,*front,*rear;
int num;
public:
linkqueue():front(NULL),rear(NULL)
{
num=0;
head=new link;
head->Next=NULL;
front=head;
}
~linkqueue()
{
link *q=head;
while(q->Next!=NULL)
{
q=head;
head=q->Next;
delete q;
}
}
bool isempty()
{
return num==0;
}
void enqueue(T a)
{
link *node=new link;
node->Next=NULL;
node->Data=a;
if(isempty())
{
front->Next=node;
rear=node;
}
else
{
rear->Next=node;
rear=node;
}
num++;
}
void dequeue()
{
link *node;
node=front->Next;
T a=node->Data;
front->Next=node->Next;
delete node;
if(front->Next==NULL)
{
rear->Next=NULL;
}
num--;
}
T getqueue()
{
return front->Next->Data;
}
};
#endif // __T_link_H