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
Bookmarks