Résultat du TP en C sur Forth
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pile.c 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "pile.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. void init( struct Pile* p ) {
  5. p->height = 0;
  6. p->l = NULL;
  7. }
  8. void push( struct Pile* p, struct NumContainer i ) {
  9. struct List* aux = p->l;
  10. p->l = (struct List*)malloc(sizeof(struct List));
  11. if (p->l == NULL) {
  12. fprintf(stderr, "Error: Could not allocate memory for the next cell on the stack\n");
  13. return;
  14. }
  15. p->l->next = aux;
  16. p->l->i = i;
  17. ++(p->height);
  18. }
  19. struct NumContainer top( struct Pile* p ) {
  20. if ( p->height == 0 ) {
  21. fprintf(stderr, "Error: The stack has no element, cannot find top\n");
  22. exit(1);
  23. }
  24. return p->l->i;
  25. }
  26. void print( struct Pile* p ) {
  27. struct List* aux = p->l;
  28. while (aux != NULL) {
  29. printNum(aux->i);
  30. printf(" ");
  31. aux = aux->next;
  32. }
  33. /* printf("NULL"); */
  34. }
  35. void pop( struct Pile* p ) {
  36. struct List* aux;
  37. if ( p->height != 0 ) {
  38. --(p->height);
  39. aux = p->l;
  40. p->l = p->l->next;
  41. free(aux);
  42. }
  43. }
  44. void end( struct Pile* p ) {
  45. while (p->height != 0) {
  46. pop(p);
  47. }
  48. }
  49. void getlastnums( struct NumContainer* i, struct NumContainer* j, struct Pile* pile ) {
  50. *j = top(pile);
  51. pop(pile);
  52. *i = top(pile);
  53. pop(pile);
  54. }