Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include <string>
- #include "stack.cpp"
- using namespace std;
- void reverse (stack <string> &st)
- {
- if (st.empty()) return;
- else
- {
- string word = st.top();
- st.pop();
- reverse(st);
- st.push_front(word);
- }
- }
- void print (stack <string> st)
- {
- while(!st.empty())
- {
- cout << st.top() << " ";
- st.pop();
- }
- cout << '\n';
- }
- int main()
- {
- stack <string> st;
- cout << "Введите ваши слова: ";
- string word;
- while (cin >> word) st.push_back(word);
- //останавливаем цикл зажав Ctrl + Z или Ctrl + D
- reverse(st);
- print(st);
- return 0;
- }
- //stack.cpp
- #include <cassert>
- template <class Type>
- class stack
- {
- struct element
- {
- Type inf;
- element *next;
- element (Type i, element *n) : inf(i), next(n) {}
- };
- element *head;
- public:
- stack() : head(nullptr) {}
- bool empty() { return head == nullptr; }
- void push_back (Type x) { head = new element (x, head); }
- Type top() { assert(!empty()); return head -> inf; }
- void pop()
- {
- if (empty()) return;
- else
- {
- element *st = head;
- head = st -> next;
- delete st;
- }
- }
- void push_front (Type x)
- {
- if(empty()) push_back(x);
- else
- {
- Type y = top();
- pop();
- push_front(x);
- push_back(y);
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement