Skip to content

Instantly share code, notes, and snippets.

@sxslex
Created November 12, 2017 02:13
Show Gist options
  • Save sxslex/c31da80c4435cc183353bfa9d4a4d4f9 to your computer and use it in GitHub Desktop.
Save sxslex/c31da80c4435cc183353bfa9d4a4d4f9 to your computer and use it in GitHub Desktop.
urionlinejudg problem 1566
// https://www.urionlinejudge.com.br/judge/en/problems/view/1566
include <stdio.h>
#include <stdlib.h>
struct Node{
int num;
struct Node *prox;
};
typedef struct Node node;
int main()
{
int total, i, pessoas;
int y, num;
node *tmp, *proxNode, *atual;
scanf("%d", &total);
for ( i = 0; i < total; i++ ) {
scanf("%d", &pessoas);
node *itens = (node *) malloc(sizeof(node));
itens->prox = NULL;
for (y = 0; y < pessoas; y++ ) {
scanf("%d", &num);
// insert
tmp = itens;
while( tmp->prox != NULL){
if( tmp->prox->num > num ){
break;
}
tmp = tmp->prox;
}
node *novo=(node *) malloc(sizeof(node));
novo->num = num;
node *oldHead = tmp->prox;
tmp->prox = novo;
novo->prox = oldHead;
}
// lista
tmp = itens->prox;
while( tmp != NULL){
printf("%d ", tmp->num);
tmp = tmp->prox;
}
printf("\n");
// free
if(itens->prox != NULL){
atual = itens->prox;
while(atual != NULL){
proxNode = atual->prox;
free(atual);
atual = proxNode;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment