1

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;
}
3
  • 1
    hints, get bits working at a time. Make sure you can read the inut data into an array of structs before trying other logic Commented Jun 1, 2020 at 18:34
  • 1
    hint2, if you dont know how to use your debugger, stop right now and find out Commented Jun 1, 2020 at 18:34
  • In general you have to choose when to sort your queue, in my opinion in this case you should insert your elements directly in the right position (kind of like "insertion sort"). Commented Jun 1, 2020 at 18:37

1 Answer 1

1

Here's a quick guide on how to approach this sort of problem. There are some actual bugs in your code, but they're not what you asked about.

I need to use queues and link lists

OK, you know which data structures you need. So - implement them.

Hopefully you have some textbook which describes them, but if not, you can just start by reading Wikipedia, eg. on linked lists which has diagrams and pseudocode.

Containers have well-defined behaviour and a bounded set of natural operations (eg. insert or append or push_front). You can write, and test, a linked list of integers (or whatever) independently of the rest of your problem.

I am unaware of all the tricks you can do with structures

You shouldn't need any tricks - just write a structure capable of containing the input fields.

Then they must be put into ascending order

You know this is called sorting, right? If you didn't there's your key search term. Look for sort or, if you're allowed to use stdlib and an array, qsort.

How do I take this input in a queue with all this information then change the order after the fact?

The natural operations on a queue are basically push (to the back) and pop (from the front). This doesn't lend itself well to sorting.

If your queue is implemented on top of a linked list, you'd perform the sort on the underlying linked list (although it definitely won't be a qsort, at least not directly).

The alternative is to use a priority queue, that keeps itself sorted as you insert. This probably isn't what you're being asked for though, since it wouldn't be implemented with a linked list.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. this was very helpful. I realized my problem is I don't know how to enqueue a struct with non integer values. is it as simple as making a pointer in the struct and using the pointer to enqueue the struct. How do I sort it once it is enqueued? yes this is a new concept for me.
You're thinking about an intrusive linked list. The alternative is that your list has its own node struct, which contains a copy of the object you actually want to store. And the simplest way to sort a linked list is bubble sort. It's not very efficient, but it is easy to implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.