Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java linked lists

  • 02-05-2001 4:37pm
    #1
    Closed Accounts Posts: 2


    Hey,

    I'm doing a program using java, and linked lists. We're not allowed use the java.util.LinkedList class. One of the things I have to do is allow a user to imput a number and for the program to return the nth element of the list. This bit I'm having problems with.

    I have 2 classes list and node to handle the linked list
    class node{
    				   String nodeData;
                       node next;
                       node(String str) {  // constructor
                           nodeData = str;
                       }
                   
    
    };
    
    class list
    {
    		public node head;
    		public node curr;
    
    		public list()
    		{
    			head = null;
    		}
    
    		public void insert(String str)
    		{
    			if(isEmpty())
    				curr = head = new node(str);
    			else
    			{
    				curr.next = new node(str);
    				curr = curr.next;
    };
    

    And then I have the main class
    import java.io.*;
    
    public class Sentences
    {
    	public static void main(String[] args) throws IOException
    	{
    		
    		list lst = new list();
    		String line;
    		//open file for reading
    		BufferedReader in = new BufferedReader(new FileReader("sentences.txt"));
    
    		//while there's lines, read them in and add them to the linked list
    		while((line = in.readLine()) != null)
    		{
    			lst.insert(line);
    	    }
    
    	}
    }
    

    Anyone got any ideas as to how to get the nth element out of this list? Thanks in advance...




Comments

  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    Well head is the first element, head.next is the second, head.next.next is the third... it's fairly easy to loop through a linked list. Use a temporary node, point it at head, and then increment a counter every time you move the temp node to the next node. Be careful to check that the number you're looking for isn't more than the amount of nodes. (Hint: check temp.next!=null in your loop).

    Probably took me longer to write that than it would the method. Shouldn't be too hard.


  • Closed Accounts Posts: 2 Kalpol Introl


    Ok so I got this method now
    public node remove(int n, list k)
    		{
    			node temp= null;
    			if (n == 1)
    			{
    				temp = head;	
    			}
    
    			return temp;
    		}
    
    Just as a small test. And I call that from within main like
    node k = lst.remove(1, lst);
    		System.out.print(k);
    
    But that just spits put gobbledegook. I'm probably doing something stupidly wrong, but any pointers would be muchly appreciated.


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    I think you want to be printing out k.nodeData there, not k. What was printing out was a pointer to the the node object.

    A handy method you can put in any class is the public String toString() method. Then you can simply do what you did, and try to print out the object. If the object has a toString() method, then what this returns will be printed out.


Advertisement