64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef QUEUETP_H_
#define QUEUETP_H_
template<typename Type>
class QueueTP
{
struct Cell
{
Type content;
Cell* pointer;
};
enum{MAX = 100};
private:
Cell* P_start;
Cell* P_end;
int number;
int top;
public:
QueueTP(int t = MAX);
~QueueTP();
bool isempty()const;
bool isfull()const;
bool push(const Type & item);
bool pop(Type & item);
void Show()const;
};
#endif
#include<iostream>
#include"QueueTP.h"
using std::cout;
using std::endl;
//Position 1
template<typename Type>
QueueTP<Type>::QueueTP(int t)
{
P_start = NULL;
P_end = NULL;
number = 0;
top = t;
}
template<typename Type>
QueueTP<Type>::~QueueTP()
{
Cell* P_temp;
while(number > 0)
{
P_temp = P_start;
P_start = P_start->pointer;
delete P_temp;
number--;
}
}
template<typename Type>
bool QueueTP<Type>::isempty() const
{
return number == 0;
}
template<typename Type>
bool QueueTP<Type>::isfull() const
{
return number == top;
}
template<typename Type>
bool QueueTP<Type>::push(const Type &item)
{
if(number >= top)
return false;
//
Cell* add = new Cell;
//
if(add == NULL)
return false;
//
add->content = item;
add->pointer = NULL;
//
if(P_start == NULL)
P_start = add;
else
P_end->pointer = add;
P_end = add;
//
number++;
//
return true;
}
template<typename Type>
bool QueueTP<Type>::pop(Type &item)
{
if(number <= 0)
return false;
//
Cell* P_temp;
//
item = P_start->content;
P_temp = P_start;
P_start = P_start->pointer;
//
delete P_temp;
//
number--;
//
if(number == 0)
P_end = NULL;
//
return true;
}
template<typename Type>
void QueueTP<Type>::Show()const
{
Cell* P_show = NULL;
P_show = P_start;
for(int i = 0;i < number;i++)
{
cout<<i+1<<": "<<P_show->content<<endl;
P_show = P_show->pointer;
}
}
//Position 2
#include<iostream>
#include"QueueTP.h"
int main()
{
QueueTP<char*> mm;
char* dd[10] = {"aaaaaaa","bbbbb",
"ccccccc","ddddd",
"eeeeee","fffffff",
"ggggg","fffff",
"hhhhh","iiiii"};
int i;
for(i=9;i>=0;i--)
{
mm.push(dd[i]);
}
mm.Show();
return 0;
}