|
| ||||||||||
| Tags: frame, java |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Problem with Java linked list
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);
}
} I would appreciate any help! Thank you |
|
#2
| ||||
| ||||
| 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;
}
}
__________________ Education, Career and Job Discussions |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |