In: Computer Science
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
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