#define Stack_
template<class T>
class Stack {
//春慎银 LIFO objects
public:
Stack(int MaxStackSize = 10)
~Stack() {delete [] stack}
bool IsEmpty() const {return top == -1}
bool IsFull() const {return top == MaxTop}
T Top() const
Stack<T>&Add(const T&x)
Stack<扒宴T>&Delete(T&x)
private:
int top // current top of stack
int MaxTop// max value for top
T *stack /孝迅/ element array
}
template<class T>
Stack<T>::Stack(int MaxStackSize)
{// Stack constructor.
MaxTop = MaxStackSize - 1
stack = new T[MaxStackSize]
top = -1
}
template<class T>
T Stack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds()// Top fails
return stack[top]
}
template<class T>
Stack<T>&Stack<T>::Add(const T&x)
{// Add x to stack.
if (IsFull()) throw NoMem()// add fails
stack[++top] = x
return *this
}
template<class T>
Stack<T>&Stack<T>::Delete(T&x)
{// Delete top element and put in x.
if (IsEmpty()) throw OutOfBounds()// delete fails
x = stack[top--]
return *this
}
#include <stdio.h>#include <stdlib.h>缺物
#define MAX 1024///栈使用数组模拟,MAX是最大元素个数
typedef int DataType ///数据域使用整形
typedef struct _stack
{
DataType data[MAX]//悄扮高/存放数据
int top ///栈顶指针
}stack
///初始化
int initStack(stack (*s))
{
return emptyStack(s)
}
///数据压栈,成功返回1,失败返回0
int push(stack (*s), DataType d)
{
if ((*s).top >= MAX - 1) //栈已满
{
printf("栈已满,不能压栈\n")
return 0
}
//数据压栈
(*s).top++
(*s).data[(*s).top] = d
return 1
}
///数据出栈,成功返回1,d指向的区启尺域存储d出的数据,失败返回0
int pop(stack (*s), DataType *d)
{
if ((*s).top <= -1)
{
printf("栈为空,不能出栈\n")
return 0
}
//出栈
*d = (*s).data[(*s).top]
(*s).top--
return 1
}
///清空栈
int emptyStack(stack (*s))
{
(*s).top = -1
return 1
}
int main(int argc, char** argv)
{
stack s
int i, d
initStack(&s)
//压栈
for (i = 0i <1025i++)
{
push(&s, i)
}
//清空
emptyStack(&s)
for (i = 0i <10i++)
{
push(&s, i)
}
//出栈
for (i = 0i <11i++)
{
pop(&s, &d)
printf("%d\n", d)
}
return 0
}
这样行不?:
class CStack{
public:
CStack()//建立虚高一个10个元素的栈
CStack(int s)//建立一个具有 s个元素的栈
CStack(CStack &r_s)//注意,没有重载赋值 *** 作符
int get(int index)//返回下标为index 的栈元素
void push(int n)//进栈,top加1,把n的值存入栈顶
int isEmpty()//判断栈是否为空,空则返回1,否则返回0
int isFull()//判断栈是否是满的,空则返回1,否则返回0
int pop()//出栈,返回栈顶元素,top减1
~CStack()//析构函数,释放毁哗在构造时申请的空间
private:
int *a
int size//栈的大小
int top//指向栈顶
}
#include <iostream>
using namespace std
void PopAll(CStack &stack)
{
while (!stack.isEmpty())
cout << stack.pop() << " "
cout << endl
}
CStack::CStack() :size(10), top(-1)
{
cout << "Constructor" << endl
a = new int[size]
}
CStack::CStack(int s) : size(s), top(-1)
{
cout << "Constructor" << endl
a = new int[size]
}
CStack::CStack(CStack &r_s) : size(r_s.size), top(r_s.top)
{
cout << "copy Constructor" << endl
a = new int[size]
memcpy(a, r_s.a, size * sizeof(int))
}
CStack::~CStack(){ PopAll(*this)cout << "Distructor" << endl delete[] a }
int CStack::get(int index){
if (index >= 0 && index < top)
return a[index]
else return -1//-1作为返回错误?还是抛出异常?
}
void CStack::push(int n){
if (!isFull())
a[++top] = n
}
int CStack::pop(){
if (!isEmpty())
return a[top--]
}
int CStack::isEmpty(){
if (-1 == top) return 1
else return 0
}
int CStack::isFull(){
if (top + 1 == size) return 1
else return 0
}
#include <iostream>
using namespace std
void GetNDataToStack(CStack &stack, unsigned int n)
{
while (n--)
{
int DataGet
cin >> DataGet
stack.push(DataGet)
}
}
int main()
{
int 纤誉行nLoop
cout << "输入要构造几组数据:"
cin >> nLoop
for (int index = 0 index < nLoop ++index)
{
cout <<"输入要多少个数据存储:"
unsigned int nDataCount
cin >> nDataCount
CStack stack(nDataCount)
GetNDataToStack(stack, nDataCount)
}
return 0
}
运行效果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)