write a doublylinked list in java goal To maintain a ranked
write a doubly-linked list in java
goal: To maintain a ranked list of popular books for a library.
Your task is to maintain a doubly-linked list (with no dummy nodes) of books ranked by their
popularity — thus, the list is kept in order of rank, with the maximum rank (most popular) book first.
A book’s popularity is measured by activity at the library that references the book (a librarian adds a new
copy of the book, a library patron queries the book, or borrows, or puts a hold on the book) — the more
the book is queried, borrowed, etc., the higher its rank in the list.
(Note that you are NOT doing a complete library simulation. You are not maintaining a catalogue of all
books in the library system, or tracking library patrons. You are only maintaining a ranked list of popular
books.)
Your popular book list for the current day will begin with the previous day’s top n books (see “Input”
below), with each book having an initial rank of 1 (a low rank that will get higher if the book is involved
in activity in the current day). Each time a book is involved in a library command — someone queries,
borrows, or puts a hold on the book, or a librarian adds a new copy of the book to circulation, you should:
• If the book is already in your popular book list, then increase its rank by one, and then move (unlink
and relink) the node containing the book to its proper place in the list (remember: the list is kept in
order of rank, with the maximum rank first).
• If the book is not in your popular book list, then insert it into the list with rank 1.
Input
Your application class (the class that contains main) must prompt for the name of the input file.
The input file contains
• An integer n on the first line, then
• The authors and titles of the top n books (one per line) from the previous day, then
• The current day’s commands (one per line).
An input line containing one of the previous day’s top n books will contain the author’s name (in quotes)
and the book’s title (in quotes). The following line is an example:
\"Oliver Sacks\" \"The Man Who Mistook His Wife for a Hat\"
(You are to insert the previous day’s top n books into a doubly-linked list, giving each the rank of 1 — this
is the initial popular book list for the current day.)
The current day’s command are a mix of the following:
• Add (\'A\'): Add a new book to the library.
• Query (\'Q\'): Get more information about a book for a library patron.
• Hold (\'H\'): Place a hold on a book (used when somebody else has borrowed the book, or the book is
at a different location than the library patron).
• Borrow (\'B\'): Borrow a book.
• Return (\'R\'): Return a book that was previously borrowed.
Each command contains the letter (\'A\' or \'Q\' or \'H\' or \'B\' or \'R\')), the author’s name (in quotes) and
the book’s title (in quotes). The following line is an example command:
Q \"Robert Zurbin\" \"The Case for Mars\"
You should write a book class. Each book instance must contain the title and author of the book.
You should write a node class. Each node will contain a rank (an int ) and a book (or you could use
generics instead of book), as well as back (to the previous node) and forward (to the next node) pointers.
You can either use an object-oriented node class (outside your linked list class, similar to my Version 2) or
something similar to my non-object-oriented node class (inside your linked list class, like my Version 1) —
except that the nodes must be doubly-linked.
You should write a linked list class, which needs (at least) methods to:
• Insert a book with a given rank if it is not already in the list,
• Increment the ranks of a book: find the book, and — if it is in the list — increase its rank by 1 and
move it to its new correct position in the ordered list (you must unlink and relink the node),
• Print the n highest rank books, and
• Print all of your popular book list.
Of course, you need to write your application class that contains main and that does all the input and
organizes all the output, using your linked list and book classes to keep track of popular books.
input.txt
5
\"Svante Paabo\" \"Neanderthal Man: In Search of Lost Genomes\"
\"The Medieval Murderers\" \"Hill of Bones\"
\"Herman Melville\" \"Moby-Dick; or, The Whale\"
\"Sue Sorensen\" \"A Large Harmonium\"
\"Karen Kucera Bate\" \"Tales of a Starving Actress\"
Q \"Robert Zurbin\" \"The Case for Mars\"
B \"Sue Sorensen\" \"A Large Harmonium\"
R \"James S. A. Covey\" \"Abaddon\'s Gate\"
A \"Jane Yolen\" \"Tales of Wonder\"
B \"Svante Paabo\" \"Neanderthal Man: In Search of Lost Genomes\"
Q \"Sue Sorensen\" \"A Large Harmonium\"
Q \"James S. A. Covey\" \"Abaddon\'s Gate\"
H \"Sue Sorensen\" \"A Large Harmonium\"
Q \"Sue Sorensen\" \"A Large Harmonium\"
H \"James S. A. Covey\" \"Abaddon\'s Gate\"
B \"Herman Melville\" \"Moby-Dick; or, The Whale\"
R \"Herman Melville\" \"Moby-Dick; or, The Whale\"
Q \"Herman Melville\" \"Moby-Dick; or, The Whale\"
Q \"Herman Melville\" \"Moby-Dick; or, The Whale\"
R \"Svante Paabo\" \"Neanderthal Man: In Search of Lost Genomes\"
Solution
/**
* Created by RahulPatidar on 24/10/16.
*/
public class Book {
private String title;
private String author;
public Book(String author, String title){
this.title = title;
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String toString(){
return author +\" : \"+title;
}
public boolean equals(Book b){
return author.equals(b.getAuthor()) && title.equals(b.getTitle());
}
}
/**
* Created by RahulPatidar on 24/10/16.
*/
public class Node<T> {
private int rank;
private Node back;
private Node forward;
private T data;
public Node(T data,int rank){
this.data = data;
this.rank = rank;
}
public Node getBack() {
return back;
}
public void setBack(Node back) {
this.back = back;
}
public Node getForward() {
return forward;
}
public void setForward(Node forward) {
this.forward = forward;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public boolean equals(Node b){
return data.equals(b.getData()) && rank==b.getRank();
}
}
import java.io.File;
import java.util.Scanner;
/**
* Created by RahulPatidar on 24/10/16.
*/
public class RankList {
private Node<Book> head; // head of doubly linkedlist
private Node<Book> tail; // tail of dooubly linkedlist
private int size; // size of doubly linkedlist
//**** add a node to rankList ****//
public void add(Node<Book> newNode){
if(head==null){
head = newNode;
}else if(tail==null){
newNode.setBack(head);
head.setForward(newNode);
tail = newNode;
}else{
tail.setForward(newNode);
newNode.setBack(tail);
tail = newNode;
}
size++;
}
//**** find a node corresponding to a book entry ****//
public Node findBook(Book book){
Node<Book> cur = head;
while(cur!=null && !book.equals(cur.getData())){
cur = cur.getForward();
}
return cur;
}
//**** increase rank of a book ****//
public void increaseRank(Book book){
Node<Book> node = findBook(book);
Node<Book> cur = head;
while(cur!=null && cur.getRank()>(node.getRank())){
cur = cur.getForward();
}
if(node.equals(cur)){
cur.setRank(cur.getRank()+1);
return;
}
else if(cur==null){
add(node);
}else {
node.setRank(node.getRank()+1);
Node<Book> nextNode = node.getForward();
Node<Book> backNode = node.getBack();
Node<Book> curBack = cur.getBack();
if(backNode!=null)backNode.setForward(nextNode);
if(nextNode!=null)nextNode.setBack(backNode);
node.setForward(cur);
cur.setBack(node);
if (cur.equals(head)){
node.setBack(null);
head = node;
}else {
node.setBack(curBack);
curBack.setForward(node);
}
}
}
// **** print N top books of day ****//
public void printNHighestRank(int n){
Node<Book> cur = head;
while(cur!=null && n>0){
System.out.println(cur.getRank()+\" : \"+cur.getData().toString());
cur = cur.getForward();
n--;
}
}
//**** print all books of present day ****//
public void printRankList(){
Node<Book> cur = head;
while(cur!=null){
System.out.println(cur.getRank()+\" : \"+cur.getData().toString());
cur = cur.getForward();
}
}
//**** method for parsing a line into auther and title ****//
public static String[] linetoAutherTitle(String line){
String[] lineAr = line.split(\" \");
String author=lineAr[0];
String title=\"\";
int k = 1;
while((lineAr[k].charAt(lineAr[k].length()-1)!=\'\\\"\')){
author += \" \"+lineAr[k];
k++;
}
author += \" \"+lineAr[k]; k++;
title = lineAr[k]; k++;
while(k<lineAr.length && lineAr[k].charAt(lineAr[k].length()-1)!=\'\\\"\'){
title += \" \"+lineAr[k];
k++;
}
title += \" \"+lineAr[k];
String[] res = new String[2];
res[0] = author;
res[1] = title;
return res;
}
public static void main(String args[]){
try {
Scanner sc = new Scanner(System.in);
System.out.print(\"Input File Name: \");
String fileName = \"./\"+sc.nextLine();
File file = new File(fileName);
sc = new Scanner(file);
String line = sc.nextLine();
int n = Integer.parseInt(line);
RankList rankList = new RankList();
String[] autherTitle;
String author;
String title;
//**** Adding previous day entry **** //
for(int i=0; i<n ; i++){
line =sc.nextLine();
autherTitle = linetoAutherTitle(line);
author = autherTitle[0];
title = autherTitle[1];
Book book = new Book(author,title);
Node<Book> node = new Node<Book>(book,1);
rankList.add(node);
}
while(sc.hasNextLine()){
line = sc.nextLine();
char query = line.charAt(0);
autherTitle = linetoAutherTitle(line.substring(2));
author = autherTitle[0];
title = autherTitle[1];
Book book = new Book(author,title);
Node<Book> node = new Node<Book>(book,1);
if(query== \'A\' || query == \'Q\' || query ==\'H\' || query ==\'B\' || query==\'R\'){
if(rankList.findBook(book)!=null){
rankList.increaseRank(book);
}else{
rankList.add(node);
}
}
}
rankList.printRankList();
}catch(Exception e){
e.printStackTrace();
}
}
}





