JAVA This method returns whether the LinkedList contains a

JAVA

/**
   * This method returns whether the LinkedList contains a given number.
   *
   * Examples:
   * elem : 0 and LinkedList : 2 --> 3 --> null ==> return false
   * elem : 1 and LinkedList : 1 --> -3 --> null ==> return true
   * elem : -2 and LinkedList : -2 --> 3 --> -2 --> null ==> return true
   */

The code I have is:

   public boolean contains(int elem) {
       return false;
   }

Solution

package snippet;


public class LinkedList<T> {

private Node<T> head;
private Node<T> tail;
//function to add data
public void add(T element){

Node<T> nd = new Node<T>();//create new node
nd.setValue(element);//set value to node

//if list is empty,create head node
if(head == null){
  
head = nd;
tail = nd;
} else {
  
tail.setNext(nd);//insert node to end
  
tail = nd;
}
System.out.println(\"Added: \"+element);
}
//function to display linked list
public void Display(){

Node<T> tmp = head;//start from head
while(true){
if(tmp == null){
return;
}
System.out.println(tmp.getValue());//print node data
tmp = tmp.getNext();//go to next node
}
}


public int dataAtPosition(int pos) {
   Node<T> tmp = head;
     
while(true)
{
if(tmp == null)//break if list is empty
{
   break;
  
}
if(pos==0)
   break;
  
tmp = tmp.getNext();//go to next node
pos--;
}
return (int) tmp.getValue();

}

public boolean contains(int elem) {
   Node<T> tmp = head;
while(true)
{
if(tmp == null)//break if list is empty
{
   break;
  
}
int data=(int) tmp.getValue();//get value of node
if(data==elem)//compare value of current node with elem
   return true;
else
tmp = tmp.getNext();//go to next node
}
return false;//if nothing matches return false
}
public static void main(String a[]){
LinkedList<Integer> ll = new LinkedList<Integer>();
ll.add(2);
ll.add(3);
System.out.println(\"Linked List is \");
ll.Display();
  
if(ll.contains(2))
{
   System.out.println(\"Linked list contains 2 \");
}
else
{
   System.out.println(\"Linked list does not contain 2 \");

}
  



  

}
}

class Node<T> implements Comparable<T> {

private T value;
private Node<T> nextRef;

public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNext() {
return nextRef;
}
public void setNext(Node<T> ref) {
this.nextRef = ref;
}
@Override
public int compareTo(T arg) {
if(arg == this.value){
return 0;
} else {
return 1;
}
}
}

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

Output:

Added: 2
Added: 3
Linked List is
2
3
Linked list contains 2

JAVA /** * This method returns whether the LinkedList contains a given number. * * Examples: * elem : 0 and LinkedList : 2 --> 3 --> null ==> return fa
JAVA /** * This method returns whether the LinkedList contains a given number. * * Examples: * elem : 0 and LinkedList : 2 --> 3 --> null ==> return fa
JAVA /** * This method returns whether the LinkedList contains a given number. * * Examples: * elem : 0 and LinkedList : 2 --> 3 --> null ==> return fa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site