2022-03-27 18:06

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

我想要达到的结果

#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;
}