Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: ,

Sponsored Links


Problem with Java linked list

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-04-2012
Member
 
Join Date: Apr 2012
Posts: 1
Problem with Java linked list

Sponsored Links
I'm trying to practice it, but I seem to totally mess it up. It's simple:

I have 10 books. title, author, ISBN, and year. The linked list also should sort it by year, but I will try to do this later. First I need to get the basic frame right. I know the following is wrong and I hope somebody can help me with this.
Summary: The code will display the 10 books sorted by year.

Code:
public class Node
{
   Books book;
   Node next;

   public Node( Books bookA)
   {
       book = bookA;
   }
}

public class MyLinkedList()
{
   private Node head;
   public MyLinkedList(Books bookA)
   {
      head = new Node(bookA);
   }
   

public boolean isEmpty()
{
return first == null;
}


public add(Books bookA)
   {
      MyLinkedList current = this;
      
     
      while ( current.next != NULL )
      {
         current = current.next;
      }
      current.next = new Node(bookA);
   }

public void add(String e)
{
if (isEmpty())
{
first = new Node(e);
last = last.next;
}
}

public static void main(String [] args)
{
MyLinkedList ll = new MyLinkedList();
ll.add(Book1, author, ISBN number, year";
ll.add(Book2, author2, ISBN number2, year2;
ll.add(Book3, author3, ISBN number3, year3,;
// add rest of books here

System.out.println("The list of books:");
System.out.print(ll);
}
}
Also: Do I have to add remove and size method ? I don't think it's needed here.
I would appreciate any help!

Thank you

Reply With Quote
  #2  
Old 13-06-2012
EINSTEIN_007's Avatar
Member
 
Join Date: Dec 2007
Posts: 2,135
Re: Problem with Java linked list

Try the below code as a test and see if it helps you out:

Code:
public void insertFirst(Object o) {
      Node newNode = new Node(o, listHead);
      if (listHead == null) {
         listHead = newNode;
          listTail = listHead;
     } else {
         listHead = newNode;
      }
        }
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem with Java linked list"
Thread Thread Starter Forum Replies Last Post
<Identifier> Expected Error Circular Linked List Java JGriff254 Software Development 1 23-03-2010 02:25 PM
Dynamic Linked List In Jsp rashmi_ay Software Development 5 23-02-2010 09:50 PM
Problem using Linked list in java Aaliya Seth Software Development 5 18-02-2010 12:06 AM
Better way to sort a linked list in c++ Juany Software Development 5 13-02-2010 03:30 PM
What is a linked list? Migueel Software Development 5 28-11-2009 10:03 PM


All times are GMT +5.5. The time now is 09:47 AM.