用进队函数进队。然后分别对两个队列交替调用出队函数出队,直到有一个队列为空为止。
3题中定义一个链栈。根据输入的值进行相应的操作。如果输入的是左括号则放入栈中。如果输入的是右括号,则先进行出栈处理,如果括号配对,则不再处理;如果括号不配对,则分别对相应的左括号和右括号进行进栈处理。如果输入其他数值和符号不进行任何操作。输入#终止输入。最后调用测试栈空函数,如果栈空则括号配对,否则不配对。
实现部分:
源代码:
1题:
#include<iostream.h>
#include<assert.h>
template<class t1>
struct stacknode{
t1 data;
stacknode *next;};
template<class t1>
class linkstack
{
stacknode<t1> *top;
unsigned height;
public:
linkstack();
~linkstack()
{clear();}
void clear();
void push(t1 &x);
bool pop(t1 &x);
bool isempty()
{return(heighe==0)?true:false;}
};
template<class t1>
linkstack<t1>::linkstack()
{height=0;
top=NULL;}
//清栈函数
template<class t1>
void linkstack<t1>::clear()
{t1 x;
while(pop(x));
}
//进栈函数