Advertisement
Guest User

Stack_Queue

a guest
May 28th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. #define N 100
  5. class Stack
  6. {
  7. protected:
  8.     int *Value;
  9.     int size, top;
  10. public: Stack();
  11.         ~Stack();
  12.         virtual void pop()
  13.         {
  14.             if (Value[top - 1]){
  15.                 Value[top - 1] = NULL;
  16.                 top--;
  17.             }
  18.             else cout << endl << "Стек пуст!";
  19.         }
  20.     void push(int x)
  21.     {
  22.         if (top < size)
  23.         {
  24.             Value[top] = x;
  25.             top++;
  26.         }
  27.         else cout << "Ошибка! Стек заполнен! "<<endl;
  28.     }
  29.  
  30.     bool empty()
  31.     {
  32.         if (top == 0)
  33.             return true;
  34.         else
  35.             return false;
  36.     }
  37.  
  38.  
  39.     bool full()
  40.     {
  41.         if (top == size - 1)
  42.             return true;
  43.         else
  44.             return false;
  45.    
  46.     }
  47.     void show()
  48.     {
  49.         int k = 0;
  50.         while (Value[k]){
  51.             cout  << Value[k]<< endl;
  52.             k++;
  53.         }
  54.     }
  55. };
  56.  
  57. Stack::Stack()
  58. {
  59.     Value = new int[N];
  60.     size = N;
  61.     top = 0;
  62.     for (int i = 0; i < N; i++)
  63.     {
  64.         Value[i] = NULL;
  65.     }
  66. };
  67.  
  68. Stack::~Stack(){
  69.    
  70. }
  71. class Queue :public Stack
  72. {
  73. public:
  74.  Queue();
  75.  ~Queue();
  76.     void pop()
  77.     {
  78.         Value[0] = NULL;
  79.         int temp = Value[0];
  80.         for (int i = 1; i < size; i++)
  81.         {
  82.             Value[i - 1] = Value[i];
  83.         }
  84.         Value[size - 1] = temp;
  85.     }
  86. };
  87.  
  88. Queue::Queue()
  89. {
  90.     Value = new int[N];
  91.     size = N;
  92.     top = 0;
  93.     for (int i = 0; i < N; i++)
  94.     {
  95.         Value[i] = NULL;
  96.     }
  97. };
  98. Queue::~Queue(){
  99.  
  100. }
  101.  
  102. int _tmain(int argc, _TCHAR* argv[])
  103. {
  104.     setlocale(LC_ALL, "Russian");
  105.     Stack stack;
  106.     int M;
  107.     cout << "Введите количество элементов стека: " << endl;
  108.     cin >> M;
  109.    
  110.     cout << "Введите элементы стека: " << endl;
  111.     for (int i = 0; i < M; i++)
  112.     {
  113.         int x;
  114.         cin >> x;
  115.         stack.push(x);
  116.     }
  117.     cout << "Полученный стек: " << endl;
  118.     stack.show();
  119.    
  120.     cout << "Стек с удаленным элементом: " << endl;
  121.     stack.pop();
  122.     stack.show();
  123.    
  124.     Queue queue;
  125.     int L;
  126.     cout << "Введите количество элементов очереди: " << endl;
  127.     cin >> L;
  128.     cout << "Введите элементы очереди: " << endl;
  129.     for (int i = 0; i < L; i++)
  130.     {
  131.         int x;
  132.         cin >> x;
  133.         queue.push(x);
  134.     }
  135.     cout << "Полученная очередь: " << endl;
  136.     queue.show();
  137.     queue.pop();
  138.     cout << "Очередь с удаленным элементом: " << endl;
  139.     queue.show();
  140.     system("pause");
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement