Advertisement
Sephinroth

page_74_own_stack

May 21st, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. /*6. Ñîçäàòü ñïèñîê èç öåëûõ ÷èñåë. Èñêëþ÷èòü èç ñïèñêà âñå ýëåìåíòû, ðàâíûå õ.*/
  2. #include <fstream>
  3. using namespace std;
  4. ifstream in ("input.txt");
  5. ofstream out ("output.txt");
  6. class Stack {
  7.     struct Element {
  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.         return head == 0; }
  17.     int pop() {
  18.         if (empty()) { return 0;}
  19.         Element *r = head;
  20.         int i = r->inf;
  21.         head = r->next;
  22.         delete r;
  23.         return i;}
  24.     void push(int data) { head = new Element(data, head);}
  25.     int top() { if (empty()) return 0;  else return head->inf;}
  26. };
  27. int main(){
  28.     Stack Stc;
  29.     int x, val;
  30.     in >> x;
  31.     while (in >> val)
  32.         Stc.push(val);
  33.     in.close();
  34.     while (!Stc.empty()){
  35.         if (Stc.top() == x) { Stc.pop(); continue;} else
  36.             out << Stc.top() << '\t';
  37.         Stc.pop();}
  38.     out.close();       
  39.     return 0;  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement