weixin_63114271 2022-03-27 18:06 采纳率: 40%
浏览 129
已结题

ptaC语言数据结构列车厢调度问题 代码写出来了但是总是找不到错误在哪

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

img

img

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize 26
typedef struct
{
char *base;
int front;
int rear;
} queue;
typedef struct
{
char *base;
char *top;
} stack;
void initqueue(queue *q)
{
q->base = (char *)malloc(sizeof(char) * maxsize);
if (!q->base)
printf("error");
q->front = q->rear;
}
int enqueue(queue *q)
{
char a[30];
int i;
scanf("%s", a);
for (i = 0; a[i] != '\0'; i++)
{
q->base[q->rear] = a[i];
q->rear = q->rear + 1;
}
return strlen(a);
}
char dequeue(queue *q)
{
char e = q->base[q->front];
q->front = q->front + 1;
return e;
}
void initstack(stack *s)
{
s->base = (char *)malloc(sizeof(char) * maxsize);
s->top = s->base;
}
void push(stack *s, char e)
{
*(s->top) = e;
s->top++;
}
char pop(stack *s)
{
char e;
e = *--s->top;
return e;
}
int isempty_s(stack *s)
{
if (s->base == s->top)
return 0;
else
return 1;
}
int isempty_q(queue *q)
{
if (q->front == q->rear)
return 0;
else
return 1;
}
int main()
{
queue q;
initqueue(&q); //创建队列 存储车站1
int length = enqueue(&q);
getchar();
stack s; //创建栈 存储车站2
initstack(&s);
int i;
char arr[30]; //创建数组
for (i = 0; i < length; i++)
scanf("%c", &arr[i]);
i = 0;
int j=0;
char *aa[100];
while (isempty_q(&q))
{
if (arr[i] == q.base[q.front])
{
dequeue(&q);
//printf("1->2\n");
aa[j]="1->2\n";
j++;
i++;
}
else
{
//printf("1->3\n");
aa[j]="1->3\n";
j++;
char e = dequeue(&q);
push(&s, e);
}
}
while(isempty_s(&s))
{
char e = pop(&s);
if(e!=arr[i])
{
printf("Are you kidding me?\n");
return 0;
}
else{
i++;
//printf("3->2\n");
aa[j]="3->2\n";
j++;
}
}
for(i=0;i<j;i++)
{
printf("%s",aa[i]);
}
return 0;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月4日
    • 创建了问题 3月27日

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料