您的位置:首页 > 其它

栈的基本操作

2016-04-28 09:08 267 查看
#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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: