怎么用c语言实现一个顺序栈

Python011

怎么用c语言实现一个顺序栈,第1张

char stack[100],s[300]

int i,top=-1

scanf("%s",s)

for(i=0s[i]i++)

{

if(s[i]=='('||s[i]=='['||s[i]=='{')stack[++top]=s[i]

else if(s[i]==')'||s[i]==']'||s[i]=='}')

if(top>=0&&(stack[top]=='('&&s[i]==')'||stack[top]=='['&&s[i]==']'||stack[top]=='{'&&s[i]=='}'))--top

else {printf("error!\n")return 0}

}

if(top==0)printf("OK!\n")

else printf("error!\n")

#define STACK_SIZE 100

#define PUSH_POP_SUCCESS 1

#define PUSH_POP_ERROR 0

struct _stackbuf {

int _collection[STACK_SIZE]

int _top

}

typedef struct _stackbuf S_STACK

typedef unsigned int u_int_f

// 入栈

u_int_f push(S_STACK *stack, int d){

if (stack->_top >= STACK_SIZE) return PUSH_POP_ERROR

stack->_collection[stack->_top++] = d

return PUSH_POP_SUCCESS

}

// 出栈

u_int_f pop(S_STACK *stack, int *e){

if (!stack->_top) return PUSH_POP_ERROR

*e=stack->_collection[--(stack->_top)]

return PUSH_POP_SUCCESS

}

int main(){

S_STACK stack = { {0},0 }

push(&stack, 1)

push(&stack, 2)

push(&stack, 3)

int gv = 0

pop(&stack, &gv)

printf("%d\n", gv)

system("PAUSE")

return 0

}