new to programming. I need to use queues and link lists to solve this problem. I am having trouble with the logic because I am unaware of all the tricks you can do with structures and how to implement them along with this new topic. I am not trying to cheat just need the general direction of what I am missing here. problem is the time the customer enters the line is added along with the number of shopped items after their name plus the cashier time to give a final checkout length. Then they must be put into ascending order. That is where I am tripped up. How do I take this input in a queue with all this information then change the order after the fact? hints greatly appreciated.
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.
#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 minIndex( queue *q, int sortedIndex);
int isEmpty(queue *q);
void initialize(queue *q);
void insertMinToFront ( queue *q, int minIndex);
void isSorted(queue *q, int numCustomers);
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 minIndex( queue *q, int sortedIndex){
int min_index = -1;
int min_val = INT_MAX;
int n = q->size;
for (int i = 0; i<n; i++){
int current = q->front;
dequeue(q);
if(current <= min_val && i <= sortedIndex){
min_index = i;
min_val = current;
}
enqueue(q, current);
}
return min_index;
}
void insertMinToFront ( queue *q, int minIndex){
int minVal;
int n = q->size;
for (int i = 0; i < n; i++)
{
int curr = q->front;
dequeue(q);
if (i != minIndex)
enqueue(q, curr);
else
minVal = curr;
}
enqueue(q, minVal);
}
void isSorted(queue *q, int numCustomers) {
for ( int i = 0; i < numCustomers; i++){
int min_index = minIndex(q, q->size - i );
insertMinToFront(q, min_index);
}
}
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 > 12){
return 0;
}
struct customer newCustomer[12];
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);
}
isSorted(q, numCustomers);
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;
}