我試圖自己制作一個佇列程式,但是當我將一個元素排入佇列然后顯示佇列時,我發現佇列中已經存在一個額外的元素。此外,由于垃圾編號占用了佇列中的空間,我無法添加我根據佇列大小決定的元素數量。這是它的代碼:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
void enqueue();
void dequeue();
void display();
int main(){
int size;
printf("Enter the size of the queue : ");
scanf("%d", &size);
int choice, element;
int queue[size];
int front = 0;
int rear = 0;
while(true){
printf("Queue Operations\nPress 1 for Enqueue\nPress 2 for Dequeue\nPress 3 for Display\nPress 4 for exit\n");
scanf("%d", &choice);
switch(choice){
case 1:{
if(rear == size - 1){
printf("Overflow!\n");
}
else{
printf("Enter the element you want to enqueue : ");
scanf("%d", &element);
queue[rear] = element;
rear ;
}
break;
}
case 2:{
if(front == rear){
printf("Underflow!\n");
}
else{
printf("The element deleted is %d\n", queue[front]);
front = 1;
size =1;
}
break;
}
case 3:{
if(front == rear){
printf("Queue is empty.\n");
}
else{
for(int i = front; i <= rear; i ){
printf("%d ", queue[i]);
}
printf("\n");
}
break;
}
case 4:{
exit(0);
}
default:{
printf("Invalid Choice.\nTry again.\n");
break;
}
}
}
return 0;
}
uj5u.com熱心網友回復:
你的出隊操作是錯誤的。首先,您永遠不需要更改front
變數,該變數將/應該始終為零。其次,在使用給定值初始化陣列之后,您不得更改 的值。size
相反,您的出隊操作應該簡單地將佇列中的每個剩余條目移動到比當前索引少一的位置。您可以回圈執行此操作,如下所示:
for (int i = 0; i < rear; i) queue[i] = queue[i 1];
但是,您可以通過呼叫memmove
函式更有效地執行此操作:
memmove(queue, queue 1, sizeof(int) * rear);
無論您選擇哪種方式,您都需要rear
在輪班后遞減。
此外,for
顯示選項的回圈條件應該是i < rear
,而不是i <= rear
; 并且,您的溢位檢查應該是 for rear == size
。
這是應用我上面概述的更正的代碼的返工:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h> // For "memmove"
int main() {
int size;
printf("Enter the size of the queue : ");
scanf("%d", &size);
int choice, element;
int queue[size];
int rear = 0;
while (true)
{
printf("Queue Operations:\n"
"Press 1 for Enqueue\n"
"Press 2 for Dequeue\n"
"Press 3 for Display\n"
"Press 4 for exit\n"
);
scanf("%d", &choice);
switch (choice) {
case 1: {
if (rear == size) { // Note: Using size not size - 1
printf("Overflow!\n");
}
else {
printf("Enter the element you want to enqueue : ");
scanf("%d", &element);
queue[rear] = element;
rear ;
}
break;
}
case 2: {
if (rear == 0) {
printf("Underflow!\n");
}
else {
printf("The element deleted is %d\n", queue[0]);
// for (int i = 0; i < rear; i) queue[i] = queue[i 1];
memmove(queue, queue 1, sizeof(int) * rear);
rear--;
}
break;
}
case 3: {
if (rear == 0) {
printf("Queue is empty.\n");
}
else {
for (int i = 0; i < rear; i ) { // Note: i < rear not i <= rear
printf("%d ", queue[i]);
}
printf("\n");
}
break;
}
case 4: {
exit(0);
}
default: {
printf("Invalid Choice.\nTry again.\n");
break;
}
}
}
}
uj5u.com熱心網友回復:
for回圈中的條件
for(int i = front; i <= rear; i ){
printf("%d ", queue[i]);
}
是不正確的。至少你必須寫
for(int i = front; i < rear; i ){
printf("%d ", queue[i]);
}
還有這個說法
size =1
沒有意義,因為動態分配的陣列的大小是固定的。
以及這個 if 陳述句中的條件
case 1:{
if(rear == size - 1){
printf("Overflow!\n");
}
也是不正確的。你應該寫
case 1:{
if(rear == size){
printf("Overflow!\n");
}
注意你應該做一個回圈佇列。為此,您應該再引入一個變數,例如full
在rear
等于front
且佇列不為空時設定。
更重要的是,最好引入一個描述佇列的結構。
這是一個演示程式,展示了它是如何完成的。
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int *a;
size_t size;
size_t front;
size_t rear;
int full;
};
int init( struct Queue *queue, size_t n )
{
*queue = ( struct Queue ){ NULL, 0, 0, 0, 0 };
int success = 0;
if (n)
{
queue->a = malloc( n * sizeof( int ) );
if (( success = queue->a != NULL ))
{
queue->size = n;
}
}
return success;
}
void clear( struct Queue *queue )
{
free( queue->a );
*queue = ( struct Queue ){ NULL, 0, 0, 0, 0 };
}
int enqueue( struct Queue *queue, int value )
{
int success = queue->size != 0 && !queue->full;
if (success)
{
queue->a[queue->rear ] = value;
queue->rear %= queue->size;
queue->full = queue->front == queue->rear;
}
return success;
}
int dequeue( struct Queue *queue, int *value )
{
int success = queue->front != queue->rear || queue->full;
if (success)
{
*value = queue->a[queue->front ];
queue->front %= queue->size;
queue->full = 0;
}
return success;
}
void display( const struct Queue *queue )
{
if (queue->front == queue->rear && !queue->full)
{
puts( "The queue is empty." );
}
else
{
printf( "The queue is " );
size_t current = queue->front;
do
{
printf( "%d ", queue->a[current ] );
current %= queue->size;
} while (current != queue->rear);
putchar( '\n' );
}
}
int is_full( const struct Queue *queue )
{
return queue->full;
}
int main( void )
{
struct Queue queue;
printf( "Enter the size of the queue: " );
size_t n = 0;
if (scanf( "%zu", &n ) != 1 || !init( &queue, n ))
{
puts( "Error. Can not reserve memory for the queue." );
}
else
{
enum { Exit = 0, Enqueue = 1, Dequeue = 2, Display = 3 };
size_t n = 0;
do
{
puts( "Queue Operations\n\n"
"Press 1 for Enqueue\n"
"Press 2 for Dequeue\n"
"Press 3 for Display\n"
"Press 0 for Exit\n" );
printf( "Your choice: " );
if (scanf( "%zu", &n ) != 1) break;
switch (n)
{
case Exit:
{
break;
}
case Enqueue:
{
if (is_full( &queue ))
{
puts( "\nThe queue is full. You can not add a new value." );
}
else
{
printf( "\nEnter the element you want to enqueue : " );
int value;
if (scanf( "%d", &value ) != 1)
{
puts( "You interrupted the input" );
}
else
{
enqueue( &queue, value );
printf( "The value %d is added to the queue.\n", value );
}
}
break;
}
case Dequeue:
{
int value;
if (!dequeue( &queue, &value ))
{
puts( "Error. The queue is empty." );
}
else
{
printf( "\nThe removed element is %d\n", value );
}
break;
}
case Display:
{
putchar( '\n' );
display( &queue );
break;
}
default:
{
puts( "Invalid input." );
break;
}
}
if (n == 0) break;
putchar( '\n' );
} while (n != 0);
clear( &queue );
}
}
享受!:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492620.html
下一篇:從陣列中洗掉包含特定字母的元素