标签:style blog io os sp for on 文件 div
#ifndef SQ_STACK_H
#define SQ_STACK_H
#include<iostream>
using namespace std;
template<class T>
class sq_stack
{
private:
int cap; //总容量
int top; //栈顶
T *s; //指向顺序栈(栈底)
public:
sq_stack(int); //初始化
void print_sq_stack();//打印
int flag_sq_stack(); //检查顺序栈的状态
void push_sq_stack(T);//入栈
T delete_sq_stack(); //出栈
T read_sq_stack(); //读栈顶元素
};
//初始化
template<class T>
sq_stack<T>::sq_stack(int m)
{
cap=m;
s=new T[cap];
top=0;
}
//打印
template<class T>
void sq_stack<T>::print_sq_stack()
{
int i;
for (i=top;i>0;i--)
{
cout<<s[i-1]<<" ";
}
cout<<endl;
}
//检查顺序栈的状态
template<class T>
int sq_stack<T>::flag_sq_stack()
{
if (top==cap) return -1; //栈满
if (top==0) return 0; //栈空
return 1;
}
//入栈
template<class T>
void sq_stack<T>::push_sq_stack(T t)
{
if (top==cap)
{
cout<<"stack overflow"<<endl;
return;
}
top=top+1;
s[top-1]=t;
}
//出栈
template<class T>
T sq_stack<T>::delete_sq_stack()
{
T y;
if (top==0)
{
cout<<"空栈"<<endl;
return 0;
}
y=s[top-1];
top=top-1;
return y;
}
template<class T>
T sq_stack<T>::read_sq_stack()
{
if (top==0)
{
cout<<"空栈"<<endl;
return 0;
}
return s[top-1];
}
#endif
#include "sq_stack.h"
int main()
{
sq_stack<int>s(10);
s.push_sq_stack(50);
s.push_sq_stack(60);
s.push_sq_stack(70);
s.push_sq_stack(80);
s.push_sq_stack(90);
s.push_sq_stack(100);
s.print_sq_stack();
cout<<s.delete_sq_stack()<<endl;
}标签:style blog io os sp for on 文件 div
原文地址:http://blog.csdn.net/jxlijunhao/article/details/41731885