这是链队列的代码,其中有个小问题 调试说是113行错了,实在不知道哪里错了
从111行开始看就行了,从111行以下是想根据输入“+”,“-”字符实现入队、出队一个一个进行,求指导
从111行开始看就行了,从111行以下是想根据输入“+”,“-”字符实现入队、出队一个一个进行,求指导
#include <stdio.h> #define MAXLEN 10 typedef struct { int data[MAXLEN]; int front, rear; }SeQueue; SeQueue InitSeQueue() { SeQueue q; q.front = 0; q.rear = 0; return(q); } int Getfront_seq(SeQueue *q, int *x){ if (q->front == q->rear) return(0); else { *x = q->data[q->front]; return(1); } } int enterSeqQueue(SeQueue *q, int x) { if ((q->rear + 1) % MAXLEN == q->front){ printf("SeqQueue is full"); return(0); } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXLEN; return(1); } int Empty_seq(SeQueue *q) { return(q->front == q->rear); } int DelSeqQueue(SeQueue *q, int *x) { if (q->front == q->rear) return(0); else { *x = q->data[q->front]; q->front = (q->front + 1) % MAXLEN; return(1); } } void print(SeQueue q) { int n; if (q.front != q.rear) { printf("队列的元素输出:"); for (n = q.front; n <= q.rear - 1; n++) printf("%d ", q.data[n]); } else printf("队列为空!!!"); printf("\n"); } void main() { SeQueue queue; int n, y, z, i, j; int y1,z1; char ch = ""/""; char cha1 = ""?""; queue = InitSeQueue(); if (Empty_seq(&queue) != 0) printf("\n队列为空!"); else printf("\n队列非空!"); printf("\n输入入队元素的数目n:"); scanf("%d", &n); printf("输入%d个待入队的元素:\n", n); for (i = 1; i <= n; i++) { scanf("%d", &y); enterSeqQueue(&queue, y); } print(queue); Getfront_seq(&queue, &z); printf("当前队头元素:%d \n", z); print(queue); printf("输入出队元素的数目j(j要小于n):"); scanf("%d", &j); printf("出队的%d个元素为:", j); for (i = 1; i <= j; i++) { DelSeqQueue(&queue, &z); printf("%d ", z); } printf("\n"); print(queue); if (Empty_seq(&queue) != 0) printf("队列为空!\n"); else printf("队列非空!\n"); printf("**************************************************************"); printf("继续操作选择“*”"); scanf("%c", cha1); if (cha1 == ""*""){ printf("入队为“+”,出队为“-”\n"); while (cha1 == ""*""){ scanf("%c", ch); if (ch == ""+""){ printf("请输入入队元素:"); scanf("%d", &y1); enterSeqQueue(&queue, y1); print(queue); } else{ printf("请输入出队元素:"); DelSeqQueue(&queue, &z); printf("%d ", z); print(queue); } ch=getchar(); } cha1 = getchar(); }else{ printf("Say GoodBy !\n"); } }
解决方案:40分
scanf(“%c”, &cha1);