gpt4 book ai didi

c - C 中循环队列的打印

转载 作者:行者123 更新时间:2023-11-30 19:33:06 25 4
gpt4 key购买 nike

我试图正确地获取该程序的输出,但我无法做到。这是因为我无法在 insert() 函数中的行 "printf("Enter the string to be inserted =\n");" 之后输入任何字符串,尽管我使用 gets () 具有适当的头文件。我得到的输出是这样的:

Choice 1 : Enter element into Queue 
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
1
Enter the string to be inserted =
Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
1
Enter the string to be inserted =
Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
1
Enter the string to be inserted =
Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
3
The contents of the queue are



Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
2
Deleted string is =

Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
3
The contents of the queue are


Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
2
Deleted string is =

Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
3
The contents of the queue are

Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
2
Deleted string is =

Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
3
Queue is empty
Choice 1 : Enter element into Queue
Choice 2 : Delete element from Queue
Choice 3 : Display
Any other choice : Exit
Enter your choice
4

我编写的程序如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 5

int front = 0;
int rear = -1;
char queue_array[MAX][30];

void insert();
void Delete();
void display();

int main()
{
int choice;
while(1)
{
printf("Choice 1 : Enter element into Queue\n");
printf("Choice 2 : Delete element from Queue\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
printf("Enter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: insert();
break;
case 2: Delete();
break;
case 3: display();
break;
default:exit(0);
} // End of switch()
} // End of while()
} // End of main()

void insert()
{
char add_item[30];
if((front == 0 && rear == MAX - 1) || (front != 0 && rear == front - 1))
printf("Queue is full\n");
else
{
printf("Enter the string to be inserted = \n");
gets(add_item);
if(rear == MAX - 1 && front != 0)
{
rear = 0;
strcpy(queue_array[rear], add_item);
}
else
{
rear = rear + 1;
strcpy(queue_array[rear], add_item);
}
}
}

void Delete()
{
char del_item[30];
if(front == 0 && rear == -1)
{
printf("Queue is empty\n");
return;
}
if(front == rear)
{
strcpy(del_item, queue_array[front]);
front = 0;
rear = -1;
}
else if(front == MAX - 1)
{
strcpy(del_item, queue_array[front]);
front = 0;
}
else
{
front = front + 1;
strcpy(del_item, queue_array[front]);
}
printf("Deleted string is =\n");
puts(del_item);
} // End of delete()

void display()
{
int i, j;
if(front == 0 && rear == -1)
{
printf("Queue is empty\n");
return;
}
printf("The contents of the queue are ");
if(front > rear)
{
for(i = 0; i <= rear; i++)
puts(queue_array[i]);
for(j = front; j < MAX - 1; j++)
puts(queue_array[j]);
}
else
{
for(i = front; i <= rear; i++)
puts(queue_array[i]);
}
printf("\n");
} // End of display()

我真的很感激任何帮助。预先感谢:)

最佳答案

输入后,某些字符仍保留在标准输入缓冲区中(scanf 不读取换行符),请刷新标准输入缓冲区 fflush(stdin) 或使用 scanf("%s",添加项目)

此外,我可能会补充一点,gets 很容易出现缓冲区溢出问题,您最好使用 fgets

关于c - C 中循环队列的打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46312640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com