数据结构:sy06

【问题描述】

已知Q是一个非空队列,S是一个空栈。仅使用少量工作变量以及对队列和栈的基本操作,编写一个算法,将队列Q中的所有元素逆置。


【输入形式】

输入的第一行为队列元素个数,第二行为队列从首至尾的元素


【输出形式】

输出队列的逆置


【样例输入】

3

1 2 3


【样例输出】

3 2 1

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 1001
typedef int ElementType ;
typedef struct
{
    ElementType data[MaxSize];
    int top;//始终指向栈顶,也就是最后一个加进来的变量
}SqStack;
typedef struct
{
    ElementType data[MaxSize];//存储MaxSize-1 个元素 循环队列要牺牲一个存储单元
    int front,rear;
}SqQueue;
void InitStack(SqStack &s)
{
    s.top=-1;//初始化:栈为空,top=-1
}
bool Empty(SqStack s)
{
    if(s.top==-1)
    {
        return true;
    }
    return false;
}
bool  Push(SqStack &s,ElementType e)
{
    if(s.top==MaxSize-1){
        return false;
    }

    s.data[++s.top]=e;

    return true;
}
bool  Pop(SqStack &s,ElementType &e)
{
    if(Empty(s)){
        return false;
    }
    e=s.data[s.top--];

    return true;
}
void InitQueue(SqQueue &Q)
{
    Q.front=Q.rear=0;
}
bool EnQueue(SqQueue &Q,ElementType e)
{
    if((Q.rear+1)%MaxSize==Q.front)//队满
    {
        return false;
    }
    Q.data[Q.rear]=e;
    Q.rear=(Q.rear+1)%MaxSize;//这里只会最大到max-1,因此,若到达这个位置,rear会回到起始位置,不出队,下一次再加入元素会return false
    return true;
}
bool isEmpty(SqQueue Q)
{
    return Q.rear==Q.front;
}
bool reverse(SqQueue &Q,SqStack s)
{
    if(Empty(s))
    {
        return false;
    }
    while (s.top>-1)
    {
        EnQueue(Q,s.data[s.top]);
        s.top--;
    }
    return true;
}
void Print_Delete(SqQueue &Q)
{//出队,从第一个进来的元素开始删
    ElementType e;
    while (1)
    {
        e=Q.data[Q.front];
        printf("%d ",e);
        Q.front=(Q.front+1)%MaxSize;
        if(isEmpty(Q))
        { return;}
    }

}
int main() {
    SqStack s;
    SqQueue Q;
    ElementType ret,e,a,b[MaxSize];
    InitStack(s);
    InitQueue(Q);
    int len,i=0;
    scanf("%d",&len);
    while (i<len)
    {
        scanf("%d",&e);
        Push(s,e);//入栈
        i++;
    }
    reverse(Q,s);
    Print_Delete(Q);
    return 0;
}

【问题描述】
 设计一个简单的计算器,一次性将所有的运算命令输入,先计算的后输入。首行输入一个数字,表示运算命令的总个数;其余各行输入,每行表示一个运算命令(包括运算操作符和操作数)。所有的输入及输出都为整数。运算操作符有只有Add、Sub、Mul,即加法、减法、乘法三种。

【输入形式】
 首行:运算命令的总个数
 其余各行:每次运算命令,包括运算操作符和操作数

【输出形式】
 输出计算结果

【样例输入】
 4
 Add 1
 Sub 2
 Mul 4
 Sub 4 5

【样例输出】
 3

【样例说明】
 计算表达式为(5-4)*4-2+1

