943,968 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9706
  • C++ RSS
Oct 19th, 2004
0

How to write a program using a stack

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwenny is offline Offline
4 posts
since Oct 2004
Oct 20th, 2004
0

Re: How to write a program using a stack

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*.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Nuez_Jr is offline Offline
18 posts
since Oct 2004
Oct 21st, 2004
0

Re: How to write a program using a stack

Quote originally posted by Nuez_Jr ...
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<iostream>

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<<endl;
}
else
{
cout<< "The two strings ARE NOT the reverse of each other. ";
cout<<endl;
}
cout<< "Do you want to enter another set?(y/n) ";
cin>>ans;
cin.get(letter);

myStack.MakeEmpty();

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


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

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

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

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

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

bool Stack:op(char &letter)
{
if(!IsEmpty())
{
mLetter=mElements[mTop];
mTop--;
return true;
}
else
return false;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwenny is offline Offline
4 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help Writing Fuction to Read Fractions From User
Next Thread in C++ Forum Timeline: Program for calculating miles per gallon





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC