1,  若入栈的元素为n,则可得到的输出序列数量为 (2n)!/(n+1)(n!)(n!)。
2,  用两个长度相同的栈S1,S2构造一个队列。在S1中进行入队操作,S2中进行出队操作 ,判断队列空的条件是,S1和S2同时为空,判断队列满的条件是S1和S2同时为满。
 void EnQueue(ElemType x)
void EnQueue(ElemType x)

 {
{ if(!Full(S1))
    if(!Full(S1))
 
     {//S1未满直接进入
{//S1未满直接进入 S1.Push(x);
        S1.Push(x); }
    } else
    else
 
     {
{ if(Empty(S2)==true)
        if(Empty(S2)==true)
 
         {
{ while(!Empty(S1))
            while(!Empty(S1))
 
             {
{ S2.Push(S1.Pop());
                S2.Push(S1.Pop()); }
            } S1.Push(x);
            S1.Push(x);         }
        } }
    } }
}
 ElemType DeQueue()
ElemType DeQueue()

 {
{ if(!Empty(S2))
    if(!Empty(S2))
 
     {
{ return S2.Pop();
        return S2.Pop(); }
    } else
    else
 
     {
{ if(!Empty(S1))
        if(!Empty(S1))
 
         {
{ while(!Empty(S1))
            while(!Empty(S1))
 
             {
{ S2.Push(S1.Pop());
                S2.Push(S1.Pop()); }
            } return S2.Pop();
            return S2.Pop(); }
        } 
         }
    } }
}
3.求两个正整数的最大公约数的非递归算法。
 #define MAX 100
#define MAX 100 struct Stack
struct Stack

 {
{ int s;
    int s; int t;
    int t; };
};
 int gcd(int m,int n)
int gcd(int m,int n)

 {
{ int k;
    int k; Stack S[MAX];
    Stack S[MAX]; int top = -1,tmp;
    int top = -1,tmp; if(m<n)
    if(m<n)
 
     {
{ tmp = m;
        tmp = m; m = n;
        m = n; n = tmp;
        n = tmp; }
    } top++;
    top++; S[top].s = m;
    S[top].s = m; S[top].t = n;
    S[top].t = n; while(top>=0&&S[top].t!=0)
    while(top>=0&&S[top].t!=0)
 
     {
{ if(S[top].t!=0)
        if(S[top].t!=0)
 
         {
{ tmp = S[top].s;
            tmp = S[top].s; m = S[top].t;
            m = S[top].t; n = m%tmp;
            n = m%tmp; top++;
            top++; S[top].s = m;
            S[top].s = m; S[top].t = n;
            S[top].t = n; }
        } }
    } return S[top].s;
    return S[top].s; }
}
4.
n+1,m =0
Akm(m,n) = Akm(m-1,1) m!=0,n=0
Akm(m-1,Akm(m,n-1)),m!=0,n!=0
写非递归算法。
 #define MAXSIZE 100
#define MAXSIZE 100 typedef struct
typedef struct

 {
{ int tm;
    int tm; int tn;
    int tn; }Stack;
}Stack;
 int akm(int m,int n)
int akm(int m,int n)

 {
{ Stack S[MAXSIZE];
    Stack S[MAXSIZE]; int top = 0;
    int top = 0; S[top].tm = m;
    S[top].tm = m; S[top].tn = n;
    S[top].tn = n; do
    do
 
     {
{ while(S[top].tm!=0)
        while(S[top].tm!=0)
 
         {
{ while(S[top].tn!=0)
            while(S[top].tn!=0)
 
             {
{ top++;
                top++; S[top].tm = S[top-1].tm;
                S[top].tm = S[top-1].tm; S[top].tn = S[top-1].tn-1;
                S[top].tn = S[top-1].tn-1; }
            } S[top].tm--;
            S[top].tm--; S[top].tn=1;
            S[top].tn=1; }
        } if(top>0)
        if(top>0)
 
         {
{ top--;
            top--; S[top].tm--;
            S[top].tm--; S[top].tn = S[top].tn+1;
            S[top].tn = S[top].tn+1; }
        } 
         }while(top!=0 || S[top].tm!=0);
    }while(top!=0 || S[top].tm!=0); top--;
    top--; return S[top+1].tn+1;
    return S[top+1].tn+1; }
}5.写出和下列递归过程等价的非递归过程
 void test(int &sum)
void test(int &sum)

 {
{ int k;
    int k; scanf("%d",&k);
    scanf("%d",&k); if(k==0) sum = 1;
    if(k==0) sum = 1; else
    else
 
     {
{ test(sum);
        test(sum); sum = k*sum;
        sum = k*sum; }
    } printf("%d",sum);
    printf("%d",sum); }
}
分析:程序功能是按照输入的整数,按相反顺序进行累计乘法运算
 #define MAXSIZE 100
#define MAXSIZE 100 void test(int &sum)
void test(int &sum)

 {
{ int Stack[MAXSIZE];
    int Stack[MAXSIZE]; int top = -1,k;
    int top = -1,k; sum = 1;
    sum = 1; scanf("%d",&k);
    scanf("%d",&k); while(k!=0)
    while(k!=0)
 
     {
{ Stack[++top] = k;
        Stack[++top] = k; scanf("%d",&k);
        scanf("%d",&k); }
    } printf("%d",sum);
    printf("%d",sum); while(top!=-1)
    while(top!=-1)
 
     {
{ sum *=Stack[top--];
        sum *=Stack[top--]; printf("%d",sum);
        printf("%d",sum); }
    } }
} 
        
6.假设表达式由单字母变量和双目四则运算算符构成,写一个算法,将一个书写正确的表达式转换为逆波兰式。
 void ConPoland(char express[],char suffix[])
void ConPoland(char express[],char suffix[])

 {
{ char *p = express,ch1 = *p,ch2;
    char *p = express,ch1 = *p,ch2; int k = 0;
    int k = 0; InitStack(S);
    InitStack(S); Push(S,'#');
    Push(S,'#'); while(!StackEmpty(S))
    while(!StackEmpty(S))
 
     {
{ if(!IsOperator(ch1))
        if(!IsOperator(ch1)) suffix[k++] = ch1;
            suffix[k++] = ch1; else
        else
 
         {
{ switch(ch1)
            switch(ch1)
 
             {
{ case '(':
                case '(':     Push(S,ch1);break;
                    Push(S,ch1);break; case ')':
                case ')':     Pop(S,ch2);
                    Pop(S,ch2); while(ch2!='(')
                    while(ch2!='(')    
 
                     {
{ suffix[k++] = ch2;
                        suffix[k++] = ch2; Pop(S,ch2);
                        Pop(S,ch2); }
                    } break;
                    break; default:
                default: while(GetTop(S,ch2)&&precede(ch2,ch1))
                    while(GetTop(S,ch2)&&precede(ch2,ch1))
 
                     {
{ suffix[k++] = ch2;
                        suffix[k++] = ch2; Pop(S,ch2);
                        Pop(S,ch2); }
                    } if(ch1!='#')
                    if(ch1!='#') Push(S,ch1);
                        Push(S,ch1); break;
                    break;                             
                     }
            } }
        } if(ch1!="#')
        if(ch1!="#')     ch1 = *++p;
            ch1 = *++p; }
    } suffix[k] = '\0';
    suffix[k] = '\0'; }
}