Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort list items by their priority in C?
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>Here is the code:</p> <pre><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", &amp;novo-&gt;n_pratos); printf("Table number: "); scanf("%d", &amp;novo-&gt;mesa); printf("Priority of request? "); scanf("%d", &amp;novo-&gt;prioridade); printf("Introduza o ID do pedido: "); scanf("%s", &amp;novo-&gt;id); for(i=0;i&lt;novo-&gt;n_pratos;i++){ printf("ID of menu %d: ", i+1); //something like "M1, M4..." doesn't matter scanf("%s", &amp;novo-&gt;prato[i]); fflush(stdin); } novo-&gt;prox=NULL; if(lista == NULL || novo-&gt;prioridade &gt; lista-&gt;prioridade) { novo-&gt;prox = lista; lista = novo; } else { aux = lista; while(aux != NULL &amp;&amp; novo-&gt;prioridade &lt; aux-&gt;prioridade) //this is where it should be sort requests by their priority and order of arrival aux = aux-&gt;prox; novo-&gt;prox = aux-&gt;prox; aux-&gt;prox = novo; } return lista; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload