Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <fstream>
  2. struct Node{
  3.     int value;
  4.     Node* next;
  5. };
  6.  
  7. void PushBack(Node* &prev, int digit) {
  8.     Node *cur = new Node;
  9.     cur->value = digit;
  10.     cur->next = prev;
  11.     prev = cur;
  12. }
  13.  
  14. int PopBack(Node* &cur) {
  15.     int digit = cur->value;
  16.     Node *temp = cur;
  17.     cur = cur->next;
  18.     delete temp;
  19.     return digit;
  20. }
  21.  
  22. int main() {
  23.     std::ifstream in;
  24.     std::ofstream out;
  25.     in.open("input.txt");
  26.     out.open("output.txt");
  27.     int n;
  28.     in >> n;
  29.     Node* stack = new Node;
  30.     char operation;
  31.     int digit;
  32.     for (int i = 0; i < n; i++) {
  33.         in >> operation;
  34.         if (operation=='+') {
  35.             in >> digit;
  36.             PushBack(stack, digit);
  37.         }
  38.         else {
  39.             if (operation=='-') {
  40.                 out << PopBack(stack) << std::endl;
  41.             }
  42.         }
  43.     }
  44.     out.close();
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement