gpt4 book ai didi

c - 我的循环队列实现无法正常工作

转载 作者:行者123 更新时间:2023-11-30 18:38:50 25 4
gpt4 key购买 nike

我创建了一个程序来实现具有插入、删除和显示功能的循环队列。插入工作正常,删除也工作正常,但是一旦我尝试在删除后输入数字,就不会显示任何内容。这是我的源代码:

#include<stdio.h>
#include<conio.h>
#define SIZE 5
int front = -1;
int rear = -1;
int queue[SIZE];
void enqueue(int item);
int dequeue();
void display();
void main()
{
int item, choice, cont = 1;
clrscr();

while(cont == 1)
{
printf("\n1.Enqueue into queue.\n");
printf("\n2.Dequeue from queue.\n");
printf("\n3.display quesue elements\n");

printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the value of item: ");
scanf("%d",&item);
enqueue(item);
break;

case 2:
item = dequeue();
if(item != NULL)
{
printf("\nItem dequeued: %d\n",item);
}
break;
case 3:
display();
break;

default:
printf("\nInvalid choice.\n");
break;
}

printf("\nDo you want to continue (1/0): ");
scanf("%d",&cont);
}

getch();
}


void enqueue(int item)
{

if(front==0 && rear==SIZE-1)
printf("\n Queue OverFlow Occured");
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=item;

}
else if(rear==SIZE-1 && front!=0)
{
rear=0;
queue[rear]=item;
}
else
{
rear++;
queue[rear]=item;
}


}
int dequeue()
{
int item = NULL;
if(front == -1 && rear == -1)
{
printf("\nQueue is empty. Dequeue not possible.\n");
}
else
{
item = queue[front];
queue[front] = NULL;

if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
}

}
return(item);
}
void display()
{
int i;
if(front==-1)
printf("\n No elements to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{
printf("\t %d",queue[i]);
}
}
}

最佳答案

检查此代码是否工作正常。

Few suggestions don't use conio.h [clrscr(), and getch()] these are not standards.

尝试下面的代码,它对我来说效果很好。

代码

    #include<stdio.h>
#define SIZE 5
int front = -1;
int rear = -1;
int queue[SIZE];
void enqueue(int item);
int dequeue();
void display();
int main()
{
int item, choice, cont = 1;

while(cont == 1)
{
printf("\n1.Enqueue into queue.\n");
printf("\n2.Dequeue from queue.\n");
printf("\n3.display quesue elements\n");

printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the value of item: ");
scanf("%d",&item);
enqueue(item);
break;

case 2:
item = dequeue();
if(item != NULL)
{
printf("\nItem dequeued: %d\n",item);
}
break;
case 3:
display();
break;

default:
printf("\nInvalid choice.\n");
break;
}

printf("\nDo you want to continue (1/0): ");
scanf("%d",&cont);
}

printf("");
return 0;
}


void enqueue(int item)
{

if(front==0 && rear==SIZE-1)
printf("\n Queue OverFlow Occured");
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=item;

}
else if(rear==SIZE-1 && front!=0)
{
rear=0;
queue[rear]=item;
}
else
{
rear++;
queue[rear]=item;
}


}
int dequeue()
{
int item = NULL;
if(front == -1 && rear == -1)
{
printf("\nQueue is empty. Dequeue not possible.\n");
}
else
{
item = queue[front];
queue[front] = NULL;

if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
}

}
return(item);
}
void display()
{
int i;
if(front==-1)
printf("\n No elements to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{
printf("\t %d",queue[i]);
}
}

}

输出

    Do you want to continue (1/0): 1

1.Enqueue into queue.

2.Dequeue from queue.

3.display quesue elements

Enter your choice: 3

The queue elements are:
5 5
Do you want to continue (1/0): 1

1.Enqueue into queue.

2.Dequeue from queue.

3.display quesue elements

Enter your choice: 1

Enter the value of item: 43

Do you want to continue (1/0): 1

1.Enqueue into queue.

2.Dequeue from queue.

3.display quesue elements

Enter your choice: 3

The queue elements are:
5 5 43
Do you want to continue (1/0): 1

1.Enqueue into queue.

2.Dequeue from queue.

3.display quesue elements

Enter your choice: 2

Item dequeued: 5

Do you want to continue (1/0): 1

1.Enqueue into queue.

2.Dequeue from queue.

3.display quesue elements

Enter your choice: 3

The queue elements are:
5 43
Do you want to continue (1/0):

关于c - 我的循环队列实现无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32300071/

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