making a popAndDiscard stack method

Make a method called popAndDiscard that removes and discards a user-specified number of elements from the top of the stack. This method does not return a value and it accepts a parameter called count of data type int, which is used to indicate how many elements to remove. Write a pointer-based implementation for this method. Your method should indicate that it might throw an OutOfDataException and then it should throw this type of exception if it runs out of data before completing its task.

1
2
3
4
5
6
void stack::popAndDiscard(int count) throw OutOfDataException{}

for(int i = 0; i < count; i++)
{
pop();
}


Can anyone help me out a bit, I just solved this using an array based stack, my question is if anyone can confirm this being correct. I also need help converting this to a pointer based stack. I am fairly new to pointers, I plan on going to the tutoring center in the morning, but if anyone can help walk me through this, that would be great!
Nobody can help a guy out? Please guys, I really need help.
how much code have you written .
All of the code above, I just need to make it pointer based!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void stack::popAndDiscard(int count) throw OutOfDataException{}
{
   if (isEmpty())
      throw StackException("StackException: stack empty on pop");
   else
   {  // stack is not empty delete specified amount of items
      for(int i = 0; i < count; i++)
      {
      StackNode *temp = topPtr;
      topPtr = topPtr->next;
      // return deleted node to system
      temp->next = NULL;  // safeguard
      delete temp;
      }  //end for
   }  // end if
}  // end pop 


Well this is what I came up with....Anyone see any problems with this?
Well your exception specification does not match what your function actually throws, but
exception specifications are stupid anyway, so I'd remove it altogether.

I'd also implement popAndDiscard as a simple loop that calls pop() N times
(you may need to write pop()).

Your function explodes if count is greater than the number of items on the stack (and
the number of items on the stack is non-zero).

There is no need to set temp->next to NULL since you delete temp anyway right
afterwards.
Topic archived. No new replies allowed.