Question

IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...

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.

  1. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods.
  2. NOT USE your language's Library List . You will receive zero points.
  3. Write a LinkedList class.
  4. Include your Can class.
  5. Write a driver (tester) call LinkListedDriver to show you have implemented all the necessary methods and appropriately implemented your LinkedList class.

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;
   }
   }

Homework Answers

Answer #1
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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
public class LinkedList {       private Node list;       public LinkedList()       {           list =...
public class LinkedList {       private Node list;       public LinkedList()       {           list = null;       }       public Node getList()       {           return list;       }       . . .//other methods       // insert method printHighEarners here       // insert method lowestPaidEmployeeRec       private class Node       {           public Employee data;           public Node next;           public Node(Employee emp)           {               data = emp;               next = null;           }           public String toString()...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly linked node, with an instance variables data and next. public class LinkedStack<T> implements Stack<T> { private Node<T> head;    public LinkedStack() { head = new Node<T>(); } // pops the top item off the stack // returning the popped value // throw an EmptyStackException if there are no // items in the stack public T pop() { // write your implementation below } }
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...