Results 1 to 2 of 2
  1. #1
    putman91 is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default return size of LinkedStack<E>

    I have an assignment where I need to use an "if statement" to return the current size and a toString() method of the Stack after each token. I need help to figure out the code for that outputs something in this matter:

    Enter a valid postfix expression:
    5 4 3 * +
    Stack contents after token 5: 5
    Stack contents after token 4: 4 5
    Stack contents after token 3: 3 4 5
    Stack contents after token *: 12 5
    Stack contents after token +: 17


    import java.util.EmptyStackException;

    /** Class to implement interface StackInt<E> as a linked list.
    * @author Koffman & Wolfgang
    **/
    public class LinkedStack<E> implements StackInt<E>
    {

    // Insert inner class Node<E> here (see Listing 2.1)
    /** A Node is the building block for a single-linked list. */
    private static class Node<E>
    {
    // Data Fields

    /** The reference to the data. */
    private E data;
    /** The reference to the next node. */
    private Node<E> next;

    // Constructors
    /**
    * Creates a new node with a null next field.
    * @param dataItem The data stored
    */
    private Node(E dataItem)
    {
    data = dataItem;
    next = null;
    }

    /**
    * Creates a new node that references another node.
    * @param dataItem The data stored
    * @param nodeRef The node referenced by new node
    */
    private Node(E dataItem, Node<E> nodeRef)
    {
    data = dataItem;
    next = nodeRef;
    }
    }
    //End of inserted class
    // Data Fields
    /** The reference to the first stack node. */
    private Node<E> topOfStackRef = null;

    /**
    * Insert a new item on top of the stack.
    * @post The new item is the top item on the stack.
    * All other items are one position lower.
    * @param obj The item to be inserted
    * @return The item that was inserted
    */
    @Override
    public E push(E obj)
    {
    topOfStackRef = new Node<E>(obj, topOfStackRef);
    return obj;
    }

    /**
    * Remove and return the top item on the stack.
    * @pre The stack is not empty.
    * @post The top item on the stack has been
    * removed and the stack is one item smaller.
    * @return The top item on the stack
    * @throws EmptyStackException if the stack is empty
    */
    @Override
    public E pop()
    {
    if (empty())
    {
    throw new EmptyStackException();
    }
    else
    {
    E result = topOfStackRef.data;
    topOfStackRef = topOfStackRef.next;
    return result;
    }
    }

    /**
    * Return the top item on the stack.
    * @pre The stack is not empty.
    * @post The stack remains unchanged.
    * @return The top item on the stack
    * @throws EmptyStackException if the stack is empty
    */
    @Override
    public E peek()
    {
    if (empty())
    {
    throw new EmptyStackException();
    }
    else
    {
    return topOfStackRef.data;
    }
    }

    /**
    * See whether the stack is empty.
    * @return true if the stack is empty
    */
    @Override
    public boolean empty()
    {
    return topOfStackRef == null;
    }

    public int size()
    {
    //if statement to find the current size
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: return size of LinkedStack<E>

    Pease edit your post to use code tags. (The links are to the relevant forum faq.)

Similar Threads

  1. Return the current size of dynamic array
    By RozenKristal in forum New To Java
    Replies: 13
    Last Post: 10-01-2012, 09:04 PM
  2. Replies: 7
    Last Post: 05-26-2012, 08:07 PM
  3. Double the size of a 2d
    By smokinjoachim in forum Java 2D
    Replies: 1
    Last Post: 01-20-2011, 07:28 AM
  4. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  5. Size of JTable
    By andre1011 in forum Advanced Java
    Replies: 3
    Last Post: 04-15-2009, 05:55 AM

Posting Permissions

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