Question

In: Computer Science

in java This class will require the following fields: Node<T> head: the first Node in the...

in java

This class will require the following fields:

  • Node<T> head: the first Node in the chain
  • Node<T> tail: the last Node in the chain
  • int size: Keeps a count of the number of Nodes in the chain

Your LinkedList class must also support the following public methods.

  • LinkedList(): A default constructor sets both pointers to null and sets the size to 0.
  • int size(): Returns the size of the LinkedList.
  • void push_back(T): Creates a new Node and assigns it to the end of the list of Nodes while updating the size by 1.
  • void push_front(T): Creates a new Node and assigns it to the front of the list of Nodes while updating the size by 1.
  • T pop_back() throws ListEmptyException: Deletes the Node at the end of the list (or throws a ListEmptyException if this is not possible). Decreases size by 1. Returns the deleted element.
  • T pop_front() throws ListEmptyException: Deletes the Node at the front of the list (or throws a ListEmptyException if this is not possible). Decreases size by 1. Returns the deleted element.
  • T at(int) throws ListException: Returns the element stored at the specified index. If the index is out-of-bounds (either for being negative or greater than/equal the size of the list), throw a ListException with the message "Index out of bounds.” This method should operate at O(1) for accessing the first or last element in a list and at O(n) for any element other than the first or the last.
  • T front() throws ListEmptyException: Returns the first element (throws a ListEmptyException if the list is empty)
  • T back() throws ListEmptyException : Returns the last element (throws a ListEmptyException if the list is empty)
  • void printAll(): prints all the nodes in the LinkedList

Solutions

Expert Solution

Java Code:

LinkedList.java

package queue;

class ListEmptyException extends Exception
{
public ListEmptyException(String s)
{
super(s);
}
}

class ListException extends Exception
{
public ListException(String s)
{
super(s);
}
}

class Node<T>
{
   T data;
   Node<T> next;
   Node(T data)
   {
       this.data = data;
       this.next = null;
   }
}


public class LinkedList<T> {
   private Node<T> head;
   private Node<T> tail;
   private int size;
  
  
   LinkedList() {
       head=tail=null;
       size = 0;
   }
  
   int size() {
       return size;
   }
  
   void push_back(T data) {
       Node<T> new_node = new Node<T>(data);
       if(head==null) {
           head = tail = new_node;
       }
      
       tail.next = new_node;
       tail = new_node;
       size++;
   }
  
   void push_front(T data) {
       Node<T> new_node = new Node<T>(data);
       if(head==null) {
           head = tail = new_node;
       }
      
       new_node.next = head;
       head = new_node;
       size++ ;
   }
  
   T pop_back() throws ListEmptyException {
       if(head == null)
           throw new ListEmptyException("Empty List.");
      
       Node<T> temp = head;
      
       if(head == tail) {
           head=tail=null;
           return temp.data;
       }
      
       while(temp.next != tail) {
           temp = temp.next;
       }
      
       T data = tail.data;
       tail = temp;
      
       return data;
   }
  
   T pop_front() throws ListEmptyException {
       if(head == null)
           throw new ListEmptyException("Empty List.");
      
       T data = head.data;
      
       if(head == tail) {
           head=tail=null;
           return data;
       }
      
      
      
       head = head.next;
      
       return data;
   }
  
   T at(int n) throws ListException {
       if(n < 1 || n>size)
           throw new ListException("Index out of bounds.");
      
       Node<T> temp = head;
      
       while(n > 1) {
           temp = temp.next;
           n--;
       }
      
       return temp.data;
   }
  
   T front() {
       return head.data;
   }
  
   T back() {
       return tail.data;
   }
  
   void printAll() {
       Node<T> temp = head;
      
       while(temp != tail.next) {
           System.out.print(temp.data + " ");
           temp = temp.next;
       }
       System.out.println();
   }
  
   public static void main(String[] args) throws ListException, ListEmptyException {
      
       LinkedList<Integer> L = new LinkedList<Integer>();
       L.push_front(1);
       L.push_front(0);
       L.push_back(2);
       L.push_back(3);
      
       System.out.print("List: "); L.printAll();
       System.out.println("Size: " + L.size());
       System.out.println("Front: " + L.front());
       System.out.println("Back: " + L.back());
       System.out.println("At 2: " + L.at(2));
       System.out.println("Pop from front: "+ L.pop_front());
       System.out.println("Pop from back: " + L.pop_back() + "\n");
       System.out.print("List: "); L.printAll();
      
   }
  
}

Pic for indentation:

Output :

List: 0 1 2 3
Size: 4
Front: 0
Back: 3
At 2: 1
Pop from front: 0
Pop from back: 3

List: 1 2

Hope you like it

Any Query? Comment Down!

I have written for you, Please upvote the answer as it encourage us to serve you Best !


Related Solutions

Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
Write the class MultiDimList according to the following requirements: 1. Each node contains 3 fields of...
Write the class MultiDimList according to the following requirements: 1. Each node contains 3 fields of data (Name, ID, Mark out of 100). 2. Each node will have two pointer: nextMark, nextName 3. The nodes can be sorted in an ascending way based on the Mark or the Name The List will have the following: • FirstMark : a Pointer to point to the first node based on the Mark sorting scheme • FirstName: a Pointer to point to the...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT