基本操作:入栈
时间: 1ms 内存:128M
描述:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
#define SizeMax 105
typedef char ElemType;
typedef struct
{
ElemType data[SizeMax];
int top;
} SqStack;void InitStack(SqStack *&s)
{
s=(SqStack*)malloc(sizeof(SqStack));
memset(s->data,0,sizeof(SqStack));
s->top=-1;
}
int StackEmpty(SqStack *s)
{
return s->top==-1;
}int Legth(SqStack *s)
{
return s->top+1;
}
void PrintStack(SqStack *s)
{
for(int i=(int)strlen(s->data)-1; i>=0; i--)
printf("%c",s->data[i]);
printf("\n");
}void DestroyStack(SqStack *&s)
{
free(s);
}
int main()
{
SqStack *s;
InitStack(s); //初始化栈
ElemType a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
Push(s,a); //入栈
Push(s,b);
Push(s,c);
Push(s,d);
Push(s,e);
printf("栈的长度为%d\n",Legth(s)); //输出栈的长度
PrintStack(s); //输出从栈顶到栈底的元素
DestroyStack(s); //释放栈
return 0;
}
实现顺序栈的入栈操作。 void Push(SqStack *&s,ElemType x) 为将元素x入栈s中。假设顺序表的元素类型为char,主函数及其他函数已给出。
注意:只提交 void Push(SqStack *&s,ElemType x)部分。
输入:
输入入栈的5个元素
输出:
输出栈的长度
输出从栈顶到栈底的元素
示例输入:
a b c d e
示例输出:
栈的长度为5
edcba
提示:
参考答案:
文章评论