私はトイレを待っている人々を保持するキューを実装しようとしていますが、トイレは男性と女性の両方のためのものですが、男性がいるときは女性は入ることができず、女性がいるときは男性は入ることができません.入力。これは私の問題です。(それは私のクラスの1つのデモであり、ほとんど学術的ではありません)
トイレとキューに人を挿入することはできますが(女性が入ろうとしてトイレに男性がいる場合、彼女はキューに追加されます)、キューから人を取り出してトイレに挿入することはできませんエントリー資格があります。これが私のコードです。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements in queue are: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty\n");
}
int main() {
int man_busy = 0;
int woman_busy = 0;
int input = 0;
int i = 0;
printf("\n(1) Man enters\n");
printf("(2) Woman enters\n");
printf("(3) Man leaves\n");
printf("(4) Woman leaves\n");
printf("\nEmpty!\n");
for(i=0; i<20; i++) {
scanf("%d", &input);
if(input == 1){
if(woman_busy > 0){
printf("Man Can't enter when women are present\n");
printf("You will have to wait in the queue\n");
push(input);
display();
}
else if(woman_busy == 0) {
man_busy = man_busy + 1;
printf("Occupied By Man: %d\n", man_busy);
}
}
else if(input == 2) {
if(man_busy > 0){
printf("Woman Can't enter when men are present\n");
printf("You will have to wait in the queue\n");
push(input);
display();
}
else if(man_busy == 0) {
woman_busy = woman_busy + 1;
printf("Occupied By Woman: %d\n", woman_busy);
}
}
else if(input == 3) {
man_busy = man_busy - 1;
if (man_busy == 0 && woman_busy == 0){
printf("Empty!\n");
delQueue();
display();
}
else if (man_busy < 0) {
printf("Invalid command!\n");
man_busy = man_busy + 1;
}
else {
printf("Occupied By Man: %d\n", man_busy);
}
}
else if(input == 4) {
woman_busy = woman_busy - 1;
if (man_busy == 0 && woman_busy == 0) {
printf("Empty!\n");
delQueue();
display();
}
else if (woman_busy < 0) {
printf("Invalid command!\n");
woman_busy = woman_busy + 1;
}
else {
printf("Occupied By Woman: %d\n", woman_busy);
}
}
}
return 0;
}