【发布时间】:2021-09-05 16:07:47
【问题描述】:
我目前正在编写一个循环队列。现在我的代码只接受整数添加到队列中。我的目标是能够将 10 种不同珠宝的信息添加到队列中。
正如我所说,我的目标是编写一个珠宝商工作组织者的代码。用户输入客户名称、工件类型、重量、金属类型和金属规律。然后所有这些信息都存储在我队列的一个“插槽”中。这样珠宝商就知道工作的具体顺序(先进先出)
所以这是我的问题:如何将珠宝信息添加到队列的一个“插槽”中。
我的代码将整数添加到队列中,我想添加珠宝首饰结构的信息。我怎样才能做到这一点?
这是我目前所拥有的。
#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);
}
【问题讨论】:
-
请注意您没有提出任何问题。编辑问题并包含有关问题的信息
-
我在帖子中明确添加了这个问题。我希望我的问题现在更清楚了
-
OT:将您的母语中的单词用于变量和/或函数是一个真正的、真正的坏主意。使用有意义的英文单词,让全世界的程序员都能轻松阅读您的代码。
-
OT:同样适用于 cmets...使用英语
-
按您的要求做很容易。在 C 中,结构可以像整数一样被赋值、传递等等。所以只需将所有引用存储值的整数更改为结构。
标签: c data-structures struct queue