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;
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
}
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.