C++模板化堆栈类的代码

在研发过程中,将写内容过程中经常用到的一些内容段做个备份,如下内容是关于C++模板化堆栈类的内容,应该对小伙伴有较大用途。

#include<iostream>
#include<cstdlib>
#define default_value 10
using namespace std;

template< class T > class Stack
{
public:
{delete [] values;}
bool push( T );
T pop();
bool isEmpty();
bool isFull();
private:
int size;
int index;

};

template< class T > Stack<T>::Stack(int x):
values(new T[size]),
index(-1)

template< class T > bool Stack<T>::isFull()
{
if((index + 1) == size )
return 1;
else
return 0;
}

template< class T > bool Stack<T>::push(T x)
{
bool b = 0;
if(!Stack<T>::isFull())
{
index += 1;
values[index] = x;
b = 1;
}
return b;
}

template< class T > bool Stack<T>::isEmpty()
{
return 1;
else
}

template< class T > T Stack<T>::pop()
{
T val = -1;
if(!Stack<T>::isEmpty())
{
val = values[index];
index -= 1;
}
else
{
cerr << "Stack is Empty : ";
}
return val;

}

int main()
{
Stack <double> stack1;
Stack <int> stack2(5);
int y = 1;
double x = 1.1;
int i, j;
cout << "n pushed values into stack1: ";

{
else
cout << "n Stack1 is full: ";
}

cout << "nn popd values from stack1 : n";
for( j = 1 ; j <= 11 ; j++)
cout << stack1.pop() << endl;



cout << "n pushd values into stack2: ";

{
else
cout << "n Stack2 is full: ";
}

cout << "nn popd values from stack2: n";
for( j = 1 ; j <= 6 ; j++)
cout << stack2.pop() << endl;
cout << endl << endl;
return 0;
}





猜你喜欢

转载自www.cnblogs.com/mantiger/p/10317936.html
今日推荐