//main of programm i create a object of book and insert it into generic linked list but im unable to access all methods of book in linklist class
public static void main(String[] args) {
LinkedList<Book> list;
list = new LinkedList<>();
Book a=new Book();
a.setAuthur("ahsan");
a.setName("DSA");
a.setNoOfPages(12);
list.insertFirst(a);
}
//Linked List class
import java.util.NoSuchElementException;
public class LinkedList<AnyType> {
public class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}
/**
* @return the data
*/
public AnyType getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(AnyType data) {
this.data = data;
}
/**
* @return the next
*/
public Node<AnyType> getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(Node<AnyType> next) {
this.next = next;
}
}
private Node<AnyType> head;
/*
* Constructs an empty list
*/
public LinkedList()
{
head = null;
}
public boolean isEmpty()
{
return head == null;
}
/*
* Inserts a new node at the beginning of this list.
* @param item to be inserted in First
*/
public void insertFirst(AnyType item)
{
head = new Node<AnyType>(item, head);
}
/*
* Returns the first element in the list.
* @return First element in linked List
*/
public AnyType getFirst()
{
if(head == null) throw new NoSuchElementException();
return head.getData();
}
/*
* Removes the first element in the list.
* @return Deleted First Item
*/
public AnyType deleteFirst()
{
AnyType tmp = getFirst();
head = head.getNext();
return tmp;
}
/*
* Recursively inserts a new node to the end of this list.
* @param item to be inserted in last
*/
public void insertLast(AnyType item)
{
if( head == null)
insertFirst(item);
else
insertLast(head, item);
}
private void insertLast(Node<AnyType> node, AnyType item)
{
if(node.getNext() != null) insertLast(node.getNext(), item);
else
node.setNext( new Node<AnyType>(item, null));
}
//want to get the actual name and auther name of book here in display list method
/*
* Display all the elements in linked list
*/
public void DisplayList()
{
Node<AnyType> Current=head;
while(Current!=null)
{
System.out.print("[ "+ Current.getData()+" ] -> ");//want to print name of the book here
//i do the following but not worked Current.getData().getName();
Current=Current.getNext();
}
System.out.println("NULL");
}
}