fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. typedef struct nod
  5. {
  6. int data;
  7. struct nod *link;
  8. }node;
  9.  
  10. node *head=NULL;
  11. void push(int x)
  12. {
  13. node *temp=malloc(sizeof(node));
  14. if(temp==NULL) { printf("Stackoverflow!!\n"); exit(1); }
  15. temp->data=x;
  16. temp->link=head;
  17. head=temp;
  18.  
  19. }
  20. int pop()
  21. { if (head==NULL) { printf("Stackunderflow!!\n"); exit(1); }
  22. node *temp;
  23. temp=head;
  24. head=head->link;
  25. int popel=temp->data;
  26. free(temp);
  27. temp=NULL;
  28. return popel;
  29. }
  30. int main(int argc,char*argv[])
  31. {
  32. FILE *mainfile;
  33. mainfile = fopen(argv[1],"r");
  34. if(mainfile == NULL)
  35. {
  36. printf("Failed opening file containing data \n");
  37. return 0;
  38. }
  39. char pass[10];
  40. while (fscanf(mainfile,"%s",pass)!=EOF)
  41. {
  42. push(atoi(pass));
  43. }
  44. int ans=1;
  45. while(ans)
  46. {
  47. printf("Enter 1 to pop 0 to stop the operation\n");
  48. scanf("%d",&ans);
  49. if(ans)
  50. printf("The data popped is : %d \n",pop());
  51.  
  52. }
  53. return 0;
  54. }
Success #stdin #stdout 0s 5508KB
stdin
Standard input is empty
stdout
Failed opening file containing  data