手写栈
实现<stack>(伪)
问题描述:给一个括号序列,问这个括号序列是否完全匹配
#pragma once
#include<cassert>
#include<string.h>
#include<iostream>
template<class T, int size = 1050>
class Stack
{
public:
Stack(): top(-1) {}
void push(const T &item)
{
assert(top<=size-2);
list[++top] = item;
}
T pop()
{
assert(top>=0);
return list[top--];
}
void clear() { top = -1; }
const T &peek() const
{
assert(top>=0);
return list[top];
}
bool empty() const { return top==-1; }
bool full() const { return top==size-1; }
private:
T list[size];
int top;
};
int main(void)
{
int n, i;
Stack<char> st;
char str[1005];
while(scanf("%s", str+1)!=EOF)
{
st.clear();
n = strlen(str+1);
for(i=1;i<=n;i++)
{
if(str[i]!='(' && str[i]!=')')
continue;
if(str[i]=='(')
st.push(str[i]);
else
{
if(st.empty()==0 && st.peek()=='(')
st.pop();
else
st.push(str[i]);
}
}
if(st.empty()==0)
printf("No\n");
else
printf("Yes\n");
}
}