I am trying to implement a queue that will hold people waiting in line for the bathroom but the thing is that the bathroom is for both men and women, but when men are present women can't enter and when women are present men can't enter. This is my problem. (Its a demo for one of my classes its not graded its barely academic)
I can insert people in the bathroom and into the queue (when a women tries to enter and there is a man in the bathroom she gets added to the queue) but I cant take the people from the queue and insert then into the bathroom when they are eligible to enter. Here is my code.
#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;
}