栈(Stack)是一个特殊的线性表,是限定仅在一端进行插入喝删除操作的线性表。
先进后出,后进先出。
下面就是我写的栈,里面也包含栈的最基础用法:判断括号是否匹配。
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAXSIZE 10
#define isMyFaith main
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef char SElemType;
typedef int Status;
typedef int Kurumi;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack, *Sq;
Status InitStack(SqStack &S){ //栈的初始化
S.base = new SElemType[MAXSIZE];
if(!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
Status DestoryStack(SqStack &S){ //摧毁这个栈
if(S.base){
delete S.base;
S.stacksize = 0;
S.base = S.top = NULL;
}
return OK;
}
Status StackEmpty(SqStack S){ //判断栈是否为空
if(S.top == S.base)
return TURE;
else
return FALSE;
}
Status StcakLength(SqStack S){ //返回栈的长度
return S.top - S.base;
}
Status GetTop(SqStack S,SElemType e){ //返回栈顶元素
e = *S.top;
return e;
}
Status ClearStack(SqStack S){ //清空栈
if(S.base) S.top = S.base;
return OK;
}
Status Push(SqStack &S,SElemType e){ //压栈
if(S.top - S.base == S.stacksize){
printf("满了,压不进去\n");
return ERROR;
}
*S.top = e;
S.top++;
return OK;
}
char Pop(SqStack &S,SElemType &e){ //出栈并使e获得栈顶元素
if(S.top == S.base)
return ERROR;
--S.top;
e = *S.top;
return e;
}
void outPutStack(SqStack S){
while(S.top != S.base){
auto temp = --S.top;
char e = *temp;
printf("%c ", e);
}
printf("\n");
}
void pushPopTest() {
printf("---- pushPopTest begins. ----\r\n");
char ch;
char e;
SqStack tempStack;
InitStack(tempStack);
printf("After initialization, the stack is: \n");
for (ch = 'a'; ch < 'm'; ch++) {
printf("Pushing %c.\r\n", ch);
Push(tempStack, ch);
printf("栈的长度为%d\n", StcakLength(tempStack)); /*插完再说长度*/
outPutStack(tempStack);
}
for (int i = 0; i < 3; i ++) {
ch = Pop(tempStack, e);
printf("Pop %c.\r\n", ch);
}
printf("---- pushPopTest ends. ----\r\n");
}
bool brackeMatching(char* paraString)
{
SqStack tempStack;
InitStack(tempStack);
int length = strlen(paraString);
char e;
for(int i = 0; i < length; i++){
char paradata = paraString[i];
switch (paradata) {
case '(':
case '[':
case '{':
Push(tempStack, paradata);
break;
case ')':
if(Pop(tempStack,e) != '('){
return FALSE;
}
break;
case ']':
if(Pop(tempStack,e) != '['){
return FALSE;
}
break;
case '}':
if(Pop(tempStack, e) != '{'){
return FALSE;
}
break;
default:
break;
}
}
if(tempStack.top == tempStack.base)
{
return TURE;
}
else
{
return FALSE;
}
}
void Test01()
{
char* temp = "666+(2*3)+111";
bool Match = brackeMatching(temp);
printf("Is the expression '%s' bracket matching? %d \r\n", temp, Match);
char* temp2 = "666+([[[2*3])+111";
bool Match1 = brackeMatching(temp2);
printf("Is the expression '%s' bracket matching? %d \r\n", temp2, Match1);
char* temp3 = "({}[])";
bool Match3 = brackeMatching(temp3);
printf("Is the expression '%s' bracket matching? %d \r\n", temp3, Match3);
}
Kurumi isMyFaith()
{
//pushPopTest();
Test01();
return 0;
}
其中:
1.栈的初始化:
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack, *Sq;
Status InitStack(SqStack &S){ //栈的初始化
S.base = new SElemType[MAXSIZE];
if(!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
2.判断栈是否为空:
Status StackEmpty(SqStack S){ //判断栈是否为空
if(S.top == S.base)
return TURE;
else
return FALSE;
}
3.返回栈的长度:
Status StcakLength(SqStack S){ //返回栈的长度
return S.top - S.base;
}
4.返回栈顶元素:
Status GetTop(SqStack S,SElemType e){ //返回栈顶元素
e = *S.top;
return e;
}
5.清空栈:
Status ClearStack(SqStack S){ //清空栈
if(S.base) S.top = S.base;
return OK;
}
6.压栈
Status Push(SqStack &S,SElemType e){ //压栈
if(S.top - S.base == S.stacksize){
printf("满了,压不进去\n");
return ERROR;
}
*S.top = e;
S.top++;
return OK;
}
7.出栈并获取栈顶元素:
char Pop(SqStack &S,SElemType &e){ //出栈并使e获得栈顶元素
if(S.top == S.base)
return ERROR;
--S.top;
e = *S.top;
return e;
}
8.输出整个栈:
void outPutStack(SqStack S){
while(S.top != S.base){
auto temp = --S.top;
char e = *temp;
printf("%c ", e);
}
printf("\n");
}
9.摧毁整个栈:
Status DestoryStack(SqStack &S){ //摧毁这个栈
if(S.base){
delete S.base;
S.stacksize = 0;
S.base = S.top = NULL;
}
return OK;
}
运算结果:
(简简单单玩下栈)
---- pushPopTest begins. ----
After initialization, the stack is:
Pushing a.
栈的长度为1
a
Pushing b.
栈的长度为2
b a
Pushing c.
栈的长度为3
c b a
Pushing d.
栈的长度为4
d c b a
Pushing e.
栈的长度为5
e d c b a
Pushing f.
栈的长度为6
f e d c b a
Pushing g.
栈的长度为7
g f e d c b a
Pushing h.
栈的长度为8
h g f e d c b a
Pushing i.
栈的长度为9
i h g f e d c b a
Pushing j.
栈的长度为10
j i h g f e d c b a
Pushing k.
满了,压不进去
栈的长度为10
j i h g f e d c b a
Pushing l.
满了,压不进去
栈的长度为10
j i h g f e d c b a
Pop j.
Pop i.
Pop h.
括号是否匹配(判断代码和源代码在全部代码里面)
---- pushPopTest ends. ----
Is the expression '666+(2*3)+111' bracket matching? 1
Is the expression '666+([[[2*3])+111' bracket matching? 0
Is the expression '({}[])' bracket matching? 1
这就是我对栈的运用和理解。