将结构添加到队列

Adding a Struct to a queue

我目前正在编写一个循环队列。现在我的代码只接受整数添加到队列中。我的目标是能够将 10 件不同珠宝的信息添加到队列中。

正如我所说,我的 objective 是为珠宝商工作组织者编写代码。用户输入客户名称、工件类型、重量、金属类型和金属法。然后所有这些信息都存储在我队列的一个“插槽”中。这样珠宝商就知道具体的工作顺序(先进先出)

所以这是我的问题:如何将一件珠宝的信息添加到队列的一个“插槽”中。

我的代码将整数添加到队列中,我想添加珠宝首饰结构的信息。我怎样才能做到这一点?

这是我目前所拥有的。

#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>

// macros:
 // INT MIN specifies the minimum value that can be used beyond that limit
#define QUEUE_EMPTY INT_MIN

// creating queue struct 
typedef struct{
 int *values;
 int head, tail, inputNumber, size;
} queue;

// declaring functions 
void iniQueue(queue *q, int sizeMax);
bool queueEmpty(queue *q);
bool queueFull(queue *q);
void deleteQueue(queue *q);
bool enqueue(queue *q, int values);
int dequeue(queue *q);




// creating jewel struct
typedef struct{
 char client[50];
 char partType[25];
 double weight;
 char metal[10];
 int law;
}part;

// queue init
void iniQueue(queue *q, int sizeMax){
 q->size = sizeMax;
 // Allocating memory to the array
 q->values = malloc(sizeof(int) * q->size);
 q->inputNumber = 0; // creating empty array
 q->head = 0;
 q->tail = 0;
}

// Func to check if queue is empty
  // returns true if inputs == 0
bool queueEmpty(queue *q){
 return(q->inputNumber == 0);
}

// Func to check is queue is full
  // returns true if inputs == size
bool queueFull(queue *q){
 return (q->inputNumber == q->size);
}

// Destroy queue (free queue)
  // this to avoid memory leaks (short lived data structures)
void deleteQueue(queue *q){
 free(q->values);
}

// ++ Enqueue ++
bool enqueue(queue *q, int values){
 // Check if the queue is full:
 if (queueFull(q)){
  return false; // the queue is already full
 }
  
 // if there is still space, add values
 q->values[q->tail] = values;
 // move indicates from the queue (the module is used to get the rest in case queue> = size)
   // the module replaces: if (queue> = size, then queue = 0)
 q->tail = (q-> tail + 1) % q->size;
// increase the input counter by one
 q->inputNumber++;
 return true;
}

// ++ Dequeue ++
int dequeue(queue *q){
 int result;

 // Checking if the queue is empty
 if(queueEmpty(q)){
  return QUEUE_EMPTY;
 }

 result = q->values[q->head];
 q->head = (q-> head + 1) % q->size;
 q->inputNumber--;

 return result;
}

// ++ Display ++ 
void display(queue *q){
 // check if it is empty
 if (QUEUE_EMPTY == true){
  printf("Your work-list is empty\n");
 }
 printf("The number of elements in the list are %d", inputNumber);
 printf("The elements of the Queue are: \n");
 //note: didn't displayed elements yet.
}


int main(){
 // Local variables
 int choice, add, t;

 // creating the queue
 queue q1;
 iniQueue(&q1, 10);

 // creating pieces of jewelery (to be filled by user)
 part p1,p2,p3,p4,p5,p6,p7,p8,p9,p10;

while(420){
 // Menu
 printf ("Welcome to your jewelry organizer! \n");
 printf ("1 - Add Job \n");
 printf ("2 - Complete Current Job \n");
 printf ("3 - View full job list \n");
 printf ("4 - Add Job \n");
 printf ("Enter the number to perform the desired action:");
 scanf("%d", &choice);

 // Actions
 switch (choice){
 case 1:
  printf("Enter a number to add to the Queue:");
  scanf("%d", &add);
  enqueue(&q1, add);
  break;
 
 case 2: 
  dequeue(&q1);
  break;

 case 3:
  display(&q1);
  // show list
  break;
 
 case 4: 
  exit(0);

 default:
  printf("Invalid choice ...");
    }
  }
 return(0);
}

给你:

#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>

// macros:
 // INT MIN especifica el minimo valor que se puede utilizar mas alla de ese limite 
#define QUEUE_EMPTY INT_MIN


// creating jewel struct
struct part{
  char client[50];
  char partType[25];
  double weight;
  char metal[10];
  int law;
};



