Results 1 to 2 of 2
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    11

    Default Stack (push/pop/top)

    I just wanted to do a sanity check on some homework I have.

    An initially empty stack (S) has performed a total of 25 push, 12 top, and 10 pop operations, 3 of which generated EmptyStackExcpetions, which were caught and ignored. What is the size of S?

    I'm thinking 18?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    13

    Default

    I'm assuming top is the same as peek.

    It is impossible to tell without knowing the order of statements.
    Java Code:
    stack.top(); // Exception, 3 times, size = 0
    stack.push(...); // 25 calls, size = 25
    stack.pop(); // 10 calls, size = 15;
    stack.top(); // 9 times, size = 15;
    
    // or
    stack.pop(); // Exception, 3 times, size = 0;
    stack.push(...); // 25 calls, size = 25
    stack.top(); // 12 times, size = 25
    stack.pop(); // 7 times, size = 18;
    
    // or ......

Similar Threads

  1. receiving gmails push-style with Javamail
    By adhoc334 in forum Advanced Java
    Replies: 0
    Last Post: 11-25-2010, 10:29 PM
  2. Replies: 1
    Last Post: 07-17-2009, 08:08 AM
  3. How to wrap words in checkbox,push buttons
    By gunjan.raizada in forum SWT / JFace
    Replies: 3
    Last Post: 08-05-2008, 12:24 PM
  4. Stack push/pop/peek operations
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:03 AM
  5. SWT push button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-31-2007, 01:21 PM

Posting Permissions

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