There is a task to implement Stack, which has, in addition to the usual stack operations, the operation of calculating the maximum element working behind O (1) and the Stack must additionally have the operation of turning over all the rows in the stack working behind O (1). In principle, I realized the finding of the maximum element, it seems to be like, I ask for help with re-rotations of the strings. I just have no idea how to do this for O (1). Thank you very much in advance. Code:
//Stack.h
#ifndef STACK_H #define STACK_H #include <iostream> #include <vector> #include <cstdlib> #include <algorithm> using namespace std; template <typename T> class Stack { private: vector<T> maxelement; vector<T> elems; public: T push(T const&); T pushfirst(T const&); T& pop() const; T top(); T maxElement(); bool empty(){ return elems.empty(); } }; template <typename T> T Stack<T>::pushfirst(T const& elem){ maxelement.push_back(elem); } template <typename T> T Stack<T>::push (T const& elem){ elems.push_back(elem); if (maxelement.back() < elems.back()){ maxelement.push_back(elems.back()); } } template <typename T> T& Stack<T>::pop ()const{ if (elems.empty()){ throw out_of_range( "Stack<>::pop(): empty stack" ); } return elems.pop_back(); } template <typename T> T Stack<T>::top () { if (elems.empty()){ throw out_of_range( "Stack<>::top(): empty stack" ); } return elems.back(); } template <typename T> T Stack<T>::maxElement() { return maxelement.back(); } ////////////// template <> class Stack<string> { private: vector<string> elements; vector<string> addelems; public: void pushstring(string const&); string& popstring(); string topstring(); string topreversedstring(); void reverseString(); }; void Stack<string>::pushstring(string const& elem){ elements.push_back(elem); } string& Stack<string>::popstring(){ if (elements.empty()){ throw out_of_range( "Stack<>::pop(): empty stack" ); } elements.pop_back(); addelems.pop_back(); } string Stack<string>::topstring () { if (addelems.empty()){ throw out_of_range( "Stack<>::top(): empty stack" ); } return addelems.back(); } string Stack<string>::topreversedstring () { if (elements.empty()){ throw out_of_range( "Stack<>::top(): empty stack" ); } return elements.back(); } void Stack<string>::reverseString(){ for (int i = 0; i < elements.size(); i++){ addelems.push_back(elements[i]); reverse(elements[i].begin(), elements[i].end()); } } #endif // STACK_H //main.cpp
#include <iostream> #include "Stack.h" int main() { try{ Stack<int> intStack; Stack<string> stringStack; intStack.pushfirst(7); intStack.push(7); cout << intStack.top() <<endl; intStack.push(2); cout << intStack.top() <<endl; intStack.push(4); cout << intStack.top() <<endl; intStack.push(99); cout << intStack.top() <<endl; cout << "Max Element is : " << intStack.maxElement() << std::endl; stringStack.pushstring("hello"); stringStack.pushstring("world"); stringStack.reverseString(); cout << "Old string: " << stringStack.topstring() << ", " << "Reversed string: " << stringStack.topreversedstring() << std::endl; stringStack.popstring(); cout << "Old string: " << stringStack.topstring() << ", " << "Reversed string: " << stringStack.topreversedstring() << std::endl; stringStack.popstring(); cout << "Old string: " << stringStack.topstring() << ", " << "Reversed string: " << stringStack.topreversedstring() << std::endl; return 0; } catch ( exception const& ex ){ cerr << "Exception: " << ex.what() << endl; return -1; } } And another question is whether it is a crutch in the program, if I wrote a special method for adding the first element to 2 vectors, in order to later dance from it. Maybe it is worth something different, smarter to do? Thanks again.
topwould return a string to me either straight or inverse depending on the field. Then the whole "coup operation" would be O (1) - just a field entry. But thentopwould turn into operation O (string length). If this time of turning the line is not taken into account, then here is O (1) :) But this is somehow unfair :) The second option - as I said, to store 2 lines - then the work is transferred to the insert, but it is more expensive, because - Harry