(基于C语言的数据结构)顺序栈的基本操作和简单习题(数值转换、括号匹配)持续更新中
stack.h
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1;
#define FALSE -1;
typedef int SElemType;
typedef struct {
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
int InitStack(SqStack &S);
int DestroyStack(SqStack &S);
int ClearStack(SqStack &S);
int EmptyStack(SqStack S);
int LengthStack(SqStack S);
int GetTop(SqStack S);
int Push(SqStack &S, SElemType e);
int Pop(SqStack &S, SElemType &e);
int visit(SqStack S);
stack.cpp
#include "stack.h"
//构造一个空栈
int InitStack(SqStack &S) {
S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S.base) return FALSE;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
//销毁栈S
int DestroyStack(SqStack &S) {
free(S.base);
S.base = NULL;
S.top = NULL;
S.stacksize = 0;
return OK;
}
//清空栈
int ClearStack(SqStack &S) {
S.top = S.base;
return OK;
}
//判空栈
int EmptyStack(SqStack S) {
if(S.top == S.base) return OK;
return FALSE;
}
//求栈的长度;
int LengthStack(SqStack S) {
if(!S.base) return FALSE;
return (S.top - S.base);
}
//返回栈顶元素
int GetTop(SqStack S) {
if(!S.base || S.top == S.base) return FALSE;
return *(S.top - 1);
}
//插入新的栈顶元素
int Push(SqStack &S, SElemType e) {
if(S.top - S.base >= S.stacksize) {
S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!S.base) return FALSE;
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
//删除栈顶元素
int Pop(SqStack &S, SElemType &e) {
if(!S.base || S.top == S.base) return FALSE;
e = *(--S.top);
return OK;
}
//打印出栈中的元素
int visit(SqStack S) {
if(!S.base || S.top == S.base) return FALSE;
while(S.top > S.base) {
printf("%d ",*--S.top);
}
printf("\n");
return OK;
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main(){
//栈的简单测试
/* SqStack S;
SElemType e;
int length;
InitStack(S);
printf("%d\n",EmptyStack(S));
for(int i = 1; i < 11; i++) {
Push(S, i);
}
printf("%d\n",EmptyStack(S));
visit(S);
length = LengthStack(S);
printf("length = %d\n",length);
GetTop(S, e);
printf("e = %d\n",e);
for(int i = 0; i <3; i++) {
Pop(S, e);
printf("e = %d\n",e);
}
length = LengthStack(S);
printf(" length = %d\n",length);
visit(S);
ClearStack(S);
printf("%d\n",EmptyStack(S));
DestroyStack(S);
printf("%d ",DestroyStack(S));
return 0;*/
//数制转换
/* SqStack S;
InitStack(S);
int N;
int M;
int e;
printf("请输入一个非负十进制数:");
scanf("%d",&N);
printf("请输入想要将该十进制数转换为多少进制数:");
scanf("%d",&M);
while(N) {
Push(S, N%M);
N = N/M;
}
while(EmptyStack(S) == -1) {
Pop(S, e);
printf("%d",e);
}
return 0;*/
//括号匹配的检验
/* char p[10], *s = p;
printf("请输入一串带括号字符串\n");
scanf("%s",s);
SqStack S;
InitStack(S);
int flag = 1;
int e;
while(*s != '#' && flag) {
switch(*s){
case '(':
case '[':
case '{':Push(S, *s);
break;
case ')':if(EmptyStack(S) == -1 && GetTop(S) == '(')
Pop(S, e);
else flag = 0;
break;
case ']':if(EmptyStack(S) == -1 && GetTop(S) == '[')
Pop(S, e);
else flag = 0;
break;
case '}':if(EmptyStack(S) == -1 && GetTop(S) == '{')
Pop(S, e);
else flag = 0;
break;
}
s++;
}
if(*s =='#' && EmptyStack(S) == -1 || flag == 0) printf("匹配失败");
else printf("匹配成功");
return 0; */
}