fork(4) download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. typedef struct celula
  6. {
  7. long long int dado;
  8. struct celula *prox;
  9. } celula;
  10. celula *inicializa()
  11. {
  12. celula *novo;
  13. novo = malloc(sizeof(celula));
  14. novo->prox = NULL;
  15. return novo;
  16. }
  17. void empilha(celula *p, long long int x)
  18. {
  19. celula *novo;
  20. novo = malloc(sizeof(celula));
  21. novo->dado = x;
  22. novo->prox = p->prox;
  23. p->prox = novo;
  24. }
  25. long long int desempilha(celula *p, int *y)
  26. {
  27. celula *lixo = p->prox;
  28. p->prox = lixo->prox;
  29. *y = lixo->dado;
  30. free(lixo);
  31. return 1;
  32. }
  33.  
  34. long long int imprimeTopo(celula *p, int *y){
  35. celula *topo = p->prox;
  36. *y = topo->dado;
  37. return 1;
  38. }
  39.  
  40. int main()
  41. {
  42. int i, j = 0, n = 0, d;
  43. long long int dado, aux;
  44. char operacao[20];
  45. celula *presentes = inicializa();
  46. celula *menor = inicializa();
  47.  
  48.  
  49. scanf("%d", &n);
  50.  
  51. for (i = 0; i < n; i++)
  52. {
  53. scanf("%s",operacao);
  54.  
  55. if (strcmp(operacao, "PUSH") == 0)
  56. {
  57.  
  58. scanf("%lld", &dado);
  59. if(presentes->prox == NULL || menor->prox == NULL)
  60. {
  61. aux = dado;
  62. empilha(menor, aux);
  63. }
  64. else
  65. {
  66. if(aux <= dado)
  67. {
  68. empilha(menor, aux);
  69. }
  70. else
  71. {
  72. empilha(menor, dado);
  73. aux = dado;
  74. }
  75. }
  76. empilha(presentes, dado);
  77. }
  78.  
  79.  
  80.  
  81. else if (strcmp(operacao, "POP") == 0)
  82. {
  83. if (presentes->prox == NULL || menor->prox == NULL){
  84. printf("EMPTY\n");
  85. }
  86. else
  87. {
  88. desempilha(presentes, &d);
  89. desempilha(menor, &d);
  90. }
  91.  
  92. }
  93. else if (strcmp(operacao, "MIN") == 0)
  94. {
  95. if(presentes->prox == NULL || menor->prox == NULL)
  96. {
  97. printf("EMPTY\n");
  98. }
  99. else
  100. {
  101. imprimeTopo(menor, &d);
  102. printf("%d\n", d);
  103. }
  104. }
  105. }
  106.  
  107. return 0;
  108. }
  109.  
Success #stdin #stdout 0s 4308KB
stdin
 11
 PUSH 5
 PUSH 7
 PUSH 3
 PUSH 8
 PUSH 10
 MIN
 POP
 POP
 MIN
 POP
 MIN
stdout
3
3
5