ユーザーが入力した優先度でリスト項目を並べ替えたいのですが、それはうまくいきます。ただし、同じ優先度のアイテムが複数ある場合、本来のように到着順にソートされません。
あなたが理解できるように私がこれを十分に明確にしていない場合は申し訳ありません. 変数名はポルトガル語なので、わからないことがあれば聞いてください。
コードは次のとおりです。
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;
}