Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to make a function for Menu 3, it said that:
If I choose Next Queue (Menu 3), then:
- Validate if there’s no data, show "There is no queue yet!" message
------------------------------
There is no queue yet!

Press enter to continue.....
------------------------------

- Otherwise, remove the frontmost queue based on its priority and show the data
-----------------------------------------------
The next patient is:
Name        : Mr. Budi
Age         : 34
Description : Serious injury from car accident
Code        : Red

Press enter to continue.....
-----------------------------------------------


What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Patient{
 int data;
 int age;
 int prior;
 char name[25];
 char symptoms[50];
 char code[10];
};

struct tnode{
 struct Patient data;
 struct tnode *next;
};

struct tnode *head = NULL, *tail = NULL;

void pushHead(struct Patient data)
{
 struct tnode newPatient = (struct tnode)malloc(sizeof(struct tnode));
 newPatient->data = data;
 newPatient->next = NULL;
 
 if(head == NULL)
 {
  head = newPatient;
 }
 else
 {
  struct tnode *curr = head;
  while(curr->next != NULL)
  {
   curr = curr->next;
  }
  curr->next = newPatient;
 }
}

void pushTail(struct Patient data)
{
 struct tnode newPatient =(struct tnode)malloc(sizeof(struct tnode));
 newPatient->data = data;
 newPatient->next = NULL;
 
 if(head == NULL)
 {
  head = tail = newPatient;
 }
 else
 {
  tail->next = newPatient;
  tail = newPatient;
 }
}

void viewList()
{
 if(head == NULL){
  printf("There is no queue yet!\n");
  return;
 }
 else{
  int i = 1;
  struct tnode *curr = head;
  printf("Patient List\n");
  printf("%-2s | %-25s | %-5s | %-25s | %-10s |\n", "No", "Name", "Age", "Description", "Code");
  while(curr != NULL){
   printf("%-2i | %-25s | %-5d | %-25s | %-10s |\n", i, curr->data.name, curr->data.age, curr->data.symptoms, curr->data.code);
   curr = curr->next;
   i++;
  }
  puts("");
 }

}
void nextQueue()
{
 struct tnode *curr = head;
  while(curr != NULL)
  {
   printf("Name        : %s\n", curr->data.name);
   printf("Age         : %s\n", curr->data.age);
   printf("Description : %s\n", curr->data.symptoms);
   printf("Code        : %s\n", curr->data.code);
   
   curr = curr->next;
  }

 
}
int main()
{
 char name[25];
 int list;
 do
 {
  printf("Bluejack Hospital\n");
  printf("=================\n");
  printf("1. Insert\n");
  printf("2. View\n");
  printf("3. Next Queue\n");
  printf("4. Exit\n");
  printf(">> ");
  scanf("%d", &list);
  struct Patient data;
  switch(list)
  {
   case 1:
    printf("Input patient name: ");
    getchar();
    scanf("%[^\n]", data.name); 
    printf("Input patient age: ");
    scanf("%d", &data.age);
    getchar();
    printf("Input description: ");
    scanf("%[^\n]", data.symptoms);
    getchar();
    printf("Input code(Red/Yellow/Green): ");
    scanf("%s", data.code);
    getchar();
    pushHead(data);
    break;
   case 2: 
    viewList();
    break;
   case 3:
    nextQueue();
    break;
  }
 }
 while(list != 4);
 
 return 0;
}
Posted
Updated 20-Mar-22 4:01am
Comments
Richard MacCutchan 20-Mar-22 8:31am    
The instructions are clear:
If the head of the queue is NULL then print the empty message and return.
Otherwise, print the details of the top queue item and remove it from the queue.

Your version prints all items in the queue and leaves it intact.
Bruhhhh 20-Mar-22 8:39am    
I dont know how to make its function
Richard MacCutchan 20-Mar-22 8:45am    
Think about it.

The first thing is to check if head == NULL. That is just a simple if block.
If it is not null, then print the details - you already have that code.

Next remove that item from the queue: So get the next pointer from the item and set that into the head pointer. Then use free to delete this item from memory.
Bruhhhh 20-Mar-22 9:56am    
I still didnt get it
If(head == NULL){
printf();
}
else{}

I got stucked
Richard MacCutchan 20-Mar-22 10:14am    
Read your instructions:
If the head of the queue is NULL then print the empty queue message and return.
Else:
Print the details of the element at the head of the queue. As I said, you already have this code. Then :
struct tnode* temp = head; // save the current item pointer
head = head->next; // point head to the next item in the queue
free(temp); // free this item.

1 solution

Quote:
I dont know how to make its function


While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900