I need to enqueue a whole struct customer in the queue - I think. This is my homework. I am not asking for an answer. I need a mentor to explain what I am missing and what I have right per the assignment. I am very new. This is my second program. see below... thanks btw.
A store is having customers queue in several lines. After the cashier finishes helping a customer, he will survey all of the lines that are currently queued. Of all of the customers at the front of those lines, he’ll take the customer who has the fewest number of items. If there are two customers with the same number of items, he’ll take the customer who comes from the smaller line number. The lines are numbered 1 through 12. It’s possible that some of these lines will be empty, in which case these lines are ignored. The number of seconds the store clerk takes to check out a customer is 30 plus 5 times the number of items. Thus, if a customer has 8 times, the clerk would check her out in 30 + 8*5 = 70 seconds. The Problem You will write a program that reads in information about customers: which line they go to the back of (1 through 12), at what time (in seconds) they enter that line, and the number of items they have, and determines at what time each customer will check out.
Sample Input:
2
5
10 1 STEVEN 12
12 6 AHMAD 8
13 1 JENNY 40
22 6 JERMAINE 39
100000 12 AMALIA 53
6
100 1 A 100
200 2 B 99
300 3 C 98
400 4 D 97
500 5 E 96
600 6 F 95
Sample Output:
STEVEN from line 1 checks out at time 100.
AHMAD from line 6 checks out at time 170.
JERMAINE from line 6 checks out at time 395.
JENNY from line 1 checks out at time 625.
AMALIA from line 12 checks out at time 100295.
A from line 1 checks out at time 630.
F from line 6 checks out at time 1135.
E from line 5 checks out at time 1645.
D from line 4 checks out at time 2160.
C from line 3 checks out at time 2680.
B from line 2 checks out at time 3205.
Implementation Restrictions:
You must create a struct that stores information about a customer (name, number of items, line number, time entering line). Note that the storage of the line number is redundant, but is designed to ease implementation.
You must create a node struct for a linked list of customers. This struct should have a pointer to a customer struct, and a pointer to a node struct.
You must create a struct to store a queue of customers. This struct should have two pointers – one to the front of the queue and one to the back.
You must implement all of the lines that form as an array of size 12 (stored as a constant) of queues.
You must dynamically allocate memory as appropriate for linked lists.
Your queue must support the following operations: a. Enqueue b. Dequeue c. Return the front of the queue WITHOUT dequeing d. Empty (returns 1 if the queue is empty, 0 if it is not)
You must free memory appropriately. Namely, when you dequeue, you’ll free memory for a node, but you will NOT free memory for the customer. You will free this memory a bit later right after you calculate when that customer will finish checking out.
Due to the nature of the problem, when you process the input, you can add everyone into their appropriate lines right at the beginning, before checking anyone out.
This wouldn’t work in all simulations (some of which you have to do in time order), but because there is ONLY one check out line, you can get away with it. The only thing you have to be cognizant about is that when you select a line, if the current time is, 100 for example, and three lines have customers who arrived before time 100 and the other lines have customers in the front who arrived AFTER time 100, you have to ignore the customers in those lines who arrived after time 100. In the case that all the lines have customers who arrived after time 100, you would take the line which has a customer who arrived first. You are guaranteed no ties for arrival time so this would be unique.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
typedef char customerName[9];
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct queue
{
int size;
int front;
int back;
int *array;
unsigned capacity;
}queue;
typedef struct customer
{
customerName name;//1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
int isEmpty(queue *q);
void initialize(queue *q);
void initialize(queue *q)
{
q->size = 0;
q->front = -1;
q->back = -1;
}
int isEmpty(queue *q){
return (q->size == 0);//returns 1 if empty or 0 if false
}
int isFull(queue *q)
{ return (q->size == q->capacity); }
void enqueue(queue *q, int item)
{
if (isFull(q))
return;
q->back = (q->back + 1)%q->capacity;
q->array[q->back] = item;
q->size = q->size + 1;
}
int dequeue(queue *q)
{
if (isEmpty(q))
return 0;
int item = q->array[q->front];
q->front = (q->front + 1)%q->capacity;
q->size = q->size - 1;
return item;
}
int front(queue* q){
if(isEmpty(q)){
return 0;
}
return q->array[q->front];
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
initialize(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
struct customer newCustomer[1];
for ( int i = 0; i < numCustomers; i++){
scanf("%d", &newCustomer[i].time);
scanf("%d", &newCustomer[i].lineNumber);
scanf("%s", newCustomer[i].name);
scanf("%d", &newCustomer[i].numberItems);
enqueue(q, newCustomer[i].time);
enqueue(q, newCustomer[i].lineNumber);
}
for ( int i = 0; i < numCustomers; i++){
printf("%d %d %s %d\n", newCustomer[i].time, newCustomer[i].lineNumber, newCustomer[i].name, newCustomer[i].numberItems);
}
}
}
return 0;
}
Here is my latest code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
#define NUMLINES 12
int currtime = 0;
typedef char customerName[9];
typedef struct node
{
int data;
struct customer* data;
struct node* next;
}node;
typedef struct queue
{
node* front;
node* back;
}queue;
typedef struct customer
{
customerName name;//1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
typedef struct node *newNode;//define a node pointer
node createNode()
{
newNode temp;//declare node
temp = (newNode)malloc(sizeof(struct node));//allocate memory
temp->next = NULL;//next point to null
return *temp;// return the new node
}
struct queue* createQueue()
{
struct queue* q = (struct queue*)malloc(sizeof(struct queue));
q->front = q->back = NULL;
return q;
}
int isEmpty(queue *q){
return (q->back == NULL);//returns 1 if empty or 0 if false
}
void enqueue(queue *q, customer* data)
{
// Create a new LL node
struct node* temp = createNode(data);
// If queue is empty, then new node is front and back both
if (q->back == NULL) {
q->front = q->back = temp;
return;
}
// Add the new node at the end of queue and change back
q->back->next = temp;
q->back = temp;
}
void dequeue(queue *q)
{
if (q->front == NULL)
return;
// Store previous front and move front one node ahead
struct node* temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->back = NULL;
free(temp);
}
int front(queue* q){
if(isEmpty(q)){
return 0;
}
return q->front;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
initialize(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
struct customer newCustomer[11];
for ( int i = 0; i < numCustomers; i++){
scanf("%d", &newCustomer[i].time);
scanf("%d", &newCustomer[i].lineNumber);
scanf("%s", newCustomer[i].name);
scanf("%d", &newCustomer[i].numberItems);
enqueue(q, newCustomer[i].time);
enqueue(q, newCustomer[i].lineNumber);
}
for ( int i = 0; i < numCustomers; i++){
printf("%d %d %s %d\n", newCustomer[i].time, newCustomer[i].lineNumber, newCustomer[i].name, newCustomer[i].numberItems);
}
}
}
return 0;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
qinit(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
queue* customerArray = (queue*) malloc(sizeof(queue) * 12);
for ( int i = 0; i < numCustomers; i++){
customer* newCustomer = (customer*) malloc(sizeof(customer));
scanf("%d", &(newCustomer->time));
scanf("%d", &(newCustomer->lineNumber));
scanf("%s", newCustomer->name);
scanf("%d", &(newCustomer->numberItems));
enqueue(&customerArray[newCustomer->lineNumber - 1], newCustomer);
}
int totalTime = INT_MAX;
for(int i=0;i<12;i++)
{
customer* frontCustomer = qfront(&customerArray[i]);
if(totalTime < frontCustomer->time)
{
totalTime = frontCustomer->time;
}
free(frontCustomer);
}
while(numCustomers--) {
int customerToCheckOutLine = 0; int minNumberOfItems = INT_MAX;
for( int j=11 ; j>=0; j--){
customer* frontCustomer = qfront(&customerArray[j]);
if(frontCustomer->time <= totalTime)
{
if(frontCustomer->numberItems < minNumberOfItems)
{
customerToCheckOutLine = frontCustomer->lineNumber;
minNumberOfItems = frontCustomer->numberItems;
}
free(frontCustomer);
}
}
customer* customerToCheckOut = qfront(&customerArray[customerToCheckOutLine -1 ]);
totalTime += 30;
totalTime += (customerToCheckOut->numberItems) * 5;
dequeue(&customerArray[customerToCheckOutLine - 1]);
}
free(customerArray);
}
}
return 0;
}