Sequential Stock

1991 ワード

#ifndef STRUCT_STACK_H
#define STRUCT_STACK_H

template 
class structstrack{
public:
	virtual void Push(const T&x)=0;
	virtual bool Pop(T&x)=0;
	virtual bool getTop(T&x)const=0;
	virtual bool IsEmpty()const=0;
	virtual bool IsFull()const=0;
	virtual int getSize()const=0;
};
#endif


// StackStruct.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "StructStack.h"
#include
#include
using namespace std;

const int stackIncreament=20;

template 
class SeqStack:public structstrack{
public:
	SeqStack(int sz=50);
	~SeqStack(){delete[]elements;}
	void Push(const T&x);
	bool Pop(T&x);
	bool getTop(T&x)const;
	bool IsEmpty()const{return(top==-1)?true:false;}
	bool IsFull()const{return(top==maxSize-1)?true:false;}
	int getSize()const{return top+1;}
	friend ostream&operator<<(ostream&os,SeqStack&s);
private:
	T *elements;
	int top;
	int maxSize;
	void overflowProcess();
};

template
SeqStack::SeqStack(int sz):top(-1),maxSize(sz){
	elements=new T[maxSize];
	assert(elements!=NULL);
}

template
void SeqStack::overflowProcess(){
	T *newArray=new T[maxSize+stackIncreament];
	if(newArray==NULL){
		cerr<
void SeqStack::Push(const T&x){
	if(IsFull()==true)overflowProcess();
	elements[++top]=x;
}

template
bool SeqStack::Pop(T&x){
	if(IsEmpty()==true)return false;
	x=elements[top--];
	return true;
}

template
bool SeqStack::getTop(T&x)const{
	if(IsEmpty()==true)return false;
	x=elements[top];
	return true;
}

template 
ostream&operator<&s){
	os< ss;
	int tmp=0;
	for(int i=0;i<=5;++i){
		cin>>tmp;
		ss.Push(tmp);
	}
	cout<