栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

王道数据结构习题代码3.3.7(栈和队列的应用)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

王道数据结构习题代码3.3.7(栈和队列的应用)

第一题(括号匹配)
#include 
#include 
#include
#define MaxSize 50

typedef char ElemType;


typedef struct
{
    ElemType data[MaxSize];
    int top;
} SqStack;

bool InitStack(SqStack *s)
{
    s->top=-1;//初始化栈
}


bool Push(SqStack *s,ElemType e)
{
    if(s->top==MaxSize-1)return false;
    s->data[++s->top]=e;
    return true;
}

bool Pop(SqStack *s,ElemType *e)
{
    if(s->top==-1)return false;
    *e=s->data[s->top--];
    return true;
}


void PrintStack(SqStack *s)
{
    int i=0;
    for(int i=0; i<=s->top; i++)
    {
        printf("%d-->",s->data[i]);
    }
    printf("n");
}


//判断栈是否为空
bool  EmptyStack(SqStack *s)
{
    if(s->top==-1)return true;
    return false;
}

bool BracketsCheck(char *str)
{
    SqStack s;
    InitStack(&s);
    int i=0;
    char e;
    while(str[i]!='')
    {
        switch(str[i])
        {
        //左括号匹配
        case '(':
            Push(&s,'(');
            break;
        case '[':
            Push(&s,'[');
            break;
        case '{':
            Push(&s,'{');
            break;
        //右括号检测栈顶
        case')':
            Pop(&s,&e);
            if(e!='(') return false;
            break;
        case ']':
            Pop(&s,&e);
            if(e!='[')return false;
            break;
        case '}':
            Pop(&s,&e);
            if(e!='{')return false;
            break;
            default: break;
        }
        i++;
    }
    if(!EmptyStack(&s))
    {
        printf("括号不匹配n");
        return false;
    }
    else
    {
        printf("括号匹配n");
        return true;

    }
}



int main()
{


    char d[12];
    char *p;//p为字符数组
    printf("请输入匹配表达式n");
    gets(d);
    p=d;
    printf("%d",BracketsCheck(p));


    return 0;
}



第二题
#include 
#include 
#include
#define MaxSize 50  //定义栈中元素最大个数

typedef char ElemType;

typedef struct
{
    ElemType data[MaxSize];//存放栈中元素
    int top;//栈顶指针

} SqStack;

//栈的初始化
void  InitStack(SqStack *S)
{
    S->top=-1;//初始化栈
}

//判断栈是否为空
bool StackEmpty(SqStack *S)
{
    if(S->top==-1)return true;
    return false;
}

//入栈
bool Push(SqStack *S,ElemType x)
{
    if(S->top==MaxSize-1)return false;//栈满
    S->data[++S->top]=x;//添加进入栈
    return true;
}

//出栈
bool Pop(SqStack *S,ElemType *x)
{
    if(S->top==-1)return false;//栈为空
    *x=S->data[S->top--];
    return true;
}

bool GetTop(SqStack *S,ElemType *x)
{
    if(S->top==-1)return false;
    *x=S->data[S->top];
    return true;
}

void PrintStack(SqStack *S)
{
    int i=0;
    while(i<=S->top)
    {
        printf("%c ",S->data[i++]);
    }
    printf("n");
}


void Train_Arrange(char *train)
{
    char *p=train,*q=train,c;
    SqStack s;
    InitStack(&s);
    //将软座调度到前列,硬座存到栈中
    int i=0,j=0;
    while(p[i]!='')
    {
        if(p[i]=='H')
        {
            Push(&s,'H');
        }else{
           q[j++]='S';//把软座调度到前面
        }
        i++;
    }
    while(!StackEmpty(&s))
    {
        Pop(&s,&c);
        q[j++]=c; //将硬座接在后面
    }
}
int main()
{

    char train[12];
    gets(train);
    char *p=train;
    Train_Arrange(p);
    while(*p)
    {
        printf("%c<-->",*(p++));
    }





    return 0;
}



第三题(利用栈实现递归)
#include 
#include 
#include
#define MaxSize 50

typedef char ElemType;




double p(int n,double x)
{
    struct stack
    {
        int no;//当前n 也就是递归层数
        double val;
    } st[MaxSize];
    int top=-1;
    for(int i=n; i>=2; i--) //最后两层不需要赋值
    {
        top++; //栈顶的n小,栈底的n大
        st[top].no=i;
    }
    double fv1=1,fv2=2*x;//P0=1,P1=2*x
    while(top>=0)
    {
        st[top].val=2*x*fv2-2*(st[top].no-1)*fv1; //递归表达式
        fv1=fv2; //更新fv1   Pn-2的值
        fv2=st[top].val; //更新fv2  Pn-1的值
        top--;
    }
    if(n==0)
    {
        return fv1;
    }
    return fv2;
}


int main()
{

    printf("%lf",p(3,1));  //-4



    return 0;
}



第四题

用两个队列模拟

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号