954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to write a program using a stack

I'm trying to write a program using a stack to determine if the two sequences of characters are the mirror image of each other. Can anyone help me to get started?

gwenny
Newbie Poster
4 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Consider the definition of a stack - last-in, first-out. If you load a bunch of stuff into a stack, and then take it all out again, you now have your original items *in reverse order*.

Nuez_Jr
Newbie Poster
18 posts since Oct 2004
Reputation Points: 10
Solved Threads: 1
 
Consider the definition of a stack - last-in, first-out. If you load a bunch of stuff into a stack, and then take it all out again, you now have your original items *in reverse order*.


Thank you so much for your response. I understand the logic but somehow it's not reading that it's the reverse when I enter the two strings like this for example....hello:olleh. It should say "These two strings ARE the reverse of each other but instead it's saying that they are not. Can you please look over this code and see what I've done wrong. I know it's something with my code in main. If you could please reply as soon as possible I would really appreciate your help. Again Thanks.........

#include

using namespace std;

const char MAX_ITEMS = 50;


class Stack
{
char mElements[MAX_ITEMS];
char mLetter;
int mTop;

public:
Stack();
bool IsEmpty()const;
bool IsFull();
bool Push(char letter);
bool Pop(char &letter);
void MakeEmpty();
};


int main()
{

Stack myStack;
char letter;
char other_letter;
char ans;
bool different = false;

do
{
cout<< "Enter two strings separated only by a : ";
cin.get(letter);
while(letter!= ':' && letter!= '\n' && !myStack.IsFull())
{
myStack.Push(letter);
cin.get(letter);
}
if (letter != ':')
cin.get(letter);
different = true;

while (!myStack.IsEmpty() && letter!= '\n')
{
myStack.Pop(other_letter);
if (other_letter != letter)
different = true;
cin>>letter;
}

if (!myStack.IsEmpty() && letter =='\n')
different = true;

if(myStack.IsEmpty() && letter != '\n')
{
while (letter!= '\n')
cin.get(letter);
different = true;
}

if (!different )
{
cout<< "The two strings ARE the reverse of each other. ";
cout<>ans;
cin.get(letter);

myStack.MakeEmpty();

}
while (ans!= 'n' && ans!= 'N');
return 0;
}


Stack::Stack()
{
mTop = -1;
}

void Stack::MakeEmpty()
{
mTop = -1;
}

bool Stack::IsEmpty()const
{
return(mTop == -1);
}

bool Stack::IsFull()
{
return(mTop == MAX_ITEMS -1);
}

bool Stack::Push(char letter)
{
if(!IsFull())
{
mTop++;
mElements[mTop] = letter;
return true;
}
else
return false;
}

bool Stack::Pop(char &letter)
{
if(!IsEmpty())
{
mLetter=mElements[mTop];
mTop--;
return true;
}
else
return false;
}

gwenny
Newbie Poster
4 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You