fork download
  1. typedef struct {
  2. int data[100001];
  3. int front;
  4. int rear;
  5. } MyStack;
  6.  
  7. /** Initialize your data structure here. */
  8.  
  9. MyStack* myStackCreate() {
  10. MyStack * node= (MyStack *) malloc(sizeof(MyStack));
  11. node->rear=-1;
  12. node->front=0;
  13. return node;
  14. }
  15.  
  16. /** Push element x onto stack. */
  17. void myStackPush(MyStack* obj, int x) {
  18.  
  19. obj->rear+=1;
  20. obj->data[obj->rear]=x;
  21. //printf("push New front = %d , rear = %d\n",obj->front,obj->rear);
  22. }
  23.  
  24. /** Removes the element on top of the stack and returns that element. */
  25. int myStackPop(MyStack* obj) {
  26. int val;
  27. if(obj->rear>0)
  28. {
  29. MyStack *s2=myStackCreate();
  30. while(obj->front<obj->rear)
  31. {
  32. myStackPush(s2,obj->data[obj->front]);
  33. obj->front+=1;
  34. }
  35. val=obj->data[obj->rear];
  36. obj=s2;
  37. //printf("pop New front = %d , rear = %d\n",obj->front,obj->rear);
  38. }
  39. else
  40. {
  41. val=obj->data[obj->rear];
  42. obj->rear=-1;
  43. }
  44. return val;
  45. }
  46.  
  47. /** Get the top element. */
  48. int myStackTop(MyStack* obj) {
  49. // printf(" New front = %d , rear = %d\n",obj->front,obj->rear);
  50. int val= obj->data[obj->rear];
  51. return val;
  52. }
  53.  
  54. /** Returns whether the stack is empty. */
  55. bool myStackEmpty(MyStack* obj) {
  56. if (obj->front>obj->rear)
  57. return true;
  58. else
  59. return false;
  60. }
  61.  
  62. void myStackFree(MyStack* obj) {
  63. obj=NULL;
  64. }
  65.  
  66. /**
  67.  * Your MyStack struct will be instantiated and called as such:
  68.  * MyStack* obj = myStackCreate();
  69.  * myStackPush(obj, x);
  70.  
  71.  * int param_2 = myStackPop(obj);
  72.  
  73.  * int param_3 = myStackTop(obj);
  74.  
  75.  * bool param_4 = myStackEmpty(obj);
  76.  
  77.  * myStackFree(obj);
  78. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘myStackCreate’:
prog.c:10:33: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
     MyStack * node= (MyStack *) malloc(sizeof(MyStack));
                                 ^~~~~~
prog.c:10:33: warning: incompatible implicit declaration of built-in function ‘malloc’
prog.c:10:33: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
prog.c:1:1:
+#include <stdlib.h>
 typedef struct {
prog.c:10:33:
     MyStack * node= (MyStack *) malloc(sizeof(MyStack));
                                 ^~~~~~
prog.c: At top level:
prog.c:55:1: error: unknown type name ‘bool’; did you mean ‘_Bool’?
 bool myStackEmpty(MyStack* obj) {
 ^~~~
 _Bool
prog.c: In function ‘myStackEmpty’:
prog.c:57:14: error: ‘true’ undeclared (first use in this function)
       return true;
              ^~~~
prog.c:57:14: note: each undeclared identifier is reported only once for each function it appears in
prog.c:59:16: error: ‘false’ undeclared (first use in this function)
         return false;
                ^~~~~
prog.c: In function ‘myStackFree’:
prog.c:63:9: error: ‘NULL’ undeclared (first use in this function)
     obj=NULL;
         ^~~~
prog.c:63:9: note: ‘NULL’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
prog.c:1:1:
+#include <stddef.h>
 typedef struct {
prog.c:63:9:
     obj=NULL;
         ^~~~
prog.c: In function ‘myStackEmpty’:
prog.c:60:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty