Results 1 to 2 of 2

Thread: Problem with Java linked list

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    Problem with Java linked list

    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

  2. #2
    Join Date
    Dec 2007
    Posts
    2,291

    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;
          }
            }

Similar Threads

  1. <Identifier> Expected Error Circular Linked List Java
    By JGriff254 in forum Software Development
    Replies: 1
    Last Post: 23-03-2010, 02:25 PM
  2. Dynamic Linked List In Jsp
    By rashmi_ay in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 10:50 PM
  3. Problem using Linked list in java
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 01:06 AM
  4. Better way to sort a linked list in c++
    By Juany in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 04:30 PM
  5. What is a linked list?
    By Migueel in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 11:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,626,048.99883 seconds with 17 queries