printf("Enter the value\n");
scanf("%d ",&data);
但是scanf
在printf
之前执行。可能是什么原因?printf scanf执行顺序正在交换
这是我的整个程序
#include<stdio.h>
#include<stdlib.h>
struct Queue{
int front,rear;
int capacity;
int *array;
};
void enqueue(struct Queue *q){
int data;
if(q->front == (q->rear+1)%q->capacity){
printf("Queue is full\n");
return;
}else{
printf("Enter the value\n");
scanf("%d ",&data);
q->rear = (q->rear+1) % q->capacity;
q->array[q->rear] =data;
if(q->front == -1)
q->front = q->rear;
}
}
void dequeue(struct Queue *q){
if(q->front == -1){
printf("Queue is Empty\n");
return;
}else{
printf("value dequeued %d \n",q->array[q->front]);
if(q->front == q->rear)
q->front = q->rear = -1;
else
q->front = (q->front+1)% q->capacity;
}
}
void printQueue(struct Queue *q){
if(q->front == -1){
printf("Queue is Empty\n");
return;
}else{
int f = q->front, r =q->rear;
while((f% (q->capacity)) != r)
{
printf ("\t %d", q->array[f]);
f++;
}
printf("\t %d",q->array[q->rear]);
printf("\n");
}
}
main(){
struct Queue *q = malloc(sizeof(struct Queue));
if(!q){
printf("Memory Error\n");
exit;
}
q->capacity = 3;
q->front= q->rear = -1;
q->array = malloc(q->capacity * sizeof(int));
if(!q->array){
printf("Memory Error\n");
exit;
}
int option;
char ch ='a';
while(1) {
printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
fflush(stdout);
scanf("%d",&option);
switch(option){
case 1:
enqueue(q);
break;
case 2:
dequeue(q);
break;
case 3:
printQueue(q);
break;
default:
printf("Wrong choice\n");
}
printf("Do you want to continue? press y to continue\n");
scanf("%s", &ch);
if(ch != 'y')break;
}
}
当我排队等待的输入,将printf语句被执行后,我的元素。 e你想继续吗? 理想的情况下,当我进入元素排队并按回车应该问我,你想继续,然后它应该等待输入,即“Y”。但是,只要我输入排队元素,它就等待输入'y',输入'y'后,'你要继续'执行
不,这不是[](http://www.google.com) –
我使用。 ubuntu的默认gcc编译器 – pst
为什么让你相信它正在被重新排序? –