0

First of all, here's my codes:

public class SLLNode
{
    protected Object element;
    protected SLLNode succ;
    
    protected SLLNode( Object elem, SLLNode successor )
    {
        element = elem;
        succ = successor;
    }
}
public class LinkedStack
{
    private SLLNode top;
    
    public LinkedStack( )
    {
        top = null;
    }
    
    public boolean empty( )
    {
        return top == null;
    }
    
    public Object getLast( ) throws Exception
    {
        if ( top == null )
            throw new Exception( "Empty Stack" );
        else
            return top.element;
    }
    
    public void clear( )
    {
        top = null;
    }
    
    public void addLast( Object elem )
    {
        top = new SLLNode( elem, top );
    }
    
    public Object removeLast( ) throws Exception
    {
        if ( top == null )
            throw new Exception( "Empty Stack in Remove Last" );
        Object topElem = top.element;
        top = top.succ;
        return topElem;    
    }
}
import java.util.Scanner;
import java.io.File;

public class TextReverse
{
    public static void main ( String [] args )
    {
        LinkedStack ls = new LinkedStack( );
        String line;
        try
        {
            Scanner textFile = new Scanner( new File( "sample.txt" ) );
            while( textFile.hasNextLine( ) )
                ls.addLast( textFile.nextLine( ) );
            while(! ls.empty( ) )
                System.out.println( ls.removeLast( ) );
        }
        catch ( Exception e )
        {
            System.out.println( e.getMessage( ) );
        }
        
    }
}

And here's whats in my scanner file:
hello
hi
jsibgj
afhebwfjhbr
adfsf

The program compiles 100% but when it runs it will only print out the last element in the list, "adfsf", but nothing else, and for the life of me I cannot see what's going wrong folks, any help would be appreciated!!

Thanks,
Mick

1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by mickinator
0

Lol stupid mistake, I saw it when I posted it, stupid protected lol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.