//数据结构-顺序存储的栈
typedef struct Linknode
{
/* data */
int data[MaxSzie]; //数据域
int top; //栈顶指针
}SqStack;
//数据结构----栈
typedef struct Linknode
{
int data; //数据域
struct Linknode *next; //指针域
}*Listack;
// 栈初始化
bool Init_LStack(SqStack &S)
{
S.top = -1;
return true;
}
// 栈初始化
bool Init_LStack(Listack &S)
{
S = NULL;
if(S == NULL)
{
cout<<"Init Success!"<<endl;
return true;
}
else
{
cout<<"Init Failure!"<<endl;
return false;
}
}
//栈判空
bool Stack_Empty(SqStack S)
{
if(S.top == -1)
{
cout<<"栈为空"<<endl;
return true;
}
else
{
cout<<"栈非空"<<endl;
return false;
}
}
//栈判空
bool Stack_Empty(Listack S)
{
if(S == NULL)
return true;//栈为空
return false; //栈非空
}
//入栈操作-链栈
bool Push_Stack(SqStack &S,int x)
{
if(S.top == MaxSzie-1)//栈满
{
cout<<"栈满"<<endl;
return false;
}
S.data[++S.top] = x;
cout<<"元素: "<<x<<" 入栈成功"<<endl;
return true;
}
//入栈操作-链栈
bool Push_Stack(Listack &S,int x)
{
Linknode *p;
p = new Linknode;
if(p == NULL)
{
cout<<"空间申请失败!"<<endl;
return false;
}
p->data = x;
p->next = S;
S = p;
cout<<"Push:"<<x<<endl;
return true;
}
//出栈操作
bool Pop_Stack(SqStack &S,int &x)
{
if(Stack_Empty(S))
return false;
x = S.data[S.top--];
return true;
}
//出栈操作
bool Pop_Stack(Listack &S,int &x)
{
if(Stack_Empty(S))//链表为空
{
cout<<"Stack is NULL!"<<endl;
return false;
}
x = S->data;
S = S->next;//指针后移
cout<<"Pop:"<<x<<endl;
return true;
}
//访问栈顶元素
bool Visit_TopEle_Stack(SqStack S, int &x)
{
if(Stack_Empty(S))
return false;
x = S.data[S.top];
return true;
}
//访问栈顶元素
bool Visit_TopEle_Stack(Listack S, int &x)
{
if(Stack_Empty(S))//链表为空
{
cout<<"Stack is NULL!"<<endl;
return false;
}
x = S->data;
return true;
}
//打印栈
void Print_Stack(SqStack S)
{
if(Stack_Empty(S))//链表为空
{
return;
}
cout<<"Stack is:"<<endl;
;
cout<<" Top->| "<<setw(5)<<setfill(' ')<<left<<S.data[S.top]<<"|"<<endl;
cout<<" |________|"<<endl;
while (S.top--)
{
cout<<" | "<<setw(5)<<setfill(' ')<<left<<S.data[S.top]<<"|"<<endl;
cout<<" |________|"<<endl;
}
}
//打印栈
void Print_Stack(Listack S)
{
if(Stack_Empty(S))//数组为空
{
cout<<"Stack is NULL!"<<endl;
return;
}
cout<<"Stack is:"<<endl;
Linknode *p = S;
cout<<" Top->| "<<setw(5)<<setfill(' ')<<left<<p->data<<"|"<<endl;
cout<<" |________|"<<endl;
p = p->next;
while (p != NULL)
{
cout<<" | "<<setw(5)<<setfill(' ')<<left<<p->data<<"|"<<endl;
cout<<" |________|"<<endl;
p = p->next;
}
}
以I为入栈操作,以O为出栈操作,检验IO字符序列的合法性
/*********************************************
* 判断栈的操作序列的合法性
* Page-71-3-(2)
*
* ********************************************/
bool Judge_operate(string str)
{
cout<<"The operate is: "<<str<<endl;
Listack S;
if(!Init_LStack(S))
{
cout<<"栈初始化失败!"<<endl;
return false;
}
int value;
for(char ch : str)
{
if(ch == 'I')
Push_Stack(S, 1);
else if(ch == 'O')
{
if(Stack_Empty(S))
{
cout<<"操作不合法:栈空,仍继续出栈"<<endl;
return false;
}
else
{
Pop_Stack(S,value);
}
}
}
if(Stack_Empty(S))
{
cout<<"操作合法!"<<endl;
return true;
}
else
{
cout<<"操作不合法:栈中仍余有元素"<<endl;
return false;
}
}
现有一括号’(’,’)’,’[’,’]’,’{’,’}'序列组成的字符序列,判断该字符序列中的括号是否配对
//括号配对问题
bool Judge_Brackets_pairs(string str)
{
cout<<"The Brackets's string is: "<<str<<endl;
Listack S; //创建一个栈
if(!Init_LStack(S))
{
cout<<"栈初始化失败!"<<endl;
return false;
}
ElemType value;
for(char ch : str)
{
if(ch == '(' || ch == '[' || ch == '{')
{
Push_Stack(S, ch);
}
else
{
ElemType xchar,tchar;
Visit_TopEle_Stack(S, xchar);
switch (xchar)
{
case '(':
if(ch == ')') {if(!Pop_Stack(S,tchar)) return false;}
else return false;
break;
case '[':
if(ch == ']') {if(!Pop_Stack(S,tchar)) return false;}
else return false;
break;
case '{':
if(ch == '}') {if(!Pop_Stack(S,tchar)) return false;}
else return false;
break;
default: return false;
break;
}
}
}
if(Stack_Empty(S))return true;
else return false;
}
待续