即使在 "if" 语句为真之后,每次都会执行 "else" 块
"else" block is executed every time even after the "if" statement is true
在删除函数中,即使在if
语句为真之后,每次都会执行else
部分。我该如何解决这个问题?
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int front=-1,rear=-1;
int Q[MAX];
void insert();
void delete();
void display();
void peek();
int main(){
int n;
while(1){
printf("Enter your operation:-\n1.Insert\n2.Delete\n3.Peek\n4.Display\n5.Exit\n");
scanf("%d",&n);
switch(n){
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
}
}
return 0;
}
void insert(){
int i;
printf("Enter your number:\n");
scanf("%d",&i);
if(rear==MAX-1)
printf("The queue is overflow");
else if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
Q[rear]=i;
}
void delete(){
int v;
if(front==-1 || front>rear)
printf("Queue is underflow\n");
else
v=Q[front];
front++;
printf("Your deleted number is:%d\n",v);
}
void peek(){
if(front==-1 || front>rear)
printf("Queue is underflow\n");
else
printf("The first number is:%d\n",Q[front]);
}
void display(){
int i;
if(front==-1 || front>rear)
printf("Queue is underflow\n");
else
printf("Your created Queue is:\n");
for(i=front;i<=rear;i++){
printf("%5d\n",Q[i]);
}
}`
你需要使用复合语句作为示例
else
{
v=Q[front];
front++;
printf("Your deleted number is:%d\n",v);
}
否则这段代码片段
else
v=Q[front];
front++;
printf("Your deleted number is:%d\n",v);
相当于
else
{
v=Q[front];
}
front++;
printf("Your deleted number is:%d\n",v);
请注意,例如函数 insert as is 有一个错误
void insert(){
int i;
printf("Enter your number:\n");
scanf("%d",&i);
if(rear==MAX-1)
printf("The queue is overflow");
else if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
Q[rear]=i;
}
你的意思好像是
void insert(){
if(rear==MAX-1)
{
printf("The queue is overflow");
}
else
{
int i;
printf("Enter your number:\n");
scanf("%d",&i);
if( front==-1 )
front = 0;
else
rear++;
Q[rear]=i;
}
}
通常错误的原因之一是代码格式不正确。
在删除函数中,即使在if
语句为真之后,每次都会执行else
部分。我该如何解决这个问题?
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int front=-1,rear=-1;
int Q[MAX];
void insert();
void delete();
void display();
void peek();
int main(){
int n;
while(1){
printf("Enter your operation:-\n1.Insert\n2.Delete\n3.Peek\n4.Display\n5.Exit\n");
scanf("%d",&n);
switch(n){
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
}
}
return 0;
}
void insert(){
int i;
printf("Enter your number:\n");
scanf("%d",&i);
if(rear==MAX-1)
printf("The queue is overflow");
else if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
Q[rear]=i;
}
void delete(){
int v;
if(front==-1 || front>rear)
printf("Queue is underflow\n");
else
v=Q[front];
front++;
printf("Your deleted number is:%d\n",v);
}
void peek(){
if(front==-1 || front>rear)
printf("Queue is underflow\n");
else
printf("The first number is:%d\n",Q[front]);
}
void display(){
int i;
if(front==-1 || front>rear)
printf("Queue is underflow\n");
else
printf("Your created Queue is:\n");
for(i=front;i<=rear;i++){
printf("%5d\n",Q[i]);
}
}`
你需要使用复合语句作为示例
else
{
v=Q[front];
front++;
printf("Your deleted number is:%d\n",v);
}
否则这段代码片段
else
v=Q[front];
front++;
printf("Your deleted number is:%d\n",v);
相当于
else
{
v=Q[front];
}
front++;
printf("Your deleted number is:%d\n",v);
请注意,例如函数 insert as is 有一个错误
void insert(){
int i;
printf("Enter your number:\n");
scanf("%d",&i);
if(rear==MAX-1)
printf("The queue is overflow");
else if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
Q[rear]=i;
}
你的意思好像是
void insert(){
if(rear==MAX-1)
{
printf("The queue is overflow");
}
else
{
int i;
printf("Enter your number:\n");
scanf("%d",&i);
if( front==-1 )
front = 0;
else
rear++;
Q[rear]=i;
}
}
通常错误的原因之一是代码格式不正确。