Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <clocale>
- //#include "UnitStack.h"
- template <class Type> // объявление класса
- class CStack
- {
- public:
- void push(Type value)
- {
- if (!head)
- head = tail;
- node *newHead = new node;
- newHead->info = value;
- newHead->prev = head;
- head = newHead;
- }
- void pop()
- {
- node *tmp = head;
- head = head->prev;
- delete tmp;
- }
- Type top()
- {
- if (head)
- return head->info;
- else
- return 0;
- }
- bool isNull()
- {
- return (head == tail) || (!head);
- }
- private:
- struct node
- {
- Type info;
- node *prev;
- };
- node *head;
- node *tail;
- };
- int main()
- {
- setlocale(LC_ALL, "Russian");
- printf("Вводите символы с клавиатуры для добавления их в стек.\nВведите'.' для завершения ввода.\n");
- CStack<char> *stack = new CStack<char>;
- char tmp = 0;
- while ( (tmp = getch() ) != '.')
- {
- printf("%c", tmp);
- stack->push(tmp);
- }
- printf("\n");
- while (!stack->isNull())
- {
- printf("%c", stack->top());
- stack->pop();
- }
- printf("\nРабота программы завершена.");
- getch();
- delete stack;
- }
Advertisement
Add Comment
Please, Sign In to add comment