delete every occurance of a specified item from a stack

Mar 4, 2010 at 5:09am
I'm trying to delete every occurrence of a specified item from aStack, leaving the order of the remaining items unchanged. Has anyone done this before or have any ideas. Im pretty sure its possible using the adt operations....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/** Delete every occurrence of item from aStack.
 *
 * @pre None.
 *
 * @post Every element with value item are deleted from aStack and the
 *       remaining elements are in the same order as they were in the
 *       stack.
 *
 * @param aStack Is the stack to delete elements from.
 *
 * @param item Is the value to remove from aStack. */
deleteItem(inout aStack:Stack, in item:StackItemType)
{
   auxStack.createStack()

Mar 4, 2010 at 5:10am
oh also Suppose that you have a stack aStack and an auxiliary stack auxStack.
Mar 4, 2010 at 5:20am
what is your starting idea to solve this homework...

what do you think is the auxiliary stack for(i can imagine at least 2 solutions - 1 has nothin to do with the problem :D)...
Mar 4, 2010 at 5:29am
haha well, I was thinking you would start by popping all the items from aStack, compare top item in the stack to the item and if the top item in the stack is = to the value dont copy the item. Then somehow push everything back onto the stack. I think I have the right idea, I just don't know how to code it!
Mar 4, 2010 at 5:49am
one solution could be:

get the containers size()...
create an array/container...
pop the elements
sort out/replace(jumper) the elements
push
Last edited on Mar 4, 2010 at 5:51am
Mar 4, 2010 at 5:53am
hmm, how would you get the size of the container...I know there is an isEmpty method but not a getLength or anthing like that
Mar 4, 2010 at 5:54am
check the reference for the container (left upper link menu)....

//assingment for lazy students: find the () in the post above:P...
Last edited on Mar 4, 2010 at 5:56am
Mar 4, 2010 at 6:00am
I see that would work, but that would not be the answer he is looking for. I should have written the question better. You can ONLY use Stack Member Function....
As you know these would be:
· stack::member functions:
· stack::empty
· stack::pop
· stack::push
· stack::size
· stack::top
Mar 4, 2010 at 6:04am
he?... schizophrenic?... anyways,,, i already answered your question...

since (as i seem to get from your last post) you are disallowed to use an array and your auxiliary stack is not for wasting ram...
Incubbus wrote:
what do you think is the auxiliary stack for
Mar 4, 2010 at 6:09am
schizophrenic like you wouldn't believe...Could you explain the auxiliary stack to me?
Mar 4, 2010 at 6:20am
isnt it an stack, too?.... <- if yes (and you should know wether or not) then it is an stack...

cool, isnt it?:D...

(come on, dont play lazy... we(im schizo, too^^) know you can do it... take a deep breath , take a 5minute breake... read everything after and you should understand...)...
Last edited on Mar 4, 2010 at 6:22am
Mar 4, 2010 at 6:26am
Thanks man, I honestly have read this chapter 2 times and it never mentioned anything about using two stacks, but im sure your right. Ill give it a shot, otherwise there is always the tutoring center in the morning. Thanks alot!
Mar 4, 2010 at 6:31am
joking?... please read your second post...

oh also Suppose that you have a stack aStack and an auxiliary stack auxStack
Mar 4, 2010 at 6:55am
hahaha WOW... I don't know what to say. My bad!
Mar 4, 2010 at 7:10am
did you finally solve it?...

loop:
pop element...
check if it is the one to be excluded...
if yes, dont push it on the auxiliary stack,
if not, push it on the auxiliary stack.
when finished:
loop:
pop from auxstack
push on astack...

finito...

this definetly should help you...
Mar 4, 2010 at 7:19am
Perfect, that basically exactly what I have! Thanks, you were a huge help!
Mar 4, 2010 at 7:22am
because i am bored and cant sleep(its 8 in the morning) i wrote this program to tell you what to do... just follow its instructions and you may be getting what u want...

you could also write an program to take what is between the '' and concatenate it;)...

Type: 'f'. Type: 'o'. Type: 'r'. Type: '('. Type: 'i'. Type: 'n'. Type: 't'. Type: ' '. Type: 'i'. Type: ' '. Type: '='. Type: ' '. Type: '1'. Type: ';'. Type: ' '. Type: 'i'. Type: ' '. Type: '<'. Type: ' '. Type: 'a'. Type: '.'. Type: 's'. Type: 'i'. Type: 'z'. Type: 'e'. Type: '('. Type: ')'. Type: ';'. Type: ' '. Type: 'i'. Type: '+'. Type: '+'. Type: ')'. Type: '{'. Type: 'c'. Type: ' '. Type: '='. Type: ' '. Type: 'a'. Type: '.'. Type: 'p'. Type: 'o'. Type: 'p'. Type: '_'. Type: 'b'. Type: 'a'. Type: 'c'. Type: 'k'. Type: '('. Type: ')'. Type: ';'. Type: 'i'. Type: 'f'. Type: '('. Type: 'c'. Type: ' '. Type: '='. Type: '='. Type: ' '. Type: 'w'. Type: 'h'. Type: 'a'. Type: 't'. Type: 'e'. Type: 'v'. Type: 'e'. Type: 'r'. Type: 'u'. Type: 'w'. Type: 'a'. Type: 'n'. Type: 't'. Type: 't'. Type: 'o'. Type: 'e'. Type: 'x'. Type: 'c'. Type: 'l'. Type: 'u'. Type: 'd'. Type: 'e'. Type: ')'. Type: '{'. Type: 'c'. Type: 'o'. Type: 'n'. Type: 't'. Type: 'i'. Type: 'n'. Type: 'u'. Type: 'e'. Type: ';'. Type: '}'. Type: 'a'. Type: 'u'. Type: 'x'. Type: '.'. Type: 'p'. Type: 'u'. Type: 's'. Type: 'h'. Type: '_'. Type: 'b'. Type: 'a'. Type: 'c'. Type: 'k'. Type: '('. Type: 'c'. Type: ')'. Type: ';'. Type: '}'. Type: 'f'. Type: 'o'. Type: 'r'. Type: '('. Type: 'i'. Type: 'n'. Type: 't'. Type: ' '. Type: 'i'. Type: ' '. Type: '='. Type: ' '. Type: '1'. Type: ';'. Type: ' '. Type: 'i'. Type: ' '. Type: '<'. Type: ' '. Type: 'a'. Type: 'u'. Type: 'x'. Type: '.'. Type: 's'. Type: 'i'. Type: 'z'. Type: 'e'. Type: '('. Type: ')'. Type: ';'. Type: ' '. Type: 'i'. Type: '+'. Type: '+'. Type: ')'. Type: '{'. Type: 'a'. Type: '.'. Type: 'p'. Type: 'u'. Type: 's'. Type: 'h'. Type: '_'. Type: 'b'. Type: 'a'. Type: 'c'. Type: 'k'. Type: '('. Type: 'a'. Type: 'u'. Type: 'x'. Type: '.'. Type: 'p'. Type: 'o'. Type: 'p'. Type: '_'. Type: 'b'. Type: 'a'. Type: 'c'. Type: 'k'. Type: '('. Type: ')'. Type: ')'. Type: ';'. Type: '}'.


EDIT: You´re welcome...
Last edited on Mar 4, 2010 at 7:23am
Mar 4, 2010 at 7:25am
jees that's pretty intense.... Did you seriously just write that?
Mar 4, 2010 at 7:40am
as i said a program i written wrote that for me from the code i thought it might be^^^...
Topic archived. No new replies allowed.