Pafnytiu

стек

May 22nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. class Stack
  5. {
  6. struct Element
  7. {
  8. int inf;
  9. Element *next;
  10. Element(int x, Element *p) :inf(x), next(p){}
  11. };
  12. Element *head;
  13. public:
  14. Stack() :head(0){}
  15. bool Empty()
  16. {
  17. return head == 0;
  18. }
  19. int Pop()
  20. {
  21. if (Empty())
  22. {
  23. return 0;
  24. }
  25. Element *r = head;
  26. int i = r->inf;
  27. head = r->next;
  28. delete r;
  29. return i;
  30. }
  31. void Push(int data)
  32. {
  33. head = new Element(data, head);
  34. }
  35. int Top()
  36. {
  37. if (Empty())
  38. {
  39. return 0;
  40. }
  41. else
  42. {
  43. return head->inf;
  44. }
  45. }
  46. };
  47. int main()
  48. {
  49. Stack q, w;
  50. int i;
  51. ifstream in("in.txt");
  52. ofstream out("out.txt");
  53. for (int i = 0; in >> i;i++)
  54. {
  55. if (i == 9)
  56. q.Push(i);
  57. else i++;
  58. }
  59. in.close();
  60. while (!q.Empty())
  61. {
  62. out << q.Pop() << " ";
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment