Click here to Skip to main content
15,897,273 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How is this possible? Pin
Richard MacCutchan17-May-20 6:28
mveRichard MacCutchan17-May-20 6:28 
GeneralRe: How is this possible? Pin
Richard MacCutchan17-May-20 22:12
mveRichard MacCutchan17-May-20 22:12 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 23:55
mveRichard MacCutchan19-May-20 23:55 
AnswerRe: How is this possible? Pin
Mircea Neacsu19-May-20 3:09
Mircea Neacsu19-May-20 3:09 
PraiseRe: How is this possible? Pin
Greg Utas19-May-20 3:37
professionalGreg Utas19-May-20 3:37 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 3:37
mveRichard MacCutchan19-May-20 3:37 
GeneralRe: How is this possible? Pin
Mircea Neacsu19-May-20 3:57
Mircea Neacsu19-May-20 3:57 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 4:16
mveRichard MacCutchan19-May-20 4:16 
Mircea, here is my implementation, using the original unchanged stack.h file. Like you I would not choose to do it this way, but I was trying to follow the rules as defined by the people who set the test. The key (which I originally struggled with) is the typedef at the beginning of stack.cpp. I have not implemented the destructors, as they are not important for the purpose of this test. And the two helper functions (pusher & popper) are just to save a bit of typing. I welcome your feedback.
C++
#include <iostream>
typedef char* T; // this implementation will use simple C-style strings
#include "stack.h"

template <class t>
_node<t>::_node(const T& item, _node<t>* next)
{
    _data = item;
    _next = next;
}

template <class t>
_node<t>::~_node()
{

}


template <class t>
stack<t>::stack()
{
    _head = nullptr;
    _size = 0;
}

template <class t>
stack<t>::~stack()
{
    // empty destructor
}

template <class t>
size_t stack<t>::size() const
{
    //return the size of the stack
    return _size;
}

template <class t>
T& stack<t>::top() const
{
    //return a reference to the top value. Throw an exception if the stack is empty.
    if (_size == 0)
        throw std::exception("Stack contains no items");

    return _head->_data;
}

template <class t>
void stack<t>::push(const T& item)
{
    //push a new value onto the stack
    _node<t>* newNode = new _node<t>(item, _head);
    _head = newNode;
    _size++;
}

template <class t>
void stack<t>::pop()
{
    //remove the top value from the stack. Do nothing if the stack is empty.
    if (_size > 0)
    {
        _node<t>* popNode = _head;
        _head = popNode->_next;
        delete popNode;
        _size--;
    }
}

template <class t>
void stack<t>::invert()
{
    //reverse the order of the entire stack, so that 1,2,3,4,5 becomes 5,4,3,2,1 and so on.
    if (_size > 1)
    {
        _node<t>* current = _head;
        _node<t>* next = nullptr;
        _node<t>* temp;

        while (current)
        {
            temp = current->_next;
            current->_next = next;
            next = current;
            current = temp;
        }
        _head = next;
    }
}

void pusher(stack<T>& theStack, T* items)
{
    while (*items != 0)
    {
        theStack.push(*items);
        std::cout << "Push " << *items << " onto the stack" << std::endl;
        items++;
    }
    std::cout << "    Top item = " << theStack.top() << ", stack size = " << theStack.size() << std::endl << std::endl;
}


void popper(stack<T>& theStack)
{
    while (theStack.size() > 0)
    {
        T& foo = theStack.top();
        std::cout << "    Top item = " << foo << ", stack size = " << theStack.size() << std::endl;
        theStack.pop();
    }
    std::cout << "    stack size = " << theStack.size() << std::endl << std::endl;
}


/// <summary>
/// Main function of the console application, which runs the application.
/// </summary>
///
/// <param name="argc">Count of command line parameters</param>
/// <param name="argv">Array of command line parameters</param>
///
/// <returns>Application defined value</returns>
///
int wmain(
    int		argc,
    wchar_t** argv
)
{
    stack<T> myStack;
//    T val = myStack.top(); // check exception

    T listA[] = { "Foo", "Bar", "Poo", 0 };
    pusher(myStack, listA);
    popper(myStack);

    T listB[] = { "The", "rain", "in", "Spain", "falls", "mainly", "in", "the", "plain", 0 };
    pusher(myStack, listB);

    std::cout << std::endl << "Invert the stack" << std::endl;
    myStack.invert();
    popper(myStack);

    std::cout << std::endl << "Press <enter> to continue ... ";
    std::cin.get();
    return 0;
}

GeneralRe: How is this possible? Pin
Mircea Neacsu19-May-20 5:08
Mircea Neacsu19-May-20 5:08 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 5:24
mveRichard MacCutchan19-May-20 5:24 
GeneralRe: How is this possible? Pin
Mircea Neacsu19-May-20 6:07
Mircea Neacsu19-May-20 6:07 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 21:36
mveRichard MacCutchan19-May-20 21:36 
GeneralRe: How is this possible? Pin
Mircea Neacsu20-May-20 2:40
Mircea Neacsu20-May-20 2:40 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 23:22
mveRichard MacCutchan19-May-20 23:22 
GeneralRe: How is this possible? Pin
Mircea Neacsu20-May-20 2:44
Mircea Neacsu20-May-20 2:44 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 4:18
mveRichard MacCutchan19-May-20 4:18 
AnswerRe: How is this possible? Pin
Richard MacCutchan19-May-20 21:38
mveRichard MacCutchan19-May-20 21:38 
QuestionQueryPerformanceCounter Pin
Calin Negru16-May-20 2:16
Calin Negru16-May-20 2:16 
AnswerRe: QueryPerformanceCounter Pin
Greg Utas16-May-20 2:29
professionalGreg Utas16-May-20 2:29 
GeneralRe: QueryPerformanceCounter Pin
Calin Negru16-May-20 3:11
Calin Negru16-May-20 3:11 
AnswerRe: QueryPerformanceCounter Pin
Richard MacCutchan16-May-20 2:58
mveRichard MacCutchan16-May-20 2:58 
GeneralRe: QueryPerformanceCounter Pin
Calin Negru16-May-20 23:00
Calin Negru16-May-20 23:00 
GeneralProject Pin
Hayley Satjan15-May-20 0:32
Hayley Satjan15-May-20 0:32 
GeneralRe: Project Pin
Richard MacCutchan15-May-20 0:51
mveRichard MacCutchan15-May-20 0:51 
GeneralRe: Project Pin
CPallini15-May-20 1:44
mveCPallini15-May-20 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.