Java Program to create a user defined LinkedList class and store list of books and display
Program
import java.util.*;
class Book
{
String bname;
String author;
int price;
Book(String bname, String author, int price)
{
this.bname = bname;
this.author = author;
this.price = price;
}
public String toString()
{
String s = "Name = " + this.bname + "\tAuthor = " + this.author + "\tPrice = " + this.price;
return s;
}
}
public class BookListDemo
{
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
ll.add(new Book("Java", "Herbert", 200));
ll.add(new Book("C", "Dennis", 150));
ll.add(new Book("C++", "Schildt", 350));
ll.add(new Book("Operating Systems", "Galvin", 500));
ListIterator litr;
System.out.println("********* Book Details **********");
for( litr = ll.listIterator(); litr.hasNext();)
System.out.println(litr.next() + "\t");
}
}
Output
********* Book Details **********
Name = Java Author = Herbert Price = 200
Name = C Author = Dennis Price = 150
Name = C++ Author = Schildt Price = 350
Name = Operating Systems Author = Galvin Price = 500