人总是会疯掉,或早或晚

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxSize 1001
typedef char ElementType ;
typedef struct
{
    ElementType data[MaxSize];
    int top;//始终指向栈顶,也就是最后一个加进来的变量
}SqStack;
void InitStack(SqStack &s)
{
    s.top=-1;//初始化:栈为空,top=-1
    s.data[MaxSize]={0};
}
bool Empty(SqStack s)
{
    if(s.top==-1)
    {
        return true;
    }
    return false;
}
bool  Push(SqStack &s,ElementType e)
{
    if(s.top==MaxSize-1){
        return false;
    }

    s.data[++s.top]=e;

    return true;
}
ElementType  Pop(SqStack &s)
{
    if(Empty(s)){
        return 0;
    }
    return  s.data[s.top--];
}
void split(char *a,int len,SqStack &s1,SqStack &s2)
{
    //将运算符和数字分离放入两个栈中
    int i=0;
    int cout=0;
    for ( i = 0; i <len ; ++i) {
        if(a[i]>='0'&&a[i]<='9')
        {
            Push(s1,a[i]);
        } else
        {
            Push(s2,a[i]);
            i=i+2;
        }
    }
}
int accurate_num(SqStack &s1)
{
    int a;
    a= Pop(s1)-48;
    while(a==0)
    {
        a=(int) Pop(s1)-48;
        a*=10;
    }
    return a;
}
int operat(SqStack &s1,SqStack &s2)
{
    int sum=0,a,c;
    ElementType op;
    a= accurate_num(s1);//先数
    c=accurate_num(s1);//后的数
    op= Pop(s2);//运算符
    if(op=='A')
    {
        sum=a+c;
    } else if(op=='S')
    {
        sum=a-c;
    } else{
        sum=a*c;
    }
    a=sum;
    while (Empty(s1)!= true)
    {
        c=accurate_num(s1);
        op=Pop(s2);
        if(op=='A')
        {
            sum=a+c;
        } else if(op=='S')
        {
            sum=a-c;
        } else{
            sum=a*c;
        }
        a=sum;
    }
    return sum;
}
int main() {
    SqStack s1,s2;
    InitStack(s1);
    InitStack(s2);
    int i=0;
    char c[100]={0};
    char d;
    int n,j=0;
    scanf("%d",&n);
    scanf("%c",&d);//吃掉换行符
    while (j<n)//只存数字和操作符
    {
        scanf("%c",&d);
        if(d=='\n')
        {
            j++;
        }
        if(d==' '||d=='\n'){
            continue;
        } else{
            c[i]=d;
//            printf("%c ",c[i]);
            i++;
        }
    }
    split(c,i,s1,s2);

    printf("%d",operat(s1,s2));
    return 0;
}

【问题描述】

假设给定的整数栈初始状态为空,栈的最大容量为100。从标准输入中输入一组栈操作,按操作顺序输出出栈元素序列。栈操作:1表示入栈操作,后跟一个整数(不为1、0和-1)为入栈元素;0表示出栈操作;-1表示操作结束。

【输入形式】

从标准输入读取一组栈操作,入栈的整数和表示栈操作的整数之间都以一个空格分隔。

【输出形式】

在一行上按照操作的顺序输出出栈元素序列,以一个空格分隔各元素,最后一个元素后也要有一个空格。如果栈状态为空时进行出栈操作,或栈满时进行入栈操作,则输出字符串“error”,并且字符串后也要有一空格。所有操作都执行完后,栈也有可能不为空。

【样例输入】

1 3 1 5 1 7 0 0 1 8 0 1 12 1 13 0 0 0 0 1 90 1 89 0 -1

【样例输出】

7 5 8 13 12 3 error 89  

【样例说明】

入栈元素依次为3、5、7,然后有两次出栈动作,所以先输出7和5,这时栈中只有元素3;之后元素8入栈,又出栈,输出8;随后元素12和13入栈,再进行4次出栈操作,输出13、12和3,这时栈为空,再进行出栈操作会输出error;最后90和89入栈,进行一次出栈操作,输出89,栈中剩余1个元素。

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef int ElementType ;
typedef struct
{
    ElementType data[MaxSize];
    int top;//始终指向栈顶,也就是最后一个加进来的变量
}SqStack;
void InitStack(SqStack &s)
{
    s.top=-1;//初始化:栈为空,top=-1
}
bool Empty(SqStack s)
{
    if(s.top==-1)
    {
        return true;
    }
    return false;
}
bool  Push(SqStack &s,ElementType e)
{
    if(s.top==MaxSize-1){
        return false;
    }
    s.data[++s.top]=e;
    return true;
}
bool  Pop(SqStack &s,ElementType &e)
{
    if(Empty(s)){
        return false;
    }
    e=s.data[s.top--];
    return true;
}

int main() {
    SqStack s;
    InitStack(s);
   int n=0;
   bool flag;
   ElementType e;
    while (1)
    {
        scanf("%d",&n);
        if(n==1)
        {
            scanf("%d",&n);
            flag=Push(s,n);
            if(flag!= true)
            {
                printf("error ");
            }
        } else if(n==0){
           flag=Pop(s,e);
           if(flag)
           {
               printf("%d ",e);
           } else{
               printf("error ");
           }
        } else{
            break;
        }
    }

    return 0;
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值