stacks push\pop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: stacks push\pop

  1. #1
    Join Date
    May 2010
    Posts
    76

    stacks push\pop

    I am going some exercises and just want to make sure I am doing it right. It says for the following...Assume StackType is sent to int and STACK_CAPACITY is set to 5. Give the values of myTop and the contents of the array referred to by myArray in the stack s after the code segment is executed or indictate why an error occurs.

    Stack s;
    s.push(10);
    s.push(22);
    s.push(37);
    s.pop();
    s.pop();

    I am guessing the answer is 10[6] and this is how I figured it in case I am doing it wrong. I starts with s at 5 and each push increases by one and each pop decreases by 1. Please let me know if this is not how I am supposed to do this question.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: stacks push\pop

    I'm guessing that STACK_CAPACITY means the Stack uses a fixed-size array internally. Therefore, if the STACK_CAPACITY is 5, the only valid stack indexes will be 0,1,2,3 and 4.

  3. #3
    Join Date
    May 2010
    Posts
    76

    Re: stacks push\pop

    so 0 would be s, 1 would be 10, etc, etc?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: stacks push\pop

    I'm not sure where you're getting the idea that "s" is at one of the indexes....s is the stack object as a whole.

  5. #5
    Join Date
    May 2010
    Posts
    76

    Re: stacks push\pop

    ok so 0 is 10, 1 is 22, and 2 is 37....so with 2 .pop then the answer would be 10[0]

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: stacks push\pop

    That would be my interpretation, yes.

  7. #7
    Join Date
    May 2010
    Posts
    76

    Re: stacks push\pop

    OK thanks and by the way u dont have to help me through all of these especially if you are busy...Here is my next question...

    Stack s;
    s.push(10);
    s.push(9);
    s.push(8);
    while(!s.empty())
    s.pop();

    and the while (!s.empty() equation is myTop < Stack_CAPACITY - 1 so 2 < 4 so this statement can be ignored correct?

  8. #8
    Join Date
    May 2010
    Posts
    76

    Re: stacks push\pop

    did some additonal reading and shouldnt this remove everything from the stack...While (!s.empty()) = myTop

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: stacks push\pop

    Quote Originally Posted by jimJohnson123 View Post
    and the while (!s.empty() equation is myTop < Stack_CAPACITY - 1 so 2 < 4 so this statement can be ignored correct?
    Curious. That would seem to imply the myTop index increments downwards from STACK_CAPACITY to 0 rather than the other way around, which is a bit odd, but a valid design.

  10. #10
    Join Date
    May 2010
    Posts
    76

    Re: stacks push\pop

    Aight this is what I got for all four problems so maybe I should turn it around. I am not asking for answers but for these 4 problems if any feedback could be offered Id really appreciate it...

    1.

    Stack s;

    s.push(10);

    s.push(22);

    s.push(37);

    s.pop();

    s.pop();



    This answer I put 10[0]



    2.

    Stack s;

    s.push(10);

    s.push(9);

    s.push(8);

    while(!s.empty())

    s.pop();



    This answer I put cannot be done because it goes empty and then removes a s.pop



    3.

    Stack s;

    for (int i = 1; i <= 6; i++)

    s.push(10*i);



    Cannot be done because I is multiplied by 10 and the
    highest it can be is 6



    4.

    Stack s;

    s.push(11);

    i = s.top();

    s.pop();



    This answer I put
    3[0]



    If these are wrong can someone explain to me what I need to put in here

  11. #11
    Join Date
    Apr 2008
    Posts
    726

    Re: stacks push\pop

    2.
    it only pops when s is NOT empty

    3.
    why does multiply by ten matter when that is the value put into the stack? What you should look at is how many times push() is called in the for loop.

    4. where does your answer '3' come from?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center