IN JAVA Language- Singly Linked List Implementation
Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list.
Can Class
public class Can {
private String company;
private String content;
private double size;
private double price;
public Can() {}
public Can(String company, String content, double
size, double price) {
super();
this.company = company;
this.content = content;
this.size = size;
this.price = price;
}
public String getCompany() {
return this.company;
}
public void setCompany(String company) {
this.company = company;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
String output = this.getClass().getName() + "[
";
output += "company = " + company + ", ";
output += "content = " + content + ", ";
output += "size = " + size + ", ";
output += "price = " + price + " ]";
return output;
}
}
import java.util.*;
class Can {
private String company;
private String content;
private double size;
private double price;
public Can() {}
public Can(String company, String content, double size, double price) {
super();
this.company = company;
this.content = content;
this.size = size;
this.price = price;
}
public String getCompany() {
return this.company;
}
public void setCompany(String company) {
this.company = company;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
String output = this.getClass().getName() + "[ ";
output += "company = " + company + ", ";
output += "content = " + content + ", ";
output += "size = " + size + ", ";
output += "price = " + price + " ]";
return output;
}
}
class LinkedList <T>{
Node head; // head of list
// Linked list Node.
// This inner class is made static
// so that main() can access it
class Node {
T data;
Node next;
// Constructor
Node(T d)
{
data = d;
next = null;
}
}
// Method to insert a new node
void sortedInsert(T val)
{
Node new_node = new Node(val);
/* Special case for head node */
if (head == null || head.data.toString().compareTo(new_node.data.toString()) >0) {
new_node.next = head;
head = new_node;
}
else {
/* Locate the node before point of insertion. */
Node current = head;
while (current.next != null && new_node.data.toString().compareTo(current.next.data.toString())>0)
current = current.next;
new_node.next = current.next;
current.next = new_node;
}
}
// Method to print the LinkedList.
void printList()
{
Node currNode = head;
System.out.print("Sorted LinkedList: \n");
// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data.toString() + "\n");
// Go to next node
currNode = currNode.next;
}
}
}
class Main {
public static void main(String[] args) {
LinkedList<Can> ll = new LinkedList<Can>();
//SAmple Inputs
ll.sortedInsert(new Can("Boom","vanile",55.6,78));
ll.sortedInsert(new Can("Coca","choco",13.5,71.7));
ll.sortedInsert(new Can("Netflix","black",56.2,62.4));
ll.sortedInsert(new Can("Pepsi","coffee",84.2,45.7));
ll.sortedInsert(new Can("Zic","pine",35.8,84.56));
ll.sortedInsert(new Can("TicTac","orance",65,94.5));
//printing the sorted linked list
ll.printList();
}
}
OUTPUT:
Explanation :
We can insert new Can object at the correct sorted place of the linked list. In this way the linked list will remain sorted in alphabetical order after every insertion.
At last we can print the linked list.
Get Answers For Free
Most questions answered within 1 hours.