Reverse string using stacks

Hello, I am unsure where to begin in my int main file. I am trying to store a string and print the string backwards using stacks. I need help in my main function file.

(i) Ask the user to enter a sentence.
(ii) Use cin.get(ch) to read each character, ch, of the sentence, and then insert ch to a stack (push()).
(iii) After reading the whole sentence and inserting all characters to the stack, print out the sentence backwards by using the member functions of the stack (top() & pop()).

Please enter a sentence: This is an example!
!elpmaxe a si sihT

************************
Header File

//linkedStackType.h
#ifndef LINKEDSTACKTYPE_H
#define LINKEDSTACKTYPE_H
#include <iostream>
using namespace std;

template<class Type>
struct nodeType
{
Type info;
nodeType<Type> * link;
};

template<class Type>
class linkedStackType
{
private:
nodeType<Type> * stackTop;
void copyStack(const linkedStackType<Type> & otherStack);
public:
const linkedStackType<Type>& operator=(const linkedStackType<Type>&);
void initializeStack();
bool isEmptyStack() const;
void push(const Type& newItem);
Type top();
void pop();
linkedStackType(); // Constructor
linkedStackType(const linkedStackType<Type> & otherStack); //Copy Constructor
~linkedStackType(); //Destructor
};

#endif // LINKEDSTACKTYPE
***************************
//linkedStackType.cpp
#include "linkedStackType.h"
#include <iostream>
#include <cassert>
using namespace std;

//Default Constructor
template<class Type>
linkedStackType<Type>::linkedStackType()
{
stackTop = NULL;
}

//Copy Constructor
template<class Type>
linkedStackType<Type>::linkedStackType(
const linkedStackType<Type> & otherStack) //Copy Constructor
{
stackTop = NULL;
copyStack(otherStack);
}

//Destructor
template<class Type>
linkedStackType<Type>::~linkedStackType()
{
initializeStack();
}

//Operator=
template<class Type>
const linkedStackType<Type>& linkedStackType<Type>::operator=(
const linkedStackType<Type>& otherStack)
{
if(this != &otherStack) //avoid self-copy
copyStack(otherStack);

return *this;
}

//Destroy Stack
template<class Type>
void linkedStackType<Type>::initializeStack()
{
nodeType<Type> * temp;
while(stackTop!=NULL)
{
temp = stackTop;
stackTop = stackTop->link;
delete temp;
}
}

//Empty Stack
template<class Type>
bool linkedStackType<Type>::isEmptyStack() const
{
return (stackTop == NULL);
}

//Return the Top Element
template<class Type>
Type linkedStackType<Type>::top()
{
assert(stackTop != NULL);
return stackTop->info;
}

//Pop stack
template<class Type>
void linkedStackType<Type>::pop()
{
nodeType<Type> *temp; //pointer to deallocate memory

if(stackTop != NULL)
{
temp = stackTop; //set temp to point to top node
stackTop = stackTop->link; //advance stackTop to next node
delete temp; //delete the top node
}
else
cout << "Canno remove from an empty stack." << endl;
}

//Push Stacknode
template<class Type>
void linkedStackType<Type>::push(const Type& newItem)
{
nodeType<Type> * newNode; //Pointer to create new node
newNode = new nodeType<Type>; //Create node
newNode->info = newItem; //Store newItem in node
newNode->link = stackTop; //Insert newNode before stackTop
stackTop = newNode; //Set stackTop to point to top node
}

I wish I had extra space in my sleep.


Your code is incomplete and not formatted for easy reading. Fix those two things and people will be much more likely to help you.

The main program should look much like you expect, and follow the instructions exactly. Read a string. Use a loop to push every character on the stack. Use another loop to pop each character, printing as you go.

Good luck!
Topic archived. No new replies allowed.