Question

In: Computer Science

The Objective is to create a Custom LinkList. Implement the following methods Class Name: CustomLinkList Methods...

The Objective is to create a Custom LinkList. Implement the following methods
Class Name: CustomLinkList

Methods
void insert (String data) – insert it the item to the last row void delete() - Deletes the last row
boolean exists() - returns a Boolean if found
String[] toArray()
String getTail() – returns the last record
 String getHead () – return the 1st record

Extra Credit
 int delete (String data) – delete an item in the link by position.  If an item is successfully

the delete method return the number 1, else return the number 0

Solutions

Expert Solution

JAVA PROGRAM

///////////////////////////CustomLinkList.java/////////////////////////

package test.custom.linkkist;
//Class: CustomLinkList
public class CustomLinkList {
   private Node headNode;
  
   /**
   * insert data at the end of list
   * @param data
   */
   public void insert(String data){
       Node theNode = new Node(data);
      
       if(this.headNode == null){ //if no element present in the list
           this.headNode = theNode;
       }else{ //otherwise find the last node and insert data
           Node lastNode = this.headNode;
           while(lastNode.hasNext()){
               lastNode = lastNode.getNextNode();
           }
          
           lastNode.setNextNode(theNode);
       }
   }
  
   /**
   * delete the last element in the list
   */
   public void delete(){
       Node currentNode = null;
       if(this.headNode!=null){
           if(this.headNode.hasNext()){ //if there are more than one element in the list
               currentNode = this.headNode;
               while(currentNode.hasNext()){
                   Node theNextNode = currentNode.getNextNode();
                   if(!theNextNode.hasNext()){//if next node is the last element
                       currentNode.setNextNode(null);
                       break;
                   }
                  
                   currentNode = currentNode.getNextNode(); //go to next node
               }
           }else{//if there is only one element in the list
               this.headNode = null;
           }
       }
   }
  
   /**
   * checks if the data exists in the list
   * @param data
   * @return
   */
   public boolean exists(String data){
       Node currentNode = this.headNode;
       boolean isExist = false;
       if(currentNode!=null){
           do{
               String currentData = currentNode.getData();
               if(currentData.equals(data)){ //check if data matches
                   isExist = true;
                   break;
               }
              
               currentNode = currentNode.getNextNode();
           }while(currentNode!=null); //while the node exists
       }
      
       return isExist;
   }
  
   //convert all the lsit elements into array
   public String[] toArray(){
       Node currentNode = this.headNode;
       int count = 0;
       String data[] = null;
       if(currentNode!=null){
           //first find the size of the array
           do{
               count++;
               currentNode = currentNode.getNextNode();
           }while(currentNode!=null);
          
           data = new String[count]; //create the array with the count
           count = 0;
           currentNode= this.headNode;
           do{
               data[count++] = currentNode.getData(); //assign data to the array
               currentNode = currentNode.getNextNode();
           }while(currentNode!=null);
       }
      
       return data;
   }
  
   /**
   * get tail of the list
   * @return
   */
   public String getTail(){
       String tailData = null;
       if(this.headNode!= null){
           Node currentNode = this.headNode;
           while(currentNode.hasNext()){ //traverse till the last element
               currentNode = currentNode.getNextNode();
           }
          
           tailData = currentNode.getData(); //get the data
       }
      
       return tailData;
   }
  
   /**
   * returns data associated with the head element of the list
   * @return
   */
   public String getHead(){
       String headData = null;
       if(this.headNode!= null){
           headData = this.headNode.getData();
       }
      
       return headData;
   }
  
   /**
   * delete a specific data from the list
   * return 1 if successful else 0
   * @param data
   * @return
   */
   int delete(String data){
       int deleted = 0;
       if(this.headNode!= null){
           //if data to be deleted is the head of the list
           //adjust the head node
           if(this.headNode.getData().equals(data)){
               this.headNode = this.headNode.getNextNode();
               deleted =1; //return status 1
           }else{ //if data is in between
               Node currentNode = this.headNode;
               do{
                   if(currentNode.hasNext()){
                       Node nextNode = currentNode.getNextNode();
                       if(nextNode.getData().equals(data)){ //if data matched
                           //set the next node of the current node to the
                           //next node of Node to be deleted
                           currentNode.setNextNode(nextNode.getNextNode());
                           deleted =1;
                           break;
                       }
                   }
                  
                   currentNode = currentNode.getNextNode();
               }while(currentNode!=null);
           }
       }
      
       return deleted;
   }
  

}

/**
* Class: Node
*/
class Node{
   private String data;
   private Node nextNode;
  
   //constructor
   public Node(String data) {
       this.data = data;
       this.nextNode = null;
   }

   public String getData() {
       return data;
   }

   public Node getNextNode() {
       return nextNode;
   }

   public void setNextNode(Node nextNode) {
       this.nextNode = nextNode;
   }
  
   public boolean hasNext(){
       return (this.nextNode!=null?true:false);
   }
  
  
}


/////////////////////////////TestCustomLinkList.java////////////////////////

package test.custom.linkkist;

public class TestCustomLinkList {
  
   public static void main(String[] args){
       CustomLinkList cll = new CustomLinkList();
      
       cll.insert("Apple");
       cll.insert("Mango");
       cll.insert("Pineapple");
       cll.insert("Banana");
       cll.insert("Orange");
      
       String[] fruits = cll.toArray();
       System.out.println("Fruits are=> "+String.join(",", fruits));
      
       String head = cll.getHead();
       System.out.println("Head: "+head);
      
       String tail = cll.getTail();
       System.out.println("Tail: "+tail);
      
       System.out.println("Deleting last....");
       cll.delete();
      
       System.out.println(tail+" exists? :"+cll.exists(tail));
       System.out.println(head+" exists? :"+cll.exists(head));
      
       String fruitToRemove = "Pineapple";
       System.out.println("Deleting "+fruitToRemove+"......");
       int isDeleted = cll.delete(fruitToRemove);
       System.out.println("Deletion status: "+ isDeleted);
      
       System.out.println("Deleting head element:"+head);
       System.out.println("Deletion status: "+cll.delete(head));
      
       fruits = cll.toArray();
       System.out.println("Fruits are=> "+String.join(",", fruits));
      
       head = cll.getHead();
       System.out.println("Head: "+head);
      
       tail = cll.getTail();
       System.out.println("Tail: "+tail);
      
      
      
      
   }

}

=================================

OUTPUT

=================================

Fruits are=> Apple,Mango,Pineapple,Banana,Orange
Head: Apple
Tail: Orange
Deleting last....
Orange exists? :false
Apple exists? :true
Deleting Pineapple......
Deletion status: 1
Deleting head element:Apple
Deletion status: 1
Fruits are=> Mango,Banana
Head: Mango
Tail: Banana


Related Solutions

java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Adapt the original LinkList class to use the generic Gnode class Code for LinkList below class...
Adapt the original LinkList class to use the generic Gnode class Code for LinkList below class LinkList { Node llist; LinkList( int sz ) { if ( sz <= 0 ) { llist = null; } else { // start with list of size 1 llist = new Node( "0", null ); Node current = llist; // temp node for loop // add further nodes for ( int i=1; i<sz; ++i ) { // create node and attach it to...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT