How to write a program using a stack

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 4
Reputation: gwenny is an unknown quantity at this point 
Solved Threads: 0
gwenny gwenny is offline Offline
Newbie Poster

How to write a program using a stack

 
0
  #1
Oct 19th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 18
Reputation: Nuez_Jr is an unknown quantity at this point 
Solved Threads: 1
Nuez_Jr's Avatar
Nuez_Jr Nuez_Jr is offline Offline
Newbie Poster

Re: How to write a program using a stack

 
0
  #2
Oct 20th, 2004
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*.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4
Reputation: gwenny is an unknown quantity at this point 
Solved Threads: 0
gwenny gwenny is offline Offline
Newbie Poster

Re: How to write a program using a stack

 
0
  #3
Oct 21st, 2004
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;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 6370 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC