How to sort list items by their priority in C?

点点圈 提交于 2020-02-05 08:39:07

问题


I want to sort list items by their priority, which the user types in, and it does that well. However, when there is more than one item with the same priority, it doesn't sort them by order of arrival like it's supposed to.

I'm sorry if I'm not making this clear enough so you can understand. The names of the variables are in portuguese, so if you don't understand someting, please ask.

Here is the code:

typedef struct pedido pedido, *ppedido;

struct pedido{
    char id[5];
    int prioridade;
    int mesa, n_pratos;
    struct prato *prato[TAM];
    ppedido prox;
};

struct prato{
    char id[5];
};

ppedido novo_pedido(ppedido lista)
{
    ppedido novo, aux, anterior = NULL;
    int i;

    novo = (struct pedido*)malloc(sizeof(pedido));

    if(novo == NULL){
        printf("Erro na alocacao de memoria...\n");
        return;
    }

    printf("Number of menus: ");
    scanf("%d", &novo->n_pratos);

    printf("Table number: ");
    scanf("%d", &novo->mesa);

    printf("Priority of request? ");
        scanf("%d", &novo->prioridade);

        printf("Introduza o ID do pedido: ");
        scanf("%s", &novo->id);

    for(i=0;i<novo->n_pratos;i++){
        printf("ID of menu %d: ", i+1);  //something like "M1, M4..." doesn't matter
        scanf("%s", &novo->prato[i]);
        fflush(stdin);
    }

    novo->prox=NULL;

    if(lista == NULL || novo->prioridade > lista->prioridade) { 
        novo->prox = lista; 
        lista = novo; 
    }
    else
    { 
        aux = lista;

        while(aux != NULL && novo->prioridade < aux->prioridade)   //this is where it should be sort requests by their priority and order of arrival
            aux = aux->prox; 
        novo->prox = aux->prox; 
        aux->prox = novo;
    }
    return lista;
}

回答1:


I think you want to change this:

while(aux != NULL && novo->prioridade < aux->prioridade)

To:

while(aux->prox != NULL && novo->prioridade <= aux->prox->prioridade)

This way it will go past all of the the ones of the same priority and be put closer to the end of the list. This will keep a reference to aux when you traverse to the end of the list.

I assume in your search you stop as soon as you find the highest priority.

This assumes the order of entry into the list is the same as the order of arrival.




回答2:


I don't see any sorting going on in your posted code, but most sorting algorithms are not stable. This means that they do not generally preserve the order of elements that are considered "equal".

You either need to switch to a stable sort, or change your comparison function to consider "arrival time" when priorities are equal.




回答3:


So let's say we have priority, item tuples (priority, item) and item is an character for our example.

NULL

List starts null. We start inserting.

(1, x)
NULL

...

(3, z)
(2, y)
(1, x)
NULL

Now we insert (0, a).

The if evaluates to false, aux = lista pointing to (3, z).

The while advances until aux points to NULL.

Then:

    novo->prox = aux->prox; 
    aux->prox = novo;

but aux is NULL.

As for arrival order, do you mean arrival order in terms of calls to the function, or some other arrive order that is part of your data?



来源:https://stackoverflow.com/questions/11161070/how-to-sort-list-items-by-their-priority-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!