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.

  • one
    "Turning over all the lines" - what's this? The reverse of the lines themselves ("abcd" -> "dcba") or just a reversal of the stack itself (who was the first - becomes the last)? - Harry
  • one
    Then I don’t understand how to expand N rows in constant time - even if the line could be expanded in O (1) ... Keeping the string next to it in expanded form is also not an option - it’s just to transfer the work to the insert. - Harry
  • 2
    Nothing is said about the time of insertion of elements. Those. you can immediately have two stacks: straight and inverted lines. The insert will require O (k) - where k is the length of the line, but it fits into the task. Instead of flipping rows on demand, do swap vectors, which is O (1). - Chorkov
  • one
    Let's just say - if you don’t bind to O (1), just generally - I just need to get strings from the stack, then such, then I’d just keep a variable on the stack that I’ll return, and the top would 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 then top would 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
  • 2
    @ Fat-Zer is possible. 2 the stack is just not a string but a flag one. When extracting apply the flag in the opposite direction. - pavel

0