stack problem !



Hi guys,

I'm a beginner in c++ , so I'm facing problems in my project :/ !

Now, I have this project about using STACKS ..
It is about a container that that hold some colors , after the user enter the colors he wants in the container , he will choose one of these color to remove from the container ..
for example ,
if he types :

Green
Red
Yellow
Red
White
Black
Yellow

then he choose to remove the " Red " color !!

I need to show the stack at the same order above after the color removal !
it will be :

Green
Yellow
White
Black
Yellow

....

I think that I need two stacks !
but I don't know how to code it correctly !

....

I did a little bit of the program :

# include <iostream>
# include <fstream>
using namespace std;
const char maxstack = 12;

class stack_type
{
public:
void clear_stack();
bool empty_stack();
bool full_stack();
void push(char color);
void pop(char& color);
int top;
};



int main()
{
stack_type s1;
int i;
char color,rcolor;

s1.clear_stack();
s2.clear_stack();


i = 0;
cout << "\nEnter 5-12 color in the container ( Green, Black, Red, White), Enter # to quit\n";
cin >> color;

while (!(s1.full_stack()) && color !='#')
{
s1.push(color);
i++;
if (!(s1.full_stack()))
{
cin >> color;
//cout << color << endl;
}

}

if (s1.full_stack())
cout << "\nFull Stack! \n";
while (!(s1.empty_stack()))
{
s1.pop(color);
cout << color << "\n";
}

cout<< " Which color do wish to remove from the container ? \n";
cout<< " COLOR = ";
cin>> rcolor;

return 0;
}

//----------------------------------------------------------------------
void stack_type::clear_stack()
{
top = 0;
}
//----------------------------------------------------------------------
bool stack_type::empty_stack()
{
if (top==0)
return true;
else
return false;
}
//----------------------------------------------------------------------
bool stack_type::full_stack()
{
if (top==maxstack-1)
return true;
else
return false;
}
//----------------------------------------------------------------------
void stack_type::push(char color)
{
top = top + 1;
stack[top]=color;
}
//----------------------------------------------------------------------
void stack_type::pop(char& color)
{
color = stack[top];
top = top - 1;
}
OFF TOPIC: You call yourself a beginner in C++ ? Not at all...
ON TOPIC: Check here: http://www.cplusplus.com/reference/stl/stack/
What's so hard? If you use 2 stacks, you read the colours as strings in the first stack, and read the cour he wants to remove in some other string. then you push the top element of the first stack on to the top element of the second stack iff it's not the one he chose to remove, and pop that element from the first stack so you can access the next. then, becouse it is now in reverse, you push and pop all the elements of the second stack back in the first stack. easy peasy. If you REALY think you cant code it by your sself i can show you.
Last edited on
Now, I have this project about using STACKS ..
It is about a container that that hold some colors , after the user enter the colors he wants in the container , he will choose one of these color to remove from the container ..
for example ,
if he types :

Green
Red
Yellow
Red
White
Black
Yellow

then he choose to remove the " Red " color !!

I need to show the stack at the same order above after the color removal !
it will be :

Green
Yellow
White
Black
Yellow


Typically, this isn't really a stack. A stack would usually follow LIFO (last in, first out) conventions. You wouldn't usually be able to remove (pop) and element from inside of it. Think of it like a stack of plates. When you add a plate, you add it to the top. To remove a plate, you take the top plate from the pile. But you can't remove a plate from the middle of the pile.

I think that a vector would be better suited: http://www.cplusplus.com/reference/stl/vector/
@iHutch105: it IS possible to do it with a stack, if you have 2 of them and you exchance data from one to another, like I said in my earlier post. Do you want me to prove it and post a code?
Oh, wait, I see now that you aren't using reals tack, but stacks defined in the program. Sorry, I didn't even look at your program, I gave up when I saw it's formatting and I just wrote a solution for regular stacks. Now, do you actualy have to use stacks, but you dint know that they are already defined, or did you realy need to redefine them?
bump
You can erase elements from a stack using only one stack. (Do mind that removing any element from any 'contiguous memory' data structure costs O(n) operations)

1
2
3
4
5
6
7
8
9
int deleteFrom(Stack &myStack, Element &myEm) {
    int found = 0;
    for (int current = 0; current < myStack.size; ++current) {
        if (myStack[current] = myEm) found++;
        else myStack[current-found] = myStack[current];
    }
    myStack.size -= found;
    return found;
}

What it does is copy every element to its new location. The new location depends on its old location and the amount of items that were deleted between the start and the old location. For example, in the list {5, 6, 6, 6, 6, 6, 6, 9, 6, 6, 6} if I deleted '6', 9 would be handled like this:
myStack[7-6] = myStack[7];
Because 9 was located at 7, but 6 elements were deleted between the first item and 9's old location. It's new location is thus: new = (old-deleted) = (7-6) = 1.

All 'deleted' elements are simply copied over. There are still elements after 'size', but they are ignored by setting the stack size to (size-found). 'Out of bounds' is the same as 'doesn't exist'.
@ jumper007 : Thank you ^^



@ viliml : Actually , I need to use only stacks in this program , and I really really appreciated if you show me a code for this .. THANK YOU



@ iHutch105 : Thank you very much ^^ , but I have to use stacks to solve this .

Here, this should do it. Actualy, not that it SHOULD work, but it DOES work, I've tested it! Hope it helps your project!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stack>
#include <cstdlib>

using namespace std;

int main()
{
    stack<string> instack, outstack;
    int n, i;
    string a, b;
    cin>>n;
    for (i=0; i<n; i++)
    {
        cin>>a;
        instack.push(a);
        }
    cin>>b;
    while (instack.size())
    {
        if (instack.top()!=b) outstack.push(instack.top());
        instack.pop();
    }
    while (outstack.size())
    {
        cout<<outstack.top()<<endl;
        outstack.pop();
    }
    system ("pause");
    return 0;
}

Study it and see if you understand!
Last edited on
Oh, and gaminic: TOTALY WRONG!!!! the is NO operator[] for stacks! the whole point of stacks is last in first out! Lets take iHutch105's example:a stack of plates. Your code would be taking a plate from the middle of the stack, and if you did that, the whole stack would fall and break! Oh and you can't modify the size of a stack directly, you have to pop it! I think you should go to the "beginners" sub-forum. You mightlear a LOT of new stuff. after a month or so studying there you can come back here and study the post here, and after another month or so you can start posting! DO NOT TALK ABOUT THINGS YOU DON'T UNDERSTAND!!!
@viliml: The report button is for lodging complaints with the site admin about very objectionable behavior. By that criteria, I'd be fully justified in reporting your post here:
viliml wrote:
Oh, and gaminic: TOTALY WRONG!!!! the is NO operator[] for stacks! the whole point of stacks is last in first out! Lets take iHutch105's example:a stack of plates. Your code would be taking a plate from the middle of the stack, and if you did that, the whole stack would fall and break! Oh and you can't modify the size of a stack directly, you have to pop it! I think you should go to the "beginners" sub-forum. You mightlear a LOT of new stuff. after a month or so studying there you can come back here and study the post here, and after another month or so you can start posting! DO NOT TALK ABOUT THINGS YOU DON'T UNDERSTAND!!!
From http://cplusplus.com/forum/general/62474/#msg338347

...just because of how rude you were to him. You could have just said that you think that Gaminic was confounding stacks and vectors in a much nicer tone, don't you think?

Also, you gave the OP a full solution to a problem, which is something we really discourage here just because of how much of the learning process it allows the OP to bias. Some people are really good about studying and learning from the solution, but sadly most aren't. :(

Please behave yourself in the future. :)

-Albatross
Last edited on
Oh, and gaminic: TOTALY WRONG!!!! the is NO operator[] for stacks! the whole point of stacks is last in first out! Lets take iHutch105's example:a stack of plates. Your code would be taking a plate from the middle of the stack, and if you did that, the whole stack would fall and break! Oh and you can't modify the size of a stack directly, you have to pop it! I think you should go to the "beginners" sub-forum. You mightlear a LOT of new stuff. after a month or so studying there you can come back here and study the post here, and after another month or so you can start posting! DO NOT TALK ABOUT THINGS YOU DON'T UNDERSTAND!!!


Wow, yeah, that was completely uncalled for. Not conducive to a learning environment and certainly not in the spirits of this board.

What's more, Gaminic's example is actually right. While the STL stack does not provide access to it's members through the [] operator, it's clear that the stack Gaminic has written is his own and not that of the STL.

As for full solutions, I'd agree with Albatross. I tend to judge each post on individual merit but, generally speaking, it's not best to hand out full solutions. If you get someone to post their code and they're pretty close, then show them the way is usually fine. If someone hasn't posted any progress, chances are they're going to struggle to learn by looking at something that has been completely written by someone else. Even after years of programming, I find one of the hardest things to do is understand other people's code due to various factors such as preferences and habits.

Please get into the mindset that we're all here to help each other. A couple of your comments in this thread are borderline condescending, even though you might think you're helping. I'd take the good advice offered by Albatross in the above post and you'll get on fine on these boards. :-)
Topic archived. No new replies allowed.