Open Side Menu Go to the Top

03-10-2013 , 10:50 PM
So, I have tried for hours and just can't figure it out.

We were given the classes LinearNode<T> and LinkedStack<T> and have to write a child class the will drop the first node when pushing another element onto the stack will cause the stack to be larger than the dropout size. I can't for the life of me get the dropout part to work.

LinearNode<T>
Code:
/**
 *  @author Lewis and Chase
 *
 *  Represents a node in a linked list.
 */
package pa2;

public class LinearNode<T>
{
  /** reference to next node in list */
  private LinearNode<T> next;
  /** element stored at this node */
  private T element; 
 
  /**
   * Creates an empty node.
   */
  public LinearNode()
  {
    next = null;
    element = null;
  }
 
  /**
   * Creates a node storing the specified element.
   * @param elem element to be stored
   */
  public LinearNode (T elem)
  {
    next = null;
    element = elem;
  }
 
  /**
   * Returns the node that follows this one.
   * @return LinearNode<T> reference to next node
   */
  public LinearNode<T> getNext()
  {
    return next;
  }
 
  /**
   * Sets the node that follows this one.
   * @param node node to follow this one
   */
  public void setNext (LinearNode<T> node)
  {
    next = node;
  }
 
  /**
   * Returns the element stored in this node.
   * @return T element stored at this node
   */
  public T getElement()
  {
    return element;
  }
 
  /**
   * Sets the element stored in this node.
   * @param elem element to be stored at this node
   */
  public void setElement (T elem)
  {
    element = elem;
  }
}
LinkedStack<T>
Code:
/**
 *  @author Lewis and Chase
 *
 *  Represents a linked implementation of a stack.
 */
package pa2;
import pa2.exceptions.*;
import java.util.Iterator;

public class LinkedStack<T> implements StackADT<T>
{
  /** indicates number of elements stored */
  protected int count; //ARG changed to protected  
  /** pointer to top of stack */
  protected LinearNode<T> top; //ARG changed to protected  

  /**
   * Creates an empty stack.
   */
  public LinkedStack()
  {
    count = 0;
    top = null;
  }

  /**
   * Adds the specified element to the top of this stack.
   * @param element element to be pushed on stack
   */
  public void push (T element)
  {
    LinearNode<T> temp = new LinearNode<T> (element);

    temp.setNext(top);
    top = temp;
    count++;
  }

  /**
   * Removes the element at the top of this stack and returns a
   * reference to it. Throws an EmptyCollectionException if the stack
   * is empty.
   * @return T element from top of stack
   * @throws EmptyCollectionException on pop from empty stack
   */
  public T pop() throws EmptyCollectionException
  {
    if (isEmpty())
      throw new EmptyCollectionException("Stack");

    T result = top.getElement();
    top = top.getNext();
    count--;
 
    return result;
  }
   
  /**
   * Returns a reference to the element at the top of this stack.
   * The element is not removed from the stack.  Throws an
   * EmptyCollectionException if the stack is empty.
   * @return T element on top of stack
   * @throws EmptyCollectionException on peek at empty stack  
   */
  public T peek() throws EmptyCollectionException
  {
    if (isEmpty())
      throw new EmptyCollectionException("Stack"); 

    return top.getElement();
  }

  /**
   * Returns true if this stack is empty and false otherwise. 
   * @return boolean true if stack is empty
   */
  public boolean isEmpty()
  {
    return (count == 0);
  }
 
  /**
   * Returns the number of elements in this stack.
   * @return int number of elements in this stack
   */
  public int size()
  {
    return count;
  }

  /**
   * Returns a string representation of this stack. 
   * @return String representation of this stack
   */
  public String toString()
  {
    String result = "";
    LinearNode current = top;

    while (current != null)
    {
      result = result + (current.getElement()).toString() + "\n";
      current = current.getNext();
    }

    return result;
  }
}
Here's my code:
Code:
/*
 * Author: Alex R Gronseth
 * 
 * Date: 
 * 
 * Course: 
 * 
 * Assignment: 
 * 
 * Description: 
 */

package pa2;

/**
 *
 * @author Alex R Gronseth
 * @version 
 */
public class DropoutStack<T> extends LinkedStack<T> {
    
    private static final int DEFAULT_SIZE = 3;
    private int size;
    
    public DropoutStack()
    {
        super();
        size = DEFAULT_SIZE;
    }
    
    public DropoutStack (int inputSize)
    {
        super();
        size = inputSize;
    }
    
    public int getSize()
    {
        return size;
    }
    
    @Override
    public void push (T element)
    {
        super.push(element);
        
        if (count > size)
        {
            LinearNode<T> current = top;
            LinearNode<T> prev = null;
            
            while (current != null)
            {
                prev = current;
                current = current.getNext();
            }
            
            current = prev;
            current.setNext(null);
        }   
    }

}
Code cliffs: The push method seems to be the most relevant
Code:
    @Override
    public void push (T element)
    {
        super.push(element);
        
        if (count > size)
        {
            LinearNode<T> current = top;
            LinearNode<T> prev = null;
            
            while (current != null)
            {
                prev = current;
                current = current.getNext();
            }
            
            current = prev;
            current.setNext(null);
        }   
    }
Can someone please help me through getting the first node to drop?
Deleting the First Node From A Singly Linked List (Java) Quote
Deleting the First Node From A Singly Linked List (Java)
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Deleting the First Node From A Singly Linked List (Java)
03-10-2013 , 11:17 PM
Well, shortly after posting I of course figured it out:
Code:
    @Override
    public void push (T element)
    {
        super.push(element);
        
        if (count > size)
        {
            LinearNode current;
            current = top;
            
            while (current.getNext().getNext() != null)
            {
                current = current.getNext();
            }
            
            current.setNext(null);
            count--;
           
        }   
    }
Deleting the First Node From A Singly Linked List (Java) Quote
03-11-2013 , 12:46 AM
You should draw pictures whenever you're using linked lists. For example, if you're deleting the first node you just need its next node to become the first node and pictures help a lot with visualizing how that would work.
Deleting the First Node From A Singly Linked List (Java) Quote
Deleting the First Node From A Singly Linked List (Java)
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Deleting the First Node From A Singly Linked List (Java)

      
m