// creating queue struct 
struct queue{
  int *values;
  int head, tail, inputNumber, size;
  struct part *p;
};
// declaring functions 


void iniQueue(struct queue *q, int sizeMax);
bool queueEmpty(struct queue *q);
bool queueFull(struct queue *q);
void deleteQueue(struct queue *q);
bool enqueue(struct queue *q, int values, struct part *p1);
struct part* dequeue(struct queue *q);


// queue init
void iniQueue(struct queue *q, int sizeMax){
 q->size = sizeMax;
 // acolcando el array
 q->values = malloc(sizeof(int) * q->size);
 q->inputNumber = 0; // creando arreglo vacío 
 q->head = 0;
 q->tail = 0;
 q->p = (struct part *)malloc(sizeof(struct part) * q->size);
}

// Func to check if queue is empty
  // regresa true si entradas == 0
bool queueEmpty(struct queue *q){
 return(q->inputNumber == 0);
}

// Func to check is queue is full 
  // regresa true si entradas ==  size 
bool queueFull(struct queue *q){
 return (q->inputNumber == q->size);
}

// Destroy queue (free queue)
  // esto para evitar memory leaks (short lived data structures)
void deleteQueue(struct queue *q){
 free(q->values);
 free(q->p);
}

// ++ Enqueue ++
bool enqueue(struct queue *q, int values, struct part *p1){
 // Checar si es queue esta lleno:
 if (queueFull(q)){
  return false; // el queue ya esta lleno
 }
 //si aun hay espacio, agregar values
 q->values[q->tail] = values;
 q->p[q->tail] = *p1;
 // mover indica de la tail (se usa el modulo para obtener el resto en caso de que tail >= size)
   // el modulo remplaza: if (tail >= size, entonces tail = 0)
 q->tail = (q-> tail + 1) % q->size;
 // aumentar por uno el contador de entradas
 q->inputNumber++;
 return true;
}

// ++ Dequeue ++
struct part* dequeue(struct queue *q){
 int resultado;
  struct part *result;

 // Checando si el queue está vacío
 if(queueEmpty(q)){
    printf("queue is empty.");
  return result;
 }

// resultado = q->values[q->head];
// q->head = (q-> head + 1) % q->size;
// q->inputNumber--;


result = &q->p[q->head];
  q->head = (q-> head + 1) % q->size;
  q->inputNumber--;
  
// return resultado;
  return result;
}

// ++ Display ++
void display (struct queue * q) {
  // check if it is empty
  if (QUEUE_EMPTY == true) {
    printf ("Your work-list is empty \n");
    return;
  }
  printf ("The number of elements in the list are %d", q->inputNumber);
  printf ("\nThe elements of the Queue are: \n");
  for (int i=0; i<q->inputNumber; i++) {
    printf("Details about product number : %d\n",i+1);
    printf("%s %s %lf %s %d\n",q->p[i].client,q->p[i].partType,q->p[i].weight,q->p[i].metal,q->p[i].law);
  }
}


int main(){
 // Variables locales
 int choice, add, t;

 //creando el queue 
 struct queue q1;
 iniQueue(&q1, 10);

 // creating peices of jewelery (to be filled by user)
 struct part *p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9,*p10;

  while (420) {
    // Menu
    printf ("\nWelcome to your jewelry organizer! \n");
    printf ("1 - Add Job \n");
    printf ("2 - Complete Current Job \n");
    printf ("3 - View full job list \n");
    printf ("4 - Exit \n");
    printf ("Enter the number to perform the desired action:");
    scanf ("%d", &choice);
    struct part part1;
    
  // Actions
  switch (choice) {
    case 1:
      printf("Enter Client name : ");
      scanf(" %s", part1.client);
      printf("Enter Part Type : ");
      scanf(" %s", part1.partType);
      printf("Enter Metal : ");
      scanf(" %s", part1.metal);
      printf("Enter Weight : ");
      scanf(" %lf", &part1.weight);
      printf("Enter law : ");
      scanf(" %d", &part1.law);
      enqueue(&q1, add,&part1);
      break;
    
    case 2:
      part1 = *dequeue(&q1);
      printf("%s %s %s %lf %d",part1.client,part1.partType,part1.metal,part1.weight,part1.law);
      break;
    
    case 3:
      display(&q1);
      // show list
      break;
    
    case 4:
      exit(0);
      
    default:
      printf ("Invalid choice ...");
  }
}
  return(0);
}