You are on page 1of 4

class Node

{
int data;Node next;
}
////////End of Node Class////
class LinkedList
{
Node Head,Tail;
public void insert(int val)
{
Node New=new Node();
New.data = val;
New.next = null;
if(Head==null)
{
Head = New ; Tail = New;
}
else
{
Node N = Head;
while(N.next != null)
{ N = N.next; }
Tail.next = New; Tail = New;
}
} /// 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 Node' data is "+ Tmp.data);
}
}//////End of Delete( )////////
public boolean contains(int search)
{
if(Head == null) { return false; }
Node Cur = Head; //Cur = Current
while(Cur != null)
{
if(Cur.data == search) { return true; }
Cur = Cur.next;
}
return false;
}//////End of contains()/////////
public void show()
{
Node N= Head;
if(N==null) { System.out.println("Empty") ;}
else {while(N.next != null)
{
System.out.print("Node=>"+ N.data + " ");
N = N.next;
}

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


}////////End of show()////////

public void reverse() {


Node Prev,Curr,Next;
Prev = null; Curr = Next = Head;
while(Next != null)
{
Next = Next.next;
Curr.next = Prev;
Prev = Curr;
Curr = Next;
}
Head = Prev;
}
}
//////End of LinkedList Class/////////////
public class TestLinkedList
{
public static void main(String[] args)
{
LinkedList List=new LinkedList();
List.insert(3);
List.insert(63);
List.insert(60);
List.insert(12);
List.show();
List.deleteAt(2);
List.show();
List.reverse();
List.show();
boolean res=List.contains(3);
if(res) { System.out.println("Node is Found");}
else { System.out.println("Node is Not Found");}
}
}

Output = >
Node=>3 Node=>63 Node=>60 Node 12
Delete Node' data is 60
Node=>3 Node=>63 Node 12
Node=>12 Node=>63 Node 3
Node is Found
Example 2
class Student
{
int rno;String name;int mark;Student next;
}
////////End of Node Class////
class StuLinkedList
{
Student head,tail;
public void insert(int r,int m,String n)
{
Student New=new Student();
New.rno = r;
New.mark=m;
New.name=n;
New.next = null;
if(head==null)
{
head = New ; tail = New;
}
else
{
Student S = head;
while(S.next != null)
{ S = S.next; }
tail.next = New; tail = New;
}
} /// End of insert ()

public void deleteAt(int index)


{
if ( index == 0)
{ head = head.next; }
else
{
Student S = head; Student Tmp = null;
for(int i = 0; i<index-1; i++)
{
S = S.next;
}
Tmp = S.next;
S.next = Tmp.next;
System.out.println("Removed Student is "+ Tmp.rno);
}
}//////End of Delete( )////////
public boolean contains(int search)
{
if(head == null) { return false; }
Student Cur = head; // Cure = Current
while(Cur != null)
{
if(Cur.rno == search)
{ System.out.println("Rno "+Cur.rno + " " +Cur.name +" is
Found.");
return true;
}
Cur = Cur.next;
}
return false;
}//////End of contains()/////////
public void show()
{
Student S= head;
if(S==null) { System.out.println("Empty") ;}
System.out.println("Roll no Name Mark ");
while(S.next != null)
{
System.out.println(S.rno + "\t "+S.name+"\t
"+S.mark);
S = S.next;
}

System.out.println(S.rno + "\t "+S.name+"\t "+S.mark);


}////////End of show()////////
}
//////End of LinkedList Class/////////////
public class StudentLinkedList
{
public static void main(String[] args)
{
StuLinkedList list=new StuLinkedList();
list.insert(1,550,"Ya Min");
list.insert(2,570,"ChitSu");
list.insert(3,530,"SaPal");
list.show();
list.deleteAt(2);
list.show();
boolean res=list.contains(1);
if(res) { System.out.println("Student is Found");}
else { System.out.println("Student is Not Found");}
}
}

Output =>
Roll no Name Mark
1 Ya Min 550
2 ChitSu 570
3 SaPal 530
Removed Student is 3
Roll no Name Mark
1 Ya Min 550
2 ChitSu 570
Rno 1 Ya Min is Found.
Student is Found

You might also like