Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6. struct stack
  7. {
  8. int inf;
  9. stack *next;
  10. };
  11.  
  12. stack *init_stack()
  13. {
  14. return 0;
  15. }
  16.  
  17. void push(stack *&s, int item)
  18. {
  19. stack *r;
  20. r = new stack;
  21. r->inf = item;
  22. r->next = s;
  23. s = r;
  24. }
  25.  
  26. int pop(stack *&s)
  27. {
  28. stack *r = s;
  29. int st;
  30. st = r->inf;
  31. s = s->next;
  32. free(r);
  33. return st;
  34. }
  35.  
  36. int peek(stack* s)
  37. {
  38. return s->inf;
  39. }
  40.  
  41. int empty_stack(stack* &s)
  42. {
  43. return (s) ? 0 : 1;
  44. }
  45.  
  46. int main()
  47. {
  48. int i=0, k;
  49. char c;
  50. FILE *fin, *f1;
  51. fin = fopen("data.txt", "r");
  52. f1 = fopen("dataout.txt", "w");
  53. stack *head = init_stack();
  54. if (fin != NULL)
  55. {
  56. while (!feof(fin))
  57. {
  58. fscanf(fin, "%d", &i);
  59. push(head, i);
  60. }
  61. i = pop(head);
  62. while (!empty_stack(head))
  63. {
  64. k = pop(head);
  65. if (i != k)
  66. {
  67. fprintf(f1, "%d ", i);
  68. i = k;
  69. }
  70. }
  71. fprintf(f1, "%d ", i);
  72. }
  73. else printf("not today");
  74. system("pause");
  75. fcloseall();
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement