Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct Node
  5. {
  6.     int var;
  7.     Node *Next;
  8. };
  9. typedef Node *PNode;
  10.  
  11. bool Check_Empty(PNode Head, PNode Tail)
  12. {
  13.     if(Head == NULL && Tail == NULL) return true;
  14.     else return false;
  15. }
  16.  
  17. void Output(PNode Head)
  18. {
  19.     PNode p = Head;
  20.     if(Head != NULL)
  21.     {
  22.         cout << "Текущий список:" << endl;
  23.         while(p != NULL)
  24.         {
  25.             cout << p -> var << " ";
  26.             p = p -> Next;
  27.         }
  28.     }
  29.     else cout << "Список пуст.";
  30.     cout << endl;
  31. }
  32.  
  33. void Push(PNode &Head, PNode &Tail, int x)
  34. {
  35.     PNode p;
  36.     p = new Node;
  37.     p -> var = x;
  38.     p -> Next = NULL;
  39.     if(Head != NULL)
  40.     {
  41.         Tail -> Next = p;
  42.         Tail = p;
  43.     }
  44.     else
  45.     {
  46.         Head = p;
  47.         Tail = p;
  48.     }
  49.     cout << "ok" << endl;
  50. }
  51.  
  52. void Pop(PNode &Head, bool Del)
  53. {
  54.     int t = Head -> var;
  55.     if(Del)
  56.     {
  57.         PNode Old = Head;
  58.         Head = Head -> Next;
  59.         delete Old;
  60.     }
  61.     cout << t << endl;
  62. }
  63.  
  64. int Size(PNode Head)
  65. {
  66.     PNode p = Head;
  67.     int n = 0;
  68.     while(p != NULL)
  69.     {
  70.         n++;
  71.         p = p -> Next;
  72.     }
  73.     return n;
  74. }
  75.  
  76. void Clear(PNode &Head)
  77. {
  78.     while(Head != NULL)
  79.     {
  80.         PNode Old = Head;
  81.         Head = Head -> Next;
  82.         delete Old;
  83.     }
  84.     cout << "ok" << endl;
  85. }
  86.  
  87. void main()
  88. {
  89.     PNode Head = NULL;
  90.     PNode Tail = NULL;
  91.     string operation;
  92.     while(cin >> operation)
  93.     {
  94.         if(operation == "check") Output(Head);
  95.         if(operation == "push")
  96.         {
  97.             int x;
  98.             cin >> x;
  99.             Push(Head, Tail, x);
  100.         }
  101.         if(operation == "pop")
  102.         {
  103.             if(Check_Empty(Head, Tail))
  104.                 cout << "error" << endl;
  105.             else Pop(Head, true);
  106.         }
  107.         if(operation == "front")
  108.         {
  109.             if(Check_Empty(Head, Tail))
  110.                 cout << "error" << endl;
  111.             else Pop(Head, false);
  112.         }
  113.         if(operation == "size") cout << Size(Head) << endl;
  114.         if(operation == "clear") Clear(Head);
  115.         if(operation == "exit")
  116.         {
  117.             cout << "bye" << endl;
  118.             return;
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement