Stacks in C++

You need to write a program in C++ that allows you to push items to a stack, and pop them off. You should allow the user to enter commands “push” then accept a value, “pop”, “match” then accept a value (display if the top items matches the value), and “exit”. If you make the command processor case insensitive then you can get extra marks, and you can also get extra marks for using ostream output (so you can use cout << stack). For more C++ programming assignments contact us for a quote.

Solution:

main.cpp

/*
* prog3_main.cpp
*/

#include <iostream>
#include “Stack.h”
#include “Utility.h”

int main(int argc, char* argv[])
{
bool running = true;
Stack mainStack;
while (running)
{
cout << endl << “Enter command: “;
string command = “”;
getline(cin, command);
command = convertToLower(command);
//cout << “DEBUG: command: ” << command << endl;

if (command == “push”)
{
// Push command
// Prompt the user to enter a word that should then be pushed on the top of
// the stack. Immediately print the state of the stack after the push operation is
// complete
cout << “Enter word: “;
string word;
getline(cin, word);
// Push to stack
mainStack.push(convertToLower(word));
// Display stack
if (!mainStack.empty())
//mainStack.display(cout);
cout << mainStack;
}
else if (command == “pop”)
{
// Pop command
// Remove the topmost word from the stack. Immediately print the state of the
// stack after the pop operation is complete.
if (mainStack.pop())
//mainStack.display(cout);
cout << mainStack;
}
else if (command == “match”)
{
// Match command
// Prompt the user to enter a word and test if that word matches the topmost
// item on the stack. Print an appropriate message in either case; the message
// should contain both the user input and the word at the top of the stack.
cout << “Enter word: “;
string word;
getline(cin, word);
if (convertToLower(word) == mainStack.top())
{
cout << “User input ” << word << ” matches top of stack” << endl;
}
else
{
cout << “User input ” << word << ” doesn’t match top of stack (” << mainStack.top() << “)” << endl;
}
}
else if (command == “exit”)
{
// Exit command
// End the program
running = false;
}
else
{
cout << “ERROR: Invalid command ” << command << endl;
}
}
}

Stack.h

/*
* Stack.h
*/

#ifndef STACK_H_
#define STACK_H_

#include <string>
using namespace std;

class Stack
{
public:
// List of public member functions
Stack(); // Default constructor
~Stack(); // Destructor
bool empty(); // Returns true if stack is empty, false otherwise
string top(); // Return the top value on the stack
void push(const string &val); // Push val on top of the stack
bool pop(); // Remove the top element from the stack
void display(ostream &out);
friend ostream& operator<<(ostream& os, const Stack& s);
private:
class Node
{
public:
string _word; // Word in each node
Node *_next; // Pointer to next node
};
Node *_top; // Pointer to top of stack
};

#endif /* STACK_H_ */

Stack.cpp

/*
* Stack.cpp
*/
#include <iostream>
#include “Stack.h”

// Constructor
Stack::Stack()
{
this->_top = NULL;
}

// Destructor
Stack::~Stack()
{
Node* currentNode = _top;
while (currentNode != NULL)
{
_top = _top->_next;
delete currentNode;
currentNode = _top;
}
}

// Returns true if stack is empty, false otherwise
bool Stack::empty()
{
return (_top == NULL);
}

// Return the top value on the stack
string Stack::top()
{
return _top->_word;
}

// Push val on top of the stack
void Stack::push(const string &val)
{
Node* newNode = new Node();
newNode->_word = val;
newNode->_next = _top;
_top = newNode;
}

// Remove the top element from the stack
bool Stack::pop()
{
Node* currentPtr = _top;
if (currentPtr != NULL)
{
_top = _top->_next;
delete currentPtr;
}
else
{
cout << “ERROR: Stack is empty” << endl;
return false;
}
return true;
}

/**
* Function that prints the contents of the calling
* object to the output stream out, starting at the top of the stack and printing one
* element per line
*/
void Stack::display(ostream &out)
{
Node* currentNode = _top;
if (empty())
{
out << “Stack is empty” << endl;
}
else
{
out << “Stack: “;
while (currentNode != NULL)
{
out << “\t” << currentNode->_word << endl;
currentNode = currentNode->_next;
}
}
}

ostream& operator<<(ostream& os, const Stack& s)
{
Stack::Node* currentNode = s._top;
if (currentNode == NULL)
{
os << “Stack is empty” << endl;
}
else
{
os << “Stack: “;
while (currentNode != NULL)
{
os << “\t” << currentNode->_word << endl;
currentNode = currentNode->_next;
}
}
return os;
}

Utility.h

/*
* Utility.h
*/

#include <string>
#include <cctype>
using namespace std;
string convertToLower(const string& input)
{
string result;
for(size_t i = 0; i < input.length(); i++)
{
result += (char)tolower(input[i]);
}
return result;
}