473,416 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

push and pop - stacks

Hi,

Sorry to be a pest...

But I can figure this out.

I'm pushing to a stack. then I need to check to see if the word is a
palindrome.
Is the code below correct?
if so, how can I check the entire word?

when I display "word" I get the last letter pushed into the stack.

Any help would be greatly appreciated.

thank you,
Bill

----------------------code ------------------
#include <iostream.h>
#include <fstream.h>
#include <string>

class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);

int array[9];
int top;
};

int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;

char newarray[8];

s.clear_stack();

cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";

i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";

while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}

cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;

for (z=1; z<=9; z++)
{

cout << word;
cin >> newarray[z];
}
}

cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;

while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}
Jul 19 '05 #1
6 23170

"Will" <ke****@secret.com> wrote in message
news:NC********************@comcast.com...
Hi,

Sorry to be a pest...
You're not being a pest.
But I can figure this out.

I'm pushing to a stack. then I need to check to see if the word is a
palindrome.
Is the code below correct?
No. See below.
if so, how can I check the entire word?

when I display "word" I get the last letter pushed into the stack.
You have provided insufficient information for
any meaningful diagnosis.

----------------------code ------------------
#include <iostream.h>
#include <fstream.h>


Putting aside for the moment these nonstandard
headers:

You have omitted the information critical to
determining the cause of the problem: the
definitions of the member functions of
your 'palindrom_stack' class.

Post the actual code for the program which is
not behaving as you desire. This missing pieces
prevent us from analyzing it or compiling it and
observing its behavior, so of course we have no
way of identifying where you went astray.

-Mike
Jul 19 '05 #2

"Will" <ke****@secret.com> wrote in message news:NC********************@comcast.com...
#include <iostream.h>
#include <fstream.h>
#include <string>
Do not USE iostream.h and fstream.h. They are not standard. Many implementations
actually get a lot of heartburn when you mix the standard versions of the files with the
bogus ones (as you do with std::string and non-std::iostream).
class palindrome_stack
Well you might show us the implementation of this.
int i, max;
char word;
Declare these as you need (and initialize them). The above is fraught with perils as you
see later.
std::string test;
You should not clutter your confused application with things you aren't using.

s.clear_stack();

cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";

i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";

while (!(s.full_stack(max)))
{
s.push(word);
The first time through the loop, word has no deterministic value at this point.
cin >> newarray[z];


What are you doing here? You read 9 characters from the standard input without
any prompt to the user. You also write leave newarray[0] unset, and write two
bytes past the end of the allocation. All this is very bad.
Jul 19 '05 #3
Sorry guys,

here is the actual code...
------------------------------------

#include <iostream.h>
#include <fstream.h>
#include <string>

class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);

int array[9];
int top;
};

int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;

char newarray[8];

s.clear_stack();

cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";

i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";
cin >> test;

while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}

// for (int z=1; z<9; z++)
// {
// cout <<"\n new array "<<newarray[z];
// }

cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;

for (z=1; z<=9; z++)
{

cout << word;
cin >> newarray[z];
}
}

cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;

while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}

return 0;
}
// CLEARING THE STACK
void palindrome_stack::clear_stack()
{
top = 0;
}

// DETERMINING IF THE STACK IS FULL
bool palindrome_stack::full_stack(int max)
{
if (top == max)
return true;
else
return false;
}

//PUSHING TO THE STACK
void palindrome_stack::push(char word)
{
top = top + 1;
array[top] = word;
}

// CHECK TO SEE IF STACK IS EMPTY
bool palindrome_stack::empty_stack(int top)
{
if (top == 0)
return true;
else
return false;
}

// POPPING FROM THE STACK
void palindrome_stack::pop(char &word)
{
word = array[top];
top = top - 1;
}



"Will" <ke****@secret.com> wrote in message
news:NC********************@comcast.com...
Hi,

Sorry to be a pest...

But I can figure this out.

I'm pushing to a stack. then I need to check to see if the word is a
palindrome.
Is the code below correct?
if so, how can I check the entire word?

when I display "word" I get the last letter pushed into the stack.

Any help would be greatly appreciated.

thank you,
Bill

----------------------code ------------------
#include <iostream.h>
#include <fstream.h>
#include <string>

class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);

int array[9];
int top;
};

int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;

char newarray[8];

