顺序栈
#include
using namespace std;
#define MAX 50
template
class Stack
{
private:
T *data;
int top;
public:
Stack():data(new T[MAX]),top(-1)
{
cout<<"Stack::无参构造函数"< } ~Stack() { delete []data; cout<<"Stack::构析函数"< } Stack(const Stack { if(data!=NULL) { delete[]data; } this->data=new T[MAX]; memcpy(this->data,other.data,sizeof(T)*MAX); this->top=other.top; cout<<"Stack::拷贝构造函数"< } void stack_push(T &&val); void stack_pop(); void stack_clear(); bool stack_empty(); bool stack_full(); T stack_gettop(); int stack_len(); void stack_show(); }; template void Stack { this->top++; this->data[top]=val; } template void Stack { if(NULL==this->data || stack_empty()) { cout<<"所给顺序栈不合法"< } cout< this->top--; } template void Stack { this->top=-1; } template bool Stack { return -1==this->top; } template bool Stack { return MAX-1==this->top; } template T Stack { return this->data[top]; } template int Stack { return this->top+1; } template void Stack { for(int i=top;i>=0;i--) { cout< } cout< } 顺序队列 #include #define MAX 50 using namespace std; template class Queue { private: T *data; T head; T tail; public: Queue():data(new T[MAX]),head((T)0),tail((T)0) { cout<<"queue::无参构造函数"< } ~Queue() { delete []data; cout<<"queue::析构函数"< } Queue(const Queue &other): data(new T(*other.data)), head(other.head), tail(other.tail) { this->data=new T[MAX]; memcpy(this->data,other.data,sizeof(T)*MAX); this->head=other.head; this->tail=other.tail; cout<<"拷贝构造函数"< } void queue_push(T &&val); void queue_pop(); void queue_clear(); bool queue_empty(); bool queue_full(); int queue_len(); void queue_show(); }; template void Queue { data[tail]=val; tail=(tail+1)%MAX; return; } template void Queue { cout< head=(head+1)%MAX; return; } template void Queue { while(!queue_empty()) { queue_pop(); } } template bool Queue { return head==tail; } template bool Queue { return head==(tail+MAX)%MAX; } template int Queue { return (tail+MAX-head)%MAX; } template void Queue { for(int i=head;i!=tail;i=(i+1)%MAX) { cout< } cout< } 文章来源
发表评论