In: Computer Science
(Implement MyLinkedList) The implementations of the methods
contains(E
e), get(int index), indexOf(E e), lastIndexOf(E e), and
set(int
index, E e) are omitted in the MyLinkedList class. Implement these
methods.
//java program implementing the required methods
// java class representing the node of a generic linked list
public class GenericNode<T>
{
private T date;
private GenericNode<T>next;
public GenericNode(T data,GenericNode<T>next)
{
this.data=data;
this.next=next;
}
public void setData(T data)
{
this.data=data;
}
public void setNext(GenericNode<T>next)
{
this.next=next;
}
public T getData()
{
return data;
}
public GenericNode<T>getNext()
{
return next;
}
}
/end of generic node
//java program to implement methods contains ,get,indexOf and set method of MyLinkedList generic class
public class MyLinkedList<T>
{
private int size;//size of the list
privateGenericNode<T>head;//point to head of the list
//constructor to initialize the list
public MyLinkedList()
{
head=null;
size=0;
}
//return size of list
public int getSize()
{
return size;
}
//insert element at the front
public void insert(T data)
{
GenericNode<T>node=new GenericNode<T>(data,null);
if(head==null)head=node;
else
{
node.setNext(head);
head=node;
}
size++;
}
//check if element is present in list
public boolean contains(T e)
{
if(head==null)
return false;
else{
GenericNode<T>temp=head;
while(temp!=null)
{
if(temp.getData()==e)
return true;
temp=temp.getNext();
}
}
return false;
// returns the element at index or raise exception if index >=size
public T get(int index)
{
if(index>=size)
{
throw new IndexOutOfBoundsException();
}
else{
int i=0;
GenericNode<T>temp=head;
while(temp!=null)
{
if(i==index)
return
temp.getData();
temp=temp.getNext();
i++;
}
}
return(T)"-1";
}
//returns indexOf e in the list or -1 if not present
public int indexOf(T e)
{
int i=0;(temp!=null)
{
if(temp.getData()==e)
return i;
i++;temp=temp.getNext();
}
return-1;
}
//returns last index of e in list
public int lastIndexOf(T e)
{
int i=0;
int max=-1;
GenericNode<T<temp=head;
while(temp!=null)
{
if(temp.getData()==e)
max=i;
i++;temp=emp.getNext();
}
return max;
}
//set the elements in index to e or raise exception if index>=size
public void set(int index,T e)
{
if(index>=size)
throw newIndexOutOfBoundsException();
else
{
int i=0;
GenericNode<T>temp=head;
GenericNode<T>node=new GenericNode<T>(e,null);
while(temp.getNext()!=null)
{
if(i==index-1)
{
node.setNext(temp.getNext());
temp.setNext(node);
size++;
return;
}
i++;
temp=temp.getNext();
///temp.setNext(node);
}
}
public static void main(String[] args){
MyLinkedList<Integer>list=newMyLinkedList<integer>();
list.insert(1);//1
list.integer(5);//51
list.integer(3);//3 5 1
list.integer(5);// 5 3 5 1
System.out.println("Size:"+list.getSize());
System.out println("Does list contains 2:"+list.contains(2));
System.out println("Does list contains 5:"+list.contains(5));
System.out println("Get element at 2:"+list.get(2));
System.out println("Get element at 3:"+list.get(3));
System.out.println("index of 3:"+list.indexOf(3));
System.out.println("Last Index of 5:"+list.lastindexOf(5));
list.se(3,6);//5 3 5 6 1
System.out.println("Size:"+list.getSize());
System.out.println("Last index of 6 :"+list.lastIndexOf(6));
System.out.println("index of 1:"+list.lastIndexOf(1));
System.out.println("Size:+list.getSize());
}
}