棧的順序儲存結構 順序棧(Sequential Stack) C++
阿新 • • 發佈:2018-11-11
seqStack.h
#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__
const int size = 10;
template <class T>
class seqStack
{
public:
seqStack(void);
virtual ~seqStack(void);
public:
void push(T _data);
T pop();
T getTop();
bool isEmpty();
private:
T stackData[size];
int top;
};
#endif
seqStack.cpp
#include "seqStack.h"
template <class T>
seqStack<T>::seqStack(void)
{
top = -1;
}
template <class T>
void seqStack<T>::push(T _data)
{
stackData[++top] = _data;
}
template <class T>
T seqStack<T>::pop()
{
T tempData = stackData[top];
top--;
return tempData;
}
template <class T>
T seqStack<T>::getTop()
{
return stackData[top];
}
template <class T>
bool seqStack<T>::isEmpty()
{
top != -1?return 0:return 1;
}
template <class T>
seqStack<T>::~seqStack(void)
{
}
stack.cpp
#include <iostream>
#include <string>
#include "seqStack.h"
#include "seqStack.cpp"
using namespace std;
int main()
{
seqStack<string> stack;
stack.push("hello");
cout << stack.pop() << endl;
stack.push("world");
cout << stack.getTop() << endl;
if (stack.isEmpty())
cout << "Not Empty" << endl;
else
cout << "Empty" << endl;
return 0;
}