Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. /*
  2. * Resolução
  3. * Pedro Henrique de Lima Ribeiro
  4. * ADS IFPI 2018.1
  5. */
  6.  
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. class No
  12. {
  13.   public:
  14.     char nome;
  15.     No *prox;
  16.  
  17.     No(char n)
  18.     {
  19.         nome = n;
  20.         prox = NULL;
  21.     }
  22. };
  23.  
  24. class Queue
  25. {
  26.   public:
  27.     No *inicio;
  28.     No *fim;
  29.  
  30.     Queue()
  31.     {
  32.         inicio = NULL;
  33.         fim = NULL;
  34.     }
  35.  
  36.     void push(char n)
  37.     {
  38.         No *novo = new No(n);
  39.         if (inicio == NULL)
  40.         {
  41.             inicio = novo;
  42.             fim = novo;
  43.         }
  44.         else
  45.         {
  46.             fim->prox = novo;
  47.             fim = novo;
  48.         }
  49.     }
  50.  
  51.     char pop()
  52.     {
  53.         char nome;
  54.         if (inicio != NULL)
  55.         {
  56.             nome = inicio->nome;
  57.             inicio = inicio->prox;
  58.         }
  59.         return nome;
  60.     }
  61. };
  62.  
  63. class StackWithQueue
  64. {
  65.   public:
  66.     Queue *q;
  67.  
  68.     StackWithQueue()
  69.     {
  70.         q = new Queue();
  71.     }
  72.  
  73.     void push(char letra)
  74.     {
  75.         Queue *auxiliar = new Queue();
  76.         // tranfere todos os elementos para outra fila
  77.         while (q->inicio != nullptr)
  78.         {
  79.             auxiliar->push(q->pop());
  80.         }
  81.         // Insere novo elemento
  82.         q->push(letra);
  83.         // Retorna todos os elementos para a lista principal
  84.         while (auxiliar->inicio != nullptr)
  85.         {
  86.             q->push(auxiliar->pop());
  87.         }
  88.     }
  89.  
  90.     char pop()
  91.     {
  92.         return q->pop();
  93.     }
  94. };
  95.  
  96. // Main
  97. int main()
  98. {
  99.     StackWithQueue *stack = new StackWithQueue();
  100.     cout << "inserindo elementos na pilha" << endl;
  101.     cout << "Primeiro: A\n";
  102.     stack->push('A');
  103.     cout << "Segundo: B\n";
  104.     stack->push('B');
  105.     cout << "Terceiro: C\n";
  106.     stack->push('C');
  107.  
  108.     cout << "\nRemovendo elementos da pilha" << endl;
  109.     cout << "Primeiro: " << stack->pop() << endl;
  110.     cout << "segundo : " << stack->pop() << endl;
  111.     cout << "terceiro: " << stack->pop() << endl;
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement