Hello Everybody Here !!

I'am a noob to programming, i am having problem in understanding the STACKS in C++, I'm just confused that how to Delete a Value From Stak, How to count The Top and One More thing How to Transfer the values of One Stack into another by using a single "for" Loop.

The logic i've been tryng to implement is that if a want to dlete a value from stack the i have to declare a second stack, then the top of the first stack will be reduced by one i.e. "top=top-1;" then tranfer the values of first stack into second one till the index to be deleted, then just push back the values from stack2 to stack1 and the desired value wil be delete,, but am not able to do that in a single loop..

i'll be very thankful if somene answer me with a detailed program, abviously in c++ , am using Turbo C++ ...

Thanks You
Regards
Pappu-Linux

A stack is just a "last-in, first-out" (or LIFO) structure. You can make one with an array:

``````const unsigned STACK_SIZE = 50;
unsigned stack_used = 0;
int stack[ STACK_SIZE ];``````

To add an item, just stick it at the end of the stack and bump the number of items used:

``````if (stack_used < STACK_SIZE)
stack[ stack_used++ ] = 42;``````

To remove an item, just decrement the number of items used:

``````if (stack_used > 0)
stack_used--;``````

The reason it is called a stack is because of the way it works. Consider when you go to your local buffet restaurant. At the end of the lines of food there are stacks of plates (probably the cool ones that sink-into/rise-out-of the counter). You can add a plate by sticking it on the top. You can remove a plate by taking it off of the top.

The item on the top of the stack is always `stack[ stack_used - 1 ]` .

Hope this helps.

You don't really need to push the elements onto another stack. I'm assuming that you use a linked list to implement the stack. In that case you can just loop through the stack until you find the element you want to remove, delete it, and then point the previous element in the list to the next element. This takes O(n) time, and requires only one loop. Here some pseudocode...

stack = stack with elements.
elem = element to remove.
prev = null = temp variable to store an elem pointer.

``````for each element el in stack
if elem is equal to el
if prev is null
delete el and point root to next elem
else
delete el and point prev to next elem
end
break loop if you don't want to delete duplicates.
end
prev = el
end``````

something like that.