栈的基本操作

问题及代码:

/*
 *copyright(c) 2016烟台大学计算机学院
 *All rights reserved
 *文件名称:test.cpp
 *作者:杨昊
 *版本:v6.0
 *
 *问题描述: 栈的基本操作
 *输入描述:无
 *程序输出:
*/


#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;
}
void Push(SqStack *&s,ElemType x)  //把x元素压入栈
{
    s->top++;
    s->data[s->top]=x;
}
int Length(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 Print(SqStack *s)    // //输出栈
{
    PrintStack(s);
}
void DestroyStack(SqStack *&s)//销毁栈
{
    free(s);
}


 

你可能感兴趣的:(栈的基本操作)