Results 1 to 2 of 2
  1. #1
    mackavelirip is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Stack with nodes question

    I recently took a test for my class and got the test back and got this part of the test all wrong

    Java Code:
    public class Stack<E>
    {
           static class SNode<T>
           {
                 T data;
                 SNode<T> next;
                 SNode(T d, SNode<T n>
                 {
                     data = d;
                     next = b;
                  }
                  SNode(){this(null,null)}
            }
    
            private SNode<E> top; //pointer to top of stack
            private int size;
      
            void push(E elt){ /*this needs to be defined */}
    }
    a)
    SNode<E> tmp = new SNode<E>();
    tmp.next = top;
    top = tmp;
    top.data = elt;
    size++;

    b)
    SNode<E> tmp = new SNode<E>();
    top = tmp;
    tmp.next = top;
    tmp.data = elt;
    size++;

    c)
    top = new SNode<E>(elt,top);
    size++

    The question is whether those are the correct body for the push method. Those are the ones I got wrong so if anyone can help id appriciate it.
    If you can give me step by step instructions on what points to what and how that means its a valid push method id appriciate it.

    Oh and the answers are yes, no, and yes.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Stack with nodes question

    Better yet, why don't you explain, step for step, what you think needs to happen, and then we will help you to correct that.

Similar Threads

  1. Inserting Nodes
    By myst in forum New To Java
    Replies: 2
    Last Post: 07-12-2010, 05:26 PM
  2. Problem with Nodes - need help!
    By Smithy in forum New To Java
    Replies: 3
    Last Post: 03-20-2010, 04:47 PM
  3. How to dynamically add Nodes to JTree?
    By javanewbie in forum AWT / Swing
    Replies: 3
    Last Post: 07-13-2009, 08:20 AM
  4. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 05:22 PM
  5. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 04:09 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
  •