#include<iostream>
#include<iomanip>
#include<cmath>
#include<string.h>
#include<string>
#include <ctime>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
using namespace std;
// 栈(stack)
// 定义:只允许在一段进行插入和删除,例如盘子
// 栈顶:允许插入和删除的一端
// 栈底
// 空栈:不包含元素的空表
//栈的基本操作:创销增删查空
// InitStack(&S)
// DestroyStack(&S)
// Push(&S,x)
// Pop(&S,&x)
// GetTop(S,&x)
// StackEmpty(S)
// n个元素排列可能:卡特兰数
//顺序栈(和顺序表差不多)
// #define MaxSize 10
// typedef struct
// {
// int data[MaxSize];//静态数组存放栈中元素
// int top;//栈顶指针
// }SqStack;
// //初始化
// void InitStack(SqStack &S)
// {
// S.top = -1;
// }
// //判断栈空
// bool StackEmpty(SqStack S)
// {
// if(S.top == -1)return true;
// else return false;
// }
// //进栈
// bool Push(SqStack &S,int e)
// {
// if(S.top == MaxSize-1)return false;//判断栈没满
// S.data[S.top+1] = e;//可以写成S.data[++S.top] = e;存入数据
// S.top++;
// return true;
// }
// //出栈
// bool Pop(SqStack &S,int &e)
// {
// if(S.top ==-1)return false;//没有元素
// e = S.data[S.top];//取出数据
// S.top--;//指针-1
// return true;
// }
// //读取栈顶
// bool GetTop(SqStack S,int &e)
// {
// if(S.top==-1)return false;
// e = S.data[S.top];
// return true;
// }
// //共享栈
// // 判断栈满 top0+1 == top1
// //栈的链式存储(被限制的单链表)
// // 头插法(进栈)
// typedef struct LNode
// {
// int data;
// LNode *next;
// }LNode,*StackList;
// //初始化
// //带头节点
// bool InitStack(StackList &S)
// {
// S = new LNode;//开辟空间
// if(S==NULL)return false;
// S->next = NULL;//搞定头节点
// int x;
// cin>>x;
// while(x!=9999)//输入数据
// {
// LNode *p = new LNode;
// p->next = S->next;
// S->next = p;
// cin>>x;
// }
// return true;
// }
// //不带头节点
// bool InitStack(StackList &S)
// {
// S = NULL;//没有头节点
// int x;
// cin>>x;
// while(x!=9999)//输入数据
// {
// LNode *p = new LNode;//开辟空间
// p->data = x;
// p->next = S;
// S = p;
// cin>>x;
// }
// return true;
// }
// //删除
// bool Push(StackList &S,int &e)
// {
// if(S==NULL)return false;//空栈
// LNode *p = S;//记录栈顶
// e = p->data;//记录数据
// S = S->next;//直接到下一个
// return true;
// }
// //查找
// bool GetTop(StackList S,int &e)
// {
// if(S==NULL)return false;
// e = S->data;
// return true;
// }
// //无节点使用栈(舒服)
// //队列:
// // 定义:只允许在一段进行插入,另一端删除的线性表,例如排队
// // 队头:可以删除
// // 队尾:可以插入
// // 空队列;
// //基本操作:创销增删查空
// // InitQueue(&Q)
// // DestroyQueue(&Q)
// // EnQueue(&Q,x)
// // DeQueue(&Q,x)
// // GetHead(Q,x)
// // QueueEmpty(Q)
// // 顺序存储
// #define MaxSize 10
// typedef struct
// {
// int data[MaxSize];//静态数组存放栈中元素
// int front,rear;//栈顶指针
// }SqQueue;
// //初始化
// void InitQueue(SqQueue &Q)
// {
// Q.front = Q.rear = 0;
// }
// //判空
// bool QueueEmpty(SqQueue Q)
// {
// if(Q.front==Q.rear)return true;
// else return false;
// }
// //入队
// bool EnQueue(SqQueue &Q,int x)
// {
// if((Q.rear+1)%MaxSize==Q.front)return false;//队列已满
// Q.data[Q.rear] = x;
// Q.rear = (Q.rear+1)%MaxSize;
// return true;
// }
// //出队
// bool DeQueue(SqQueue &Q,int &x)
// {
// if(Q.front==Q.rear)return false;//空队
// x = Q.data[Q.front];//读取值
// Q.front = (Q.front+1)%MaxSize;//向下移动
// return true;
// }
// //查找
// bool GetHead(SqQueue Q,int &x)
// {
// if(Q.front==Q.rear)return false;//空队
// x = Q.data[Q.front];
// return true;
// }
// //长度
// // bool len = (rear-front+MaxSize)%MaxSize;
// //队列链式实现
// typedef struct LinkNode
// {
// int data;
// LinkNode *next;
// }LinkNode;
// typedef struct
// {
// LinkNode *front,*rear;
// }LinkQueue;
// //初始化带头结点
// void InitQueue(LinkQueue &Q)
// {
// Q.front = Q.rear = new LinkNode;//开辟空间
// Q.front->next = NULL;
// }
// //判空
// bool IsEmpty(LinkQueue Q)
// {
// if(Q.front==Q.rear)return true;
// else return false;
// }
// //不带头结点
// void InitQueue(LinkQueue &Q)
// {
// Q.front = Q.rear =NULL;//开辟空间
// }
// //判空
// bool IsEmpty(LinkQueue Q)
// {
// if(Q.front==NULL)return true;
// else return false;
// }
// // 入队
// // 带头结点
// void EnQueue(LinkQueue &Q,int x)
// {
// LinkNode *s = new LinkNode;//开辟空间
// s->data = x;
// s->next = NULL;
// Q.rear->next = s;//插入结点
// Q.rear = s;//移动表尾位置
// }
// //不带头结点
// void EnQueue(LinkQueue &Q,int x)
// {
// LinkNode *s = new LinkNode;//开辟空间
// s->data = x;
// s->next = NULL;
// if(Q.front==NULL)//特殊考虑
// {
// Q.front = s;
// Q.rear = s;
// }
// else
// {
// Q.rear->next = s;
// Q.rear = s;
// }
// }
// //出队
// // 带头结点
// bool DeQueue(LinkQueue &Q,int &x)
// {
// if(Q.front==Q.rear)return false;
// LinkNode *p = Q.front->next;//出队结点
// x = p->data;
// Q.front->next = p->next;
// if(Q.rear==p)Q.rear=Q.front;//特殊判断
// delete p;
// return true;
// }
//双端队列
// 定义:两端插入,两端删除
//识别括号匹配
//栈
char arr[100];
typedef struct LNode
{
char a;
LNode *next;
}LNode,*StackList;
void InitStack(StackList &S)
{
S=NULL;//没有头结点
}
void Push(StackList &S,char x)
{
LNode *p = new LNode;//开辟空间
p->a = x;
p->next = S;
S = p;//换位
}
void Pop(StackList &S,char &x)
{
LNode *p = S;//记住位置
x = p->a;
S = p->next;
delete p;//释放空间
}
bool StackEmpty(StackList S)
{
if(S==NULL)return true;
else return false;
}
bool bracketCheack(char arr[], int len)//输入字符和长度
{
StackList S;
InitStack(S);//初始化栈
for(int i =0;i<len;i++)//遍历字符
{
if(arr[i]=='('||arr[i]=='['||arr[i]=='{')Push(S,arr[i]);//入栈
else
{
if(StackEmpty(S))return false;//没有左括号来匹配
else
{
char item;
Pop(S,item);//出栈
if(arr[i]==')'&&item!='(')return false;
if(arr[i]==']'&&item!='[')return false;
if(arr[i]=='}'&&item!='{')return false;
//判断匹配
}
}
}
return StackEmpty(S);//判断是否还有左括号
}
int main()
{
int n;
cin>>n;
for(int i =0;i<n;i++)cin>>arr[i];
cout<<bracketCheack(arr,n);
}
//中缀表达式的计算(感觉太牛了)
线性表——栈,队列
最新推荐文章于 2024-09-14 22:25:23 发布