#include "myhead.h"
//栈
struct stack
{
char data;
struct stack *next;
};
//初始化
struct stack *init()
{
struct stack *p=malloc(sizeof(struct stack));
p->next=NULL;
return p;
}
//入栈
int push(struct stack *p,int n)
{
struct stack *new=malloc(sizeof(struct stack));
new->data=n;
new->next=p->next;
p->next=new;
}
//出栈
char pop(struct stack *p)
{
struct stack *q=p->next;
char n;
if(p->next==NULL)
return -1;
n=p->next->data;
p->next=p->next->next;
free(q);
return n;
}
int main()
{
int i=0;
char temp;
char buf[200]="shfhd(hdfhfhdh) fdjfjdjfj} [fhdhsfhdsh (fdhsfh)";
struct stack *filo=init();//栈初始化
while(buf[i]!='\0')
{
if(buf[i]=='(' || buf[i]=='{' || buf[i]=='[')//入栈
{
push(filo,buf[i]);
printf("%c 进栈!\n",buf[i]);
}
if(buf[i]=='
编程实现判断程序中的括号是否匹配 () {} []
本文介绍了栈的数据结构,包括顺序栈和链式栈的实现,并详细阐述了如何利用栈来判断程序中的括号(() {} [])是否匹配,常见于面试题和操作系统中的函数调用。
摘要由CSDN通过智能技术生成