Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <exception>
- #define myStack
- #define myAlg
- using namespace std;
- #ifdef myStack
- template<class T>
- class Stack {
- struct Node {
- Node(T el, Node * node)
- {
- data = el;
- next = node;
- }
- Node* next;
- T data;
- };
- Node* _top;
- int _size;
- public:
- Stack()
- {
- _top = NULL;
- _size = 0;
- }
- ~Stack()
- {
- while (_top != NULL && _top->next != NULL)
- {
- Node* temp = _top;
- _top = _top->next;
- delete temp;
- }
- }
- void push(T el);
- void pop();
- T top();
- int size();
- T peek();
- bool empty();
- };
- #endif
- #ifdef myAlg
- template<class T>
- bool Stack<T>::empty()
- {
- return !_size;
- }
- template<class T>
- T Stack<T>::peek()
- {
- if (!_size)
- return NULL;
- return _top->data;
- }
- template<class T>
- int Stack<T>::size()
- {
- return _size;
- }
- template<class T>
- T Stack<T>::top()
- {
- try
- {
- if (_top == NULL)
- throw out_of_range("Error: Stack is empty!");
- return _top->data;
- }
- catch (exception &e)
- {
- cout << e.what() << endl;
- }
- }
- template<class T>
- void Stack<T>::pop() {
- Node* temp = _top;
- _top = _top->next;
- delete temp;
- _size--;
- }
- template<class T>
- void Stack<T>::push(T el)
- {
- Node * temp = new Node(el, _top);
- _top = temp;
- _size++;
- }
- #endif // myStack
- int main()
- {
- Stack<string> words;
- fstream file("words.txt", ios::in);
- while (file.peek() != EOF)
- {
- string s;
- file >> s;
- words.push(s);
- }
- //cout << words.size();
- while (!words.empty()){
- string s;
- s = words.top();
- words.pop();
- cout << s << endl;
- if (s.length() == 1)
- cout << s << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement