3个回答
展开全部
#include <iostream>
using namespace std;
template<typename Type>
class Stack
{
public:
Stack(int iLenth);
~Stack(){delete[] m_p;}
Type Pop();
void Push(Type newData);
Type GetData();
private:
int m_iMaxLenth;
int m_iCurLenth;
Type *m_p;
};
template<typename Type>
Stack<Type>::Stack(int iLenth)
: m_iMaxLenth(iLenth)
, m_iCurLenth(0)
{
m_p = new Type[m_iMaxLenth];
if (NULL == m_p)
throw(1);
}
template<typename Type>
Type Stack<Type>::Pop()
{
if (NULL == m_p)
throw(2);
if (0 == m_iCurLenth)
throw(3);
Type k = m_p[--m_iCurLenth];
return k;
}
template<typename Type>
void Stack<Type>::Push(const Type newData)
{
if (NULL == m_p)
throw(2);
if (m_iCurLenth == m_iMaxLenth)
throw(4);
m_p[m_iCurLenth++] = newData;
}
template<typename Type>
Type Stack<Type>::GetData()
{
if (NULL == m_p)
throw(2);
if (0 == m_iLenth)
throw(3);
return m_p[m_iCurLenth-1];
}
int _tmain(int argc, _TCHAR* argv[])
{
Stack<int> s(10);
for(int i=0; i<10; i++)
{
s.Push(i);
}
for(int i=0; i<10; i++)
{
cout << s.Pop() << endl;
}
}
using namespace std;
template<typename Type>
class Stack
{
public:
Stack(int iLenth);
~Stack(){delete[] m_p;}
Type Pop();
void Push(Type newData);
Type GetData();
private:
int m_iMaxLenth;
int m_iCurLenth;
Type *m_p;
};
template<typename Type>
Stack<Type>::Stack(int iLenth)
: m_iMaxLenth(iLenth)
, m_iCurLenth(0)
{
m_p = new Type[m_iMaxLenth];
if (NULL == m_p)
throw(1);
}
template<typename Type>
Type Stack<Type>::Pop()
{
if (NULL == m_p)
throw(2);
if (0 == m_iCurLenth)
throw(3);
Type k = m_p[--m_iCurLenth];
return k;
}
template<typename Type>
void Stack<Type>::Push(const Type newData)
{
if (NULL == m_p)
throw(2);
if (m_iCurLenth == m_iMaxLenth)
throw(4);
m_p[m_iCurLenth++] = newData;
}
template<typename Type>
Type Stack<Type>::GetData()
{
if (NULL == m_p)
throw(2);
if (0 == m_iLenth)
throw(3);
return m_p[m_iCurLenth-1];
}
int _tmain(int argc, _TCHAR* argv[])
{
Stack<int> s(10);
for(int i=0; i<10; i++)
{
s.Push(i);
}
for(int i=0; i<10; i++)
{
cout << s.Pop() << endl;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以参考stl的vector
push_back()
pop_back()
back()
push_back()
pop_back()
back()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以前做的
参考一下吧
#include<iostream.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status ;
typedef int ElemType;
struct STACK
{
ElemType *base;
ElemType *top;
int stacksize;
};
typedef struct STACK SqStack;
//typedef struct STACK *pSqstack;
//函数声明
Status InitStack(SqStack &S);
void DestroyStack(SqStack &S);
void ClearStack(SqStack &S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,ElemType &e);
Status Push(SqStack &S,ElemType e);
Status Pop(SqStack &S,ElemType &e);
//初始化
Status InitStack(SqStack &S)
{
S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base) return ERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
//销毁栈
void DestroyStack(SqStack &S)
{
free(S.base);
}
//清空栈
void ClearStack(SqStack &S)
{
S.top=S.base;
}
//判栈空
Status StackEmpty(SqStack S)
{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
//求栈中元素个数
int StackLength(SqStack S)
{
int i;
ElemType *p;
i=0;
p=S.top;
while(p!=S.base)
{p--;
i++;
}
return i;
}
//获取栈顶元素值
Status GetTop(SqStack S,ElemType &e)
{
if(S.top==S.base) return ERROR;
e=*(S.top-1);
return OK;
}
//入栈
Status Push(SqStack &S,ElemType e)
{
if(S.top - S.base>=S.stacksize)
{
S.base=(ElemType *) realloc(S.base,
(S.stacksize + STACKINCREMENT) * sizeof(ElemType));
if(!S.base) return ERROR;
S.top=S.base+S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top++)=e;
return OK;
}
//出栈
Status Pop(SqStack &S,ElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
//逆序打印栈中元素
void PrintElem(SqStack S)
{
ElemType e;
if(S.top==S.base) cout<<"该栈为空栈.";
else
while(S.top!=S.base)
{
Pop(S,e);cout<<e<<ends;
}
cout<<endl;
}
//进制转换函数
void conversion()
{
SqStack S;
ElemType e;
char b[17]="0123456789ABCDEF";
int n,r;
InitStack(S);
cout<<"Input a number to convert to :\n";
cin>>n;
cout<<"请输入要转换的进制:";
cin>>r;
if(n<0)
{
cout<<"\nThe number must be over 0.";
return;
}
if(!n) Push(S,0);
while(n){
Push(S,n%r);
n=n/r;
}
cout<<"the result is: ";
while(!StackEmpty(S)){
Pop(S,e);
cout<<b[e];
}
cout<<endl;
}
void main()
{
ElemType e;
SqStack Sa;
cout<<"\n\n-------------------SqStack Demo is running...----------------\n\n";
cout<<"First is Push function.\n";
InitStack(Sa);
cout<<" Now Stack is Empty.\n";
cout<<"请输入第一个要入栈的元素值:";
cin>>e;
Push(Sa,e);
cout<<"\n Now Stack has one element.\n";
PrintElem(Sa);
cout<<"请输入第二个要入栈的元素值:";
cin>>e;
Push(Sa,e);
cout<<"\n Now Stack has another element.\n";
PrintElem(Sa);
int n;
cout<<"请输入还要入栈的元素个数:";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\n请输入第"<<i+2<<"个元素:";
cin>>e;
Push(Sa,e);
}
cout<<"\n Now Pop Stack,the top elem put into variable e.\n";
Pop(Sa,e);
cout<<e<<endl;
cout<<" Let's see the left of Stack's elem:\n";
PrintElem(Sa);
cout<<"现在栈中元素个数为:"<<StackLength(Sa);
cout<<endl;
getch();
cout<<"进制转换演示:\n";
conversion();
}
参考一下吧
#include<iostream.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status ;
typedef int ElemType;
struct STACK
{
ElemType *base;
ElemType *top;
int stacksize;
};
typedef struct STACK SqStack;
//typedef struct STACK *pSqstack;
//函数声明
Status InitStack(SqStack &S);
void DestroyStack(SqStack &S);
void ClearStack(SqStack &S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,ElemType &e);
Status Push(SqStack &S,ElemType e);
Status Pop(SqStack &S,ElemType &e);
//初始化
Status InitStack(SqStack &S)
{
S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base) return ERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
//销毁栈
void DestroyStack(SqStack &S)
{
free(S.base);
}
//清空栈
void ClearStack(SqStack &S)
{
S.top=S.base;
}
//判栈空
Status StackEmpty(SqStack S)
{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
//求栈中元素个数
int StackLength(SqStack S)
{
int i;
ElemType *p;
i=0;
p=S.top;
while(p!=S.base)
{p--;
i++;
}
return i;
}
//获取栈顶元素值
Status GetTop(SqStack S,ElemType &e)
{
if(S.top==S.base) return ERROR;
e=*(S.top-1);
return OK;
}
//入栈
Status Push(SqStack &S,ElemType e)
{
if(S.top - S.base>=S.stacksize)
{
S.base=(ElemType *) realloc(S.base,
(S.stacksize + STACKINCREMENT) * sizeof(ElemType));
if(!S.base) return ERROR;
S.top=S.base+S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top++)=e;
return OK;
}
//出栈
Status Pop(SqStack &S,ElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
//逆序打印栈中元素
void PrintElem(SqStack S)
{
ElemType e;
if(S.top==S.base) cout<<"该栈为空栈.";
else
while(S.top!=S.base)
{
Pop(S,e);cout<<e<<ends;
}
cout<<endl;
}
//进制转换函数
void conversion()
{
SqStack S;
ElemType e;
char b[17]="0123456789ABCDEF";
int n,r;
InitStack(S);
cout<<"Input a number to convert to :\n";
cin>>n;
cout<<"请输入要转换的进制:";
cin>>r;
if(n<0)
{
cout<<"\nThe number must be over 0.";
return;
}
if(!n) Push(S,0);
while(n){
Push(S,n%r);
n=n/r;
}
cout<<"the result is: ";
while(!StackEmpty(S)){
Pop(S,e);
cout<<b[e];
}
cout<<endl;
}
void main()
{
ElemType e;
SqStack Sa;
cout<<"\n\n-------------------SqStack Demo is running...----------------\n\n";
cout<<"First is Push function.\n";
InitStack(Sa);
cout<<" Now Stack is Empty.\n";
cout<<"请输入第一个要入栈的元素值:";
cin>>e;
Push(Sa,e);
cout<<"\n Now Stack has one element.\n";
PrintElem(Sa);
cout<<"请输入第二个要入栈的元素值:";
cin>>e;
Push(Sa,e);
cout<<"\n Now Stack has another element.\n";
PrintElem(Sa);
int n;
cout<<"请输入还要入栈的元素个数:";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\n请输入第"<<i+2<<"个元素:";
cin>>e;
Push(Sa,e);
}
cout<<"\n Now Pop Stack,the top elem put into variable e.\n";
Pop(Sa,e);
cout<<e<<endl;
cout<<" Let's see the left of Stack's elem:\n";
PrintElem(Sa);
cout<<"现在栈中元素个数为:"<<StackLength(Sa);
cout<<endl;
getch();
cout<<"进制转换演示:\n";
conversion();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询