Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef DEBUG
  5. #define DEBUG(...) printf(__VA_ARGS__)
  6. #endif
  7.  
  8. typedef struct Node{
  9. struct Node *next;
  10. int val;
  11. } node;
  12.  
  13. int push(node **top, int val) {
  14. if(!*top){
  15. *top=(node *)malloc(sizeof(node));
  16. if(*top==NULL) return -1;
  17. (*top)->val=val;
  18. (*top)->next=NULL;
  19. return 0;
  20. }
  21. node *novi=(node *)malloc(sizeof(node));
  22. if(novi==NULL){ return -1;}
  23. novi->val=val;
  24. novi->next=*top;
  25. *top=novi;
  26.  
  27.  
  28. return 0;
  29. }
  30.  
  31. int pop(node **top) {
  32. if(!*top) return -1;
  33. node *temp=(node *)malloc(sizeof(node));
  34. temp=*top;
  35. *top=(*top)->next;
  36. free(temp);
  37. return 0;
  38. }
  39.  
  40. int print(node *top) {
  41. if(top==NULL) return -1;
  42. printf("Stoga: ");
  43. for(; top!=NULL; top=top->next){
  44. printf("%d ", top->val);
  45. }
  46. printf("\n");
  47. return 0;
  48. }
  49.  
  50. int main() {
  51. node *top = NULL;
  52. char menu_choice;
  53. int val, retval;
  54.  
  55. do {
  56. DEBUG("\n(d) dodaj - push\n(b) brisi - pop\n(i) ispis - print\n(e) Exit\n");
  57.  
  58. scanf(" %c", &menu_choice);
  59. switch (menu_choice) {
  60. case 'd':
  61. scanf("%d", &val);
  62. retval = push(&top, val);
  63. if (retval==-1) printf("Alokacija nije uspjela.\n");
  64. break;
  65. case 'b':
  66. retval = pop(&top);
  67. if (retval==-1) printf("Stog je prazan.\n");
  68. break;
  69. case 'i':
  70. retval = print(top);
  71. if (retval==-1) printf("Stog je prazan.\n");
  72. break;
  73. }
  74. } while(menu_choice!='e');
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement