Since a lot of people have been posting about Linked Lists and Stacks and Queues recently, I thought of paying visit to one of my old Stack programs (I have three more for Queues, Circular Queues and Trees). There seems to be some error with the DisplayStack () method. Everything looks fine to me but when I run the code, it crashes. If I comment out the call to DisplayStack, everything works well. I haven't looked at the code in quite sometime and nothing makes sense right now as it's pretty late here (5 am) and I need to hit the bed for a few hours. Hopefully, someone will be able to figure it out and I wouldn't need to think about this when I wake up.
There's a tonne more of other methods and operator overloads but I'm not adding all of them as thry were working pretty much fine the last time I ran it. Thanks, 'night.Code:template <typename T> class Stack { private: struct Object { T Data; Object* Next; Object (void) : Data (T()) , Next (nullptr) { } }; Object* StackTop; public: Stack (void); ~Stack (void); void Push(T const& data); void Pop (void) ; void DisplayStack (void) const; }; template <typename T> Stack<T>::Stack (void) : StackTop (nullptr) { } template <typename T> void Stack<T>::Push (T const& data) { Object* ptr = new Object; ptr->Data = data; if (StackTop == nullptr) StackTop = ptr; else { ptr->Next = StackTop; StackTop = ptr; } } template <typename T> void Stack<T>::Pop (void) { Object* temp = StackTop; StackTop = StackTop->Next; delete temp; } template <typename T> void Stack<T>::DisplayStack (void) const { Object* temp = StackTop; std::cout << std::endl << temp->Data << "<- Stack Top"; while (temp != nullptr) { temp = temp->Next; std::cout << std::endl << temp->Data; } } template <typename T> Stack<T>::~Stack (void) { Object* temp = StackTop; while (temp != nullptr) { StackTop = StackTop->Next; delete temp; temp = StackTop; } } int main (void) { Stack <std::string> S; S.Push ("ABCD"); S.Push ("EFGH"); S.Push ("IJKL"); S.Push ("MNOP"); S.DisplayStack (); return 0; }