You are on page 1of 2

Test Linked List

class Node
{
int data;Node next;
}
////////End of Node Class////
class LinkedList
{
Node head,tail;
public void insert(int data)
{
Node node=new Node();
node.data = data;
node.next = null;
if(head==null)
{
head = node ; tail = node;
}
else
{
Node n = head;
while(n.next != null)
{ n = n.next; }
tail.next = node; tail = node;
}
} /// End of insert ()

public void deleteAt(int index)


{
if ( index == 0)
{ head = head.next; }
else
{
Node n = head; Node tmp = null;
for(int i = 0; i<index-1; i++)
{
n = n.next;
}
tmp = n.next;
n.next = tmp.next;
System.out.println("Delete List is "+ tmp.data);
}
}//////End of Delete( )////////
public boolean contains(int search)
{
if(head == null) { return false; }
Node current = head;
while(current != null)
{
if(current.data == search) { return true; }
current = current.next;
}
return false;
}//////End of contains()/////////
public void show()
{
Node node= head;
if(node==null) { System.out.println("Empty") ;}
else {while(node.next != null)
{
System.out.print("Node=>"+node.data + " ");
node = node.next;
}

System.out.println("Node "+node.data + " "); }


}////////End of show()////////
}
//////End of LinkedList Class/////////////
public class TestLinkedList
{
public static void main(String[] args)
{
LinkedList list=new LinkedList();
list.insert(3);
list.insert(6);
list.insert(60);
list.insert(16);
list.show();
list.deleteAt(2);
list.show();
boolean res=list.contains(3);
if(res) { System.out.println("Node is Found");}
else { System.out.println("Node is Not Found");}
}
}

OUTPUT is

Node=>3 Node=>6 Node=>60 Node 16


Delete List is 60
Node=>3 Node=>6 Node 16
Node is Found

You might also like