s.clear_stack();

cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";

i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";

while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}

cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;

for (z=1; z<=9; z++)
{

cout << word;
cin >> newarray[z];
}
}

cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;

while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}

Jul 19 '05 #4

"Will" <ke****@secret.com> wrote in message news:Wb********************@comcast.com...
Sorry guys,

here is the actual code...
You didn't fix the things we pointed out.

void palindrome_stack::clear_stack()
{
top = 0;
}
Would be nice if your class had a constructor that initialized it
to clear.

// DETERMINING IF THE STACK IS FULL
bool palindrome_stack::full_stack(int max)
Why do you make the caller pass in the stack size, when you
are not ready to really handle numbers bigger than 8. The
class should manage this.

if (top == max) \ void palindrome_stack::push(char word)
{
top = top + 1;
array[top] = word;
}
You will write off the end of your "array" arg. You have several
major problems. FIRST, you should let the caller decide if he
has pushed too many things on the stack, you should check it
here. You know you leave array[0] unset in this case. Probably
would be better to increment top AFTER storing word.
// POPPING FROM THE STACK
void palindrome_stack::pop(char &word)
{
word = array[top];
top = top - 1;


Again you shouldn't trust the caller to not pop an empty stack here.
Jul 19 '05 #5


Will wrote:

Sorry guys,

here is the actual code...
Suggestion:

Fire up your debugger and step through the code.
While doing do so examine the values of variables as they
change. This way you will get a better feeling of what is going
on in which order in your code.

eg.

[snip] i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";
cin >> test;

while (!(s.full_stack(max)))
{
s.push(word);
Interesting. When the loop is entered the first time, 'word' has not
received any value up to now, yet you push it on the stack ...
i++;
cin >> word;
and now 'word' receives a value the first time ...
cout <<"\n"<<word;
}


.... but if the stack happens to be full, the last read character
in 'word' isn't processed at all.

As said: use your debugger to step through the code. It will be a good
exercise in techniques on how to find logical bugs in your code.
BTW: logical bugs and how to find and fix them is an important issue
in everday life as a programmer. So developing skills on how to do
this is an important issue too.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6
Here is just some things you might want to think about that aren't really
part of any code in specific. Assuming that the stack is set up correctly
this should only actually take two for loops and a comparion with a cout
statement to run if I understand the problem correctly. You are just
supposed to take in a word from the user and then. Basically say "Yes this
is a palindrome" or "No this isn't a palindrome." I'm assuming this is for
some class you are taking right now or else from some book that your
reading. Either way. It was hard for me to follow your code and understand
what you are trying to do.
Basically this is the approach I would take.
First of all the class
The clear_stack function looks fine.
The emtpy_stack function should not have to take an argument. Top is
already a variable that is a public part of the class although it shouldn't
be public. Nor should the array be. You don't need any external data to
figure it out.
Full stack you lack the data and have to look else where. But this
shouldn't be true. You should have a "max" or "capacity" variable in your
class which is mazimum number of elements that it can contain. Then your
function would look fine with no arguments being passed.
Push looks okay, but it depends on how you are using your array index.
Remember with an array of size 8 the index run from 0 to 7 not from 1 to 8.
Your pop function looks ok as well, however I would have just returned a
char with it as opposed to using a reference variable inside. But that is
personal preference.
Set up your array as dynamic memory. It and befor you do a push funtion
check to see if the stack is full and if it is allocate more memory for it.
I am assuming you guys covered a little bit of it. You might not have I
know we did similar things earlier in my c++ courses too where we just hard
coded the size in.

With that in mind look at your main program and think about what you are
doing. I don't know exactly what your code is trying to do but if I read it
correctly it isn't doing it right.
I am assuming you know what a palindrome is although I had to look it up
myself just to make sure :) Its a word like bob or dad where it is the same
backwards and forwards. So now the question is how to use a stack to check
for them. Well you started off with the right idea you need to take the
user input of a word again, I don't think you want to go one at a time here.
Take in a char*. I am assuming you guys covered something of pointers and
such. If not then I guess you have to keep doing what you are doing. But a
char* would be a lot easier and require less loops and everything else.
Loop one should be a for loop where you look at each letter of the word and
input it into the stack.
should look something like this.
for(int i=0; i<strlen(word);i++)
s.push(word[i]);
}

Now, the neat thing about a stack is when you take them off they will be in
reverse order.
so that should be the second loop;
char* new_word = new char[strlen(word)+1]
int k = 0;
while(!s.empty_stack())
{
s.pop(new_word[k]);
i++;
}
I'm sure there is a better way of writing the above function in a for loop.
But I couldn't think of one without a size function in the palindrome_stack
class. Which wouldn't be a bad addition.
now all you need yet is a simple if statement
if(strcmp(word, new_word)==true)
cout << word << " is a palindrome.";
else
cout << word << " is not a palindrome.";

and that should be all you have to write. i hope this helps a bit.
~Justin

"Will" <ke****@secret.com> wrote in message
news:Wb********************@comcast.com...
Sorry guys,

here is the actual code...
------------------------------------

#include <iostream.h>
#include <fstream.h>
#include <string>

class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);

int array[9];
int top;
};

int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;

char newarray[8];

s.clear_stack();

cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";

i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";
cin >> test;
Here you should add an initial "cin >> word;" so that the first time your
run through the push function you aren't just throwing random garbage onto
your stack.
Also, I think you probably understand this but just from glancing at your
code. Are you sure you want word to be just a char? I would have made it a
char* and then accessed each character individually and added it to the
stack. The way it is set up now the user can only enter one character at a
time and then hit enter and run through the word that way. you should
change your cout above to indicate if that is what you want them to do.
while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}

// for (int z=1; z<9; z++)
// {
// cout <<"\n new array "<<newarray[z];
// }

cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;

for (z=1; z<=9; z++)
{

cout << word;
cin >> newarray[z];
}
}

cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;

while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}

return 0;
}
// CLEARING THE STACK
void palindrome_stack::clear_stack()
{
top = 0;
}

// DETERMINING IF THE STACK IS FULL
bool palindrome_stack::full_stack(int max)
{
if (top == max)
return true;
else
return false;
}

//PUSHING TO THE STACK
void palindrome_stack::push(char word)
{
top = top + 1;
array[top] = word;
}

// CHECK TO SEE IF STACK IS EMPTY
bool palindrome_stack::empty_stack(int top) you don't need the argument above. { top is already a part of the function. if (top == 0)
return true;
else
return false;
}

// POPPING FROM THE STACK
void palindrome_stack::pop(char &word)
{
word = array[top];
top = top - 1;
}



"Will" <ke****@secret.com> wrote in message
news:NC********************@comcast.com...
Hi,

Sorry to be a pest...

But I can figure this out.

I'm pushing to a stack. then I need to check to see if the word is a
palindrome.
Is the code below correct?
if so, how can I check the entire word?

when I display "word" I get the last letter pushed into the stack.

Any help would be greatly appreciated.

thank you,
Bill

----------------------code ------------------
#include <iostream.h>
#include <fstream.h>
#include <string>

class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);

int array[9];
int top;
};

int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;

char newarray[8];

s.clear_stack();

cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";

i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";

while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}

cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;

for (z=1; z<=9; z++)
{

cout << word;
cin >> newarray[z];
}
}

cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;

while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}


Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vanessa T. | last post by:
Hello All! Is there a place where I can learn stacks, (push and pop) etc. Thanks,
18
by: pmm | last post by:
Hi all, Plz dont fire at me if this is a silly question Is there any way to know in which direction stack grows pmm
1
by: LedZep | last post by:
This program has to use a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and...
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
0
by: raghuveer | last post by:
i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an element form any of the stack..This is what i have #include<stdio.h>...
14
Mhel
by: Mhel | last post by:
I'm really having a hard time with Stack... The problem goes this way: Create an array ( a ). If choice == 1, push number. If choice == 2, pop number, If choice == 3, display stack. If...
5
by: flaca2 | last post by:
I need help understanding how a stack works in the following code stack1.push(10) stack1.push(8) stack2.push(2) stack1.push(3) while !(stack1.isempty()) x=stack1.top; stack1.pop();
4
by: aemado | last post by:
I am creating a program that takes a specified number of nodes in a stack...from here, certain nodes are to be removed based on criteria within the node. Once these certain nodes are removed, they...
8
by: cerise | last post by:
I can't figure out how to make and handle multiple stacks and use them so I could create four linked list stacks representing each suit of cards, one stack each for diamonds, hearts, spades, and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.