Stacking Help!

Started by
11 comments, last by Leanna 19 years, 10 months ago
quote:Original post by Leanna
void Stack:ush(double item_to_push)
{
data[top_of_stack] = item_to_push;
top_of_stack++;
if (top_of_stack > SIZE)
cout << "Error \n";
}


getting closer. i apologize for not reading the entire thread, but if top_of_stack is the position of the actual item that is top_of_stack then you''ll want to increment top_of_stack _before_ you push the item_to_push onto the stack. if top_of_stack points to the first "empty" position at the top of the stack, ignore this:

think of it as a simple array and numbers.

say i have this:
int myArray[10];
myArray[o] = some_item;
int top_of_stack = 0; //top_of stack now "points" to some_item

if we go through your function now:
data[top_of_stack] = item_to_push;

at this point top_of_stack == 0, so you''ve put item_to_push into the zeroth position of the array. however, some_item was there, so you''ve now over-written it.

make sense? so increment top_of_stack _before_ you put the item into the array. you''ll want to do a check against SIZE between that increment and the actual insertion of the item_to_push.

hope this is all starting to make sense.

-me

Advertisement
Thank you for the help. I think I understand what you''re saying about the increment coming first. If the Push function is now correct, I will be needing help with the Pop function, and the others as well. I''ve tried writing the whole program, and it compiles without errors, however, when I run the program it''s not working correctly.
I''ll paste the whole thing below again.
Thank you again!
Leanna
#include <cstdlib>
#include <iostream>
using namespace std;

#define SIZE 6

class Stack
{
private:
double data[SIZE];
int top_of_stack;
public:
void Init();
void Push(double item_to_push);
double Pop();
};

void Stack::Init()
{
for (int k = 0; k < SIZE; k++)
data[k] = 0.0;

top_of_stack= 0;
}

void Stack:ush(double item_to_push)
{
top_of_stack++;
data[top_of_stack] = item_to_push;


if (top_of_stack > SIZE)
cout << "Error \n";
}

double Stack:op()
{
if (top_of_stack > 0)
top_of_stack--;

else cout<< "Error: Stack Empty \n";

}

int main()
{

Stack s1, s2;
int k;

s1.Init();
s2.Init();

cout << s1.Pop();

s1.Push(60);
s1.Push(50);
s1.Push(40);
s1.Push(30);
s1.Push(20);
s1.Push(10);

s2.Push(85);
s2.Push(75);
s2.Push(65);
s2.Push(55);
s2.Push(45);
s2.Push(35);

cout <<"\n--------------------First Stack----------------\n";
for (k=0; k<8; k++)
cout << s1.Pop() << endl;


cout <<"\n--------------------Second Stack----------------\n";
for (k=0; k<8; k++)
cout << s2.Pop() << endl;
system ("pause");
return 0;
}
You're really close. I'll take it as you having chosen option B I mentioned above, and say that Push is fine now, except for the fact that you should check to make sure that top_of_stack when incremented isn't > SIZE (or for that matter, top_of_stack isn't == SIZE, keep in mind that arrays always count starting with 0 and the count ends one integer before the size of the array) BEFORE you try putting the data in there, or you could cause a memory related fault. Init is mostly fine too, except that since top_of_stack for you now means the current level that there is data on, top_of_stack should be initialized to -1, so that it's capable of filling in position 0 with a push, and it should be allowed to go back to -1 but no lower. The main problem I can see is that you're not returning the value on Pop. So, you need to add a double for a return value, and set that to the element you're popping before the decrement, like this:

double foo = 0;
foo = data[top_of_stack];
top_of_stack--;
return foo;

That should work I think. Really, it doesn't matter what you're modelling top_of_stack as, either the current top plate, or the space above the current top plate, as long as you're consistent about it.

The other thing you might want to fix is this:
cout <<"\n--------------------First Stack----------------\n";for (k=0; k<8; k++)cout << s1.Pop() << endl;should becout <<"\n--------------------First Stack----------------\n";for (k=0; k<s1.top_of_stack; k++)cout << s1.Pop() << endl;

It's bad to hardcode something that's supposed to be dynamic.

Be sure to ask questions if you're not sure why something works, because it's important to understand all this.

-fel


[edited by - felisandria on June 3, 2004 10:55:49 PM]
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

This topic is closed to new replies.

Advertisement