栈和队列

栈和队列

栈:先进后出;
队列:先进先出
注意:循环队列:在一个给定长度len的队列里,当队列队尾已经是最后一个,tail=len时,再入队时,从队头开始入队。像这样队列头尾相接的顺序存储结构称为循环队列。

循环队列入队时,要看队列是否已满,出队时要看队列是否为空。
典型例题
https://vjudge.net/contest/351438#problem/A
https://vjudge.net/contest/351438#problem/D

#include<stdio.h>
#include<string.h>
int main()
{
    int s;
    scanf("%d",&s);
    while(s--)
    {
        int n,i,j,k,s,f,w1,w2;
        scanf("%d",&n);
        int a[100]= {0};
        int b[100]= {0};
        char c[100];
        int c2[100]= {0};
        for(s=0,j=0; s<n; s++)
        {

            scanf("%s",c);
            if(strcmp(c,"push")==0)
            {
                scanf("%d",&k);
                a[j]=k;
                b[j]=k;
                j++;
            }

            if(strcmp(c,"pop")==0)
            { w1=1,w2=1;
                for(i=0; i<j; i++)
                {
                    if(a[i]!=0)
                    {
                        w1=0;
                        a[i]=0;
                        break;
                    }
                }
                for(f=j-1; f>=0; f--)
                {
                    if(b[f]!=0)
                    {
                        w2=0;
                        b[f]=0;
                        break;
                    }
                }
            }

        }
        if(w1!=0&&w2==0)
         {
             printf("error\n");
             for(i=0; i<j; i++)
            {
                if(b[i]!=0)
                    printf("%d ",b[i]);
            }
            printf("\n");
         }
         if(w1==0&&w2!=0)
         {
              for(i=0; i<j; i++)
            {
                if(a[i]!=0)
                    printf("%d ",a[i]);
            }
            printf("\n");
             printf("error\n");
         }
         if(w1!=0&&w2!=0)
            printf("error\nerror\n");
        else
        {
            for(i=0; i<j; i++)
            {
                if(a[i]!=0)
                    printf("%d ",a[i]);
            }
            printf("\n");
            for(i=0; i<j; i++)
           
            if(b[i]!=0)
                    printf("%d ",b[i]);
            }
            printf("\n");
        }

    }
    return 0;
}
***********************************************************************************************
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include <math.h>
float a[10000];
char b[10000],z[10000],h[10000];
int w1,w2;
int i=0, j=1;
void floatpush(float x)
{
    a[++w1]=x;
}
void charpush(char x)
{
    b[++w2]=x;
}
float floatpop()
{
    w1--;
    return a[w1+1];
}
char charpop()
{
    w2--;
    return b[w2+1];
}
void floatadd(float x)
{
    a[w1]*=10;
    a[w1]+=x;
}
int find()
{
    char c;
    c=charpop();
    h[j]=' ';
    h[j+1]=c;
    j+=2;
    if(w2==0)
        return 0;
    else
        return 1;
}
int main()
{
    while(gets(z)!=NULL)
    {
        w1=0,w2=0;
        int les=strlen(z);
        if(strcmp(z,"0")==0)
            break;
        else
        {
            for(i=0; i<les; i++)
            {
                if (z[i]>='0'&&z[i]<='9')
                    h[j++]=z[i];
                else
                {
                    if(z[i]=='+')
                    {
                        while(b[w2]=='+'||b[w2]=='-'||b[w2]=='*'||b[w2]=='/')
                        {
                            if(find()==0)
                                break;
                        }
                        charpush('+');
                    }
                    if(z[i]=='-')
                    {
                        while(b[w2]=='+'||b[w2]=='-'||b[w2]=='*'||b[w2]=='/')
                        {
                            if(find()==0)
                                break;
                        }
                        charpush('-');
                    }
                    if(z[i]=='*')
                    {
                        while(b[w2]=='*'||b[w2]=='/')
                        {
                            if(find()==0)
                                break;
                        }
                        charpush('*');
                    }
                    if(z[i]=='/')
                    {
                        while(b[w2]=='*'||b[w2]=='/')
                        {
                            if(find()==0)
                                break;
                        }
                        charpush('/');
                    }
                    h[j]=' ';
                    j++;
                }
            }
            while(w2>0)
            {
                int c=charpop();
                h[j]=' ';
                h[j+1]=c;
                j+=2;
            }
            h[j]='#';
            i=1;
            while(h[i]!='#')
            {
                if(h[i]=='+'||h[i]=='-'||h[i]=='*'||h[i]=='/')
                {
                    float a=floatpop(),b=floatpop();
                    float c;
                    if(h[i]=='+')
                        c=a+b;
                    if(h[i]=='-')
                        c=b-a;
                    if(h[i]=='*')
                        c=a*b;
                    if(h[i]=='/')
                        c=(float)b/(float)a;
                    floatpush(c);
                }
                else
                {
                    if(h[i]!=' ')
                    {
                        floatpush(h[i]-48);
                        j=1;
                        while(h[i+j]!=' ')
                        {
                            floatadd(h[i+j]-48);
                            j++;
                        }
                        i+=j-1;
                    }
                }
                i++;
            }
            printf("%.2f\n",a[1]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值