Epso

Untitled

May 27th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <clocale>
  6. //#include "UnitStack.h"
  7.  
  8. template <class Type>   // объявление класса
  9. class CStack
  10. {
  11.   public:
  12.  
  13.   void push(Type value)
  14.   {
  15.    if (!head)
  16.      head = tail;
  17.    node *newHead = new node;
  18.    newHead->info = value;
  19.    newHead->prev = head;
  20.    head = newHead;
  21.   }
  22.  
  23.   void pop()
  24.   {
  25.     node *tmp = head;
  26.     head = head->prev;
  27.     delete tmp;
  28.   }
  29.  
  30.   Type top()
  31.   {
  32.     if (head)  
  33.       return head->info;
  34.     else  
  35.       return 0;
  36.   }
  37.  
  38.   bool isNull()
  39.   {
  40.     return (head == tail) || (!head);
  41.   }
  42.  
  43.   private:
  44.     struct node
  45.     {
  46.       Type info;
  47.       node *prev;
  48.     };
  49.     node *head;
  50.     node *tail;
  51.  
  52. };
  53.  
  54. int main()
  55. {
  56.   setlocale(LC_ALL, "Russian");
  57.   printf("Вводите символы с клавиатуры для добавления их в стек.\nВведите'.' для завершения ввода.\n");
  58.  
  59.   CStack<char> *stack = new CStack<char>;
  60.   char tmp = 0;
  61.   while ( (tmp = getch() ) != '.')
  62.   {
  63.     printf("%c", tmp);
  64.     stack->push(tmp);
  65.   }
  66.  
  67.   printf("\n");
  68.  
  69.   while (!stack->isNull())
  70.   {
  71.     printf("%c", stack->top());
  72.     stack->pop();
  73.   }
  74.  
  75.   printf("\nРабота программы завершена.");
  76.   getch();
  77.   delete stack;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment