0% found this document useful (0 votes)
2 views5 pages

Kli

The document contains Java code for a simple book management system using a singly linked list. It includes classes for Book, Node, and SinglyLinkedList, with methods for inserting, deleting, displaying, and searching books. The Test class demonstrates the functionality of the linked list by performing various operations on Book objects.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Kli

The document contains Java code for a simple book management system using a singly linked list. It includes classes for Book, Node, and SinglyLinkedList, with methods for inserting, deleting, displaying, and searching books. The Test class demonstrates the functionality of the linked list by performing various operations on Book objects.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1 Book.

java
package core;
public class Book {
private int bookid;
private String booktitle;
private String bookauthor;
private double price;

public Book(int bookid, String booktitle, String bookauthor, double price) {


super();
this.bookid = bookid;
this.booktitle = booktitle;
this.bookauthor = bookauthor;
this.price = price;
}
@Override
public String toString() {
return "Book [bookid=" + bookid + ", booktitle=" + booktitle + ",
bookauthor=" + bookauthor + ", price=" + price
+ "]";
}
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getBooktitle() {
return booktitle;
}
public void setBooktitle(String booktitle) {
this.booktitle = booktitle;
}
public String getBookauthor() {
return bookauthor;
}
public void setBookauthor(String bookauthor) {
this.bookauthor = bookauthor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

2.Test.java
package core;
public class Test {

public static void main(String[] args) {

SinglyLinkedList clist=new SinglyLinkedList();

clist.insert(new Book(1, "abc","xyz", 120));


clist.insert(new Book(2, "rty","vbn", 140));
clist.insert(new Book(3, "kio","asd", 150));
System.out.println("-------------display-----------------");
clist.display();
System.out.println("-------------count------------------");
clist.nodecount();
System.out.println("--------insertion at end---------");
clist.insert(new Book(4, "insert","insert", 145));
clist.display();
System.out.println("---------deletion at end-------");
clist.deleteionatend();
clist.display();
System.out.println("---------insertion at front--------");
clist.insertionatfront(new Book(5, "jhg","ggg", 145));
clist.display();
System.out.println("--------deletion at front---------");
clist.deletionatfront();
clist.display();
System.out.println("--------delete by postion---------");
clist.deletebypostion(2);
clist.display();
System.out.println("--------insertion by postion---------");
clist.insertionbypos(new Book(11,"kxy","ytu",12000), 3);
clist.display();
System.out.println("-----searching by book title---------");
clist.searchbybooktitle("kio");
System.out.println("------arranging all book prices in descending
order-----");
clist.sortasperprice();
}

3.Node.java
package core;

public class Node {

private Book book;


private Node next;

public Node(Book book) {

this.book = book;
this.next=null;

}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
4
package core;

import java.util.ArrayList;
import java.util.List;

public class SinglyLinkedList {

private Node head;


private int count;

public SinglyLinkedList()
{
head=null;
}

public boolean insert(Book book)


{
Node newnode=new Node(book);

if(head == null)
{
head=newnode;
count++;
return true;

Node temp=head;

while(temp.getNext()!=null)
{
temp=temp.getNext();
}

temp.setNext(newnode);
count++;
return true;

public void display()


{
Node temp=head;
while(temp!=null)
{
System.out.println(temp.getBook());
temp=temp.getNext();
}
}
public void nodecount()
{
System.out.println(count);
}
public boolean deleteionatend()
{
Node temp=head;
while(temp.getNext()!=null)
{
temp=temp.getNext();
}
temp=null;
return true;
}

public boolean insertionatfront(Book book)


{
Node newnode=new Node(book);

newnode.setNext(head);
head=newnode;
return true;
}

public boolean deletionatfront()


{
head=head.getNext();
return true;
}

public boolean deletebypostion(int pos)


{
if(pos == 1)
{
head=head.getNext();
return true;
}

Node del=head;
Node temp=del;

for(int i=1;i<pos;i++)
{
temp=del;
del=del.getNext();
}

temp.setNext(del.getNext());
return true;

public void searchbybooktitle(String bt)


{
Node temp=head;
while(temp!=null)
{
if(temp.getBook().getBooktitle().equals(bt))
{
System.out.println(temp.getBook());
}
temp=temp.getNext();
}
}

public void sortasperprice()


{
List<Book> clist=new ArrayList<>();
Node temp=head;
while(temp!=null)
{
clist.add(temp.getBook());
temp=temp.getNext();
}

clist.stream().sorted((s1,s2)-
>((Double)s2.getPrice()).compareTo(s1.getPrice())).forEach((s)-
>System.out.println(s));

public boolean insertionbypos(Book book,int pos)


{
Node newnode=new Node(book);
if(pos == 1)
{
newnode.setNext(head);
head=newnode;
return true;
}

Node temp=head;
for(int i=1;i<pos-1;i++)
{
temp=temp.getNext();
}

newnode.setNext(temp.getNext());
temp.setNext(newnode);
return true;
}
}

You might also like