Here is a Source Code for Linked List In Java.
import java.io.*;
class Node
{
Student data;
Node link;
Node()
{
data=null;
link=null;
}
public Node(Student data)
{
this.data=data;
link=null;
}
}
class LinkedList
{
Node head,newNode;
LinkedList()
{
head=null;
}
public void initList()
{
head=null;
}
public Node createNode(Student data)
{
Node node = new Node(data);
return node;
}
public int size()
{
int count=0;
Node p;
if(head==null)
{
return 0;
}
else
{
p=head;
while(p!=null)
{
count++;
p=p.link;
}
}
return count;
}
public void beforeX(Node newNode,Node x)
{
Node p=head;
while(p!=x.link)
{
p=p.link;
}
newNode.link=p;
x.link=newNode;
}
public void afterX(Node newNode,Node x)
{
Node p=head;
while(p!=x.link)
{
p=p.link;
}
newNode.link=p;
x.link=newNode;
}
public void insertAsFirst(Node newNode)
{
newNode.link=head;
head=newNode;
}
public void insertAsLast(Node newNode)
{
Node p = head;
while(p.link!=null)
p=p.link;
p.link=newNode;
}
public void relativeToX(Node newNode,int rel,Node x)
{
switch(rel)
{
case 1 : beforeX(newNode,x);
break;
case 2 : afterX(newNode,x);
break;
default: System.out.print("\n\nInvalid Option..... Check Syntax of relativeToX()");
}
}
public void insert(Node newNode,int pos,int reltox,Node x)
{
if(head==null)
{
head=newNode;
}
else
{
switch(pos)
{
case 0 : relativeToX(newNode,reltox,x);
break;
case 1 : insertAsFirst(newNode);
break;
case 2 : insertAsLast(newNode);
break;
}
}
}
public Node deleteAny(Node x)
{
Node p=head;
Node tmp;
tmp=x;
if(x==head)
{
head=x.link;
return tmp;
}
else
{
while(p.link!=x)
{
p=p.link;
}
p.link=x.link;
return tmp;
}
}
public Node deleteFirst()
{
Node tmp;
tmp=head;
head=head.link;
return tmp;
}
public Node deleteLast()
{
System.out.print("\n\n........Under Development.........");
return null;
}
public Node delete(int opt,Node x)
{
Node p=head;
Node tmp=null;
if(p==null)
{
System.out.print("\n\n!!!!!! LIST DOES NOT EXIST !!!!!!");
}
else
{
switch(opt)
{
case 1 : tmp=deleteFirst();
break;
case 2 : tmp=deleteLast();
break;
case 3 : tmp=deleteAny(x);
break;
default: System.out.print("\n\nDelete !!-- Wrong Option");
}
}
return tmp;
}
public Node search(int admno)
{
Node p = head;
Student s=null;
if(p==null)
System.out.print("\n\n!!!!!! LIST DOES NOT EXIST !!!!!!");
else
{
while(p!=null)
{
if(p.data.admno==admno)
return p;
p=p.link;
}
}
return null;
}
public void display()
{
Node p=head;
if(p==null)
System.out.print("\n\n!!!!!! LIST DOES NOT EXIST !!!!!!");
while(p!=null)
{
p.data.printData();
p=p.link;
}
}
}
class Student
{
public int admno;
private String name;
Student()
{
this.admno=0;
this.name="";
}
Student(int admno,String name)
{
this.admno=admno;
this.name=name;
}
public int getAdmno()
{
return admno;
}
public void printData()
{
System.out.print("\n\nAdmission Number : "+admno);
System.out.print("\n\nName : "+name);
}
}
class MyStudentListH
{
public static LinkedList ll=new LinkedList();
public static void deleteSubMenu()
{
int choice=0;
while(true)
{
System.out.print("\n\t\t\n\n\tDELETE\n\n");
System.out.print("\n\t1.FIRST\t\t:");
System.out.print("\n\t2.LAST\t\t:");
System.out.print("\n\t3.ANY NODE\t:");
System.out.print("\n\t4.MAIN MENU\t:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("\n\nEnter Your Choice : ");
choice=Integer.parseInt(br.readLine());
}
catch(Exception ex)
{
System.out.print("\nInput - Output Error !!!");
System.exit(1);
}
switch(choice)
{
case 1 : ll.delete(1,null);
break;
case 2 : ll.delete(2,null);
break;
case 3 : Node tmp=search();
if(tmp!=null)
{
ll.delete(3,tmp);
}
break;
case 4 : menu();
break;
default: System.out.print("\n\nInvalid Menu Option !!!!!!!!!");
}
}
}
public static void insertSubMenu()
{
int choice=0;
Student s=null;
Node newNode=null;
Node tmp;
while(true)
{
System.out.print("\n\t\t\n\n\tINSERT\n\n");
System.out.print("\n\t1.FIRST\t\t:");
System.out.print("\n\t2.LAST\t\t:");
System.out.print("\n\t3.BEFORE NODE X\t:");
System.out.print("\n\t4.AFTER NODE X\t:");
System.out.print("\n\t5.MAIN MENU\t:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("\n\nEnter Your Choice : ");
choice=Integer.parseInt(br.readLine());
if(choice!=5)
{
System.out.print("\n\n---Data to new Record-----");
System.out.print("\n\nEnter The Admission Number : ");
int admno=Integer.parseInt(br.readLine());
System.out.print("\n\nEnter Name : ");
String name = br.readLine();
s=new Student(admno,name);
newNode=ll.createNode(s);
}
}
catch(Exception ex)
{
System.out.print("\nInput - Output Error !!!");
System.exit(1);
}
switch(choice)
{
case 1 : System.out.print("\n\nInsert as First Node ");
ll.insert(newNode,1,0,null);
break;
case 2 : System.out.print("\n\nInsert as Last Node ");
ll.insert(newNode,2,0,null);
break;
case 3 : System.out.print("\n\nInsert before Node X ");
tmp=search();
if(tmp!=null)
{
ll.insert(newNode,0,1,tmp);
}
break;
case 4 : System.out.print("\n\nInsert after Node X ");
tmp=search();
if(tmp!=null)
{
ll.insert(newNode,0,2,tmp);
}
break;
case 5 : System.out.print("\n\nTo Main Menu ");
menu();
break;
default: System.out.print("\n\nInvalid menu option !!!!!");
}
}
}
public static Node search()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int admno=0;
Node res;
try
{
System.out.print("\n\n----Search Data-----");
System.out.print("\n\nEnter Admission number : ");
admno=Integer.parseInt(br.readLine());
}
catch(Exception ex)
{
System.out.print("\nInput - Output Error !!!");
System.exit(1);
}
res=ll.search(admno);
if(res==null)
{
System.out.print("\n\n--->Student Record Not Found");
}
else
{
System.out.print("\n\n--->Student Record Found");
res.data.printData();
}
return res;
}
public static void menu()
{
int choice=0;
while(true)
{
System.out.print("\n\t\t\n\n\tLINKED LIST\n");
System.out.print("\n\t1.INSERT\t:");
System.out.print("\n\t2.DELETE\t:");
System.out.print("\n\t3.DISPLAY\t:");
System.out.print("\n\t4.SEARCH\t:");
System.out.print("\n\t5.INITIALIZE\t:");
System.out.print("\n\t6.SIZE\t\t:");
System.out.print("\n\t7.EXIT\t\t:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("\n\nEnter Yor Choice : ");
choice=Integer.parseInt(br.readLine());
}
catch(Exception ex)
{
System.out.print("\nInput - Output Error !!!");
System.exit(1);
}
switch(choice)
{
case 1 : insertSubMenu();
break;
case 2 : deleteSubMenu();
break;
case 3 : ll.display();
break;
case 4 : Node tmp=search();
break;
case 5 : ll.initList();
System.out.print("\n\nList is Initialized");
break;
case 6 : System.out.print("\n\nNumber of items in the List : "+ll.size());
break;
case 7 : System.out.print("\nGOOD BYE.....\n\n");
System.exit(1);
default: System.out.print("\n\nInvalid menu Option !!!!!");
}
}
}
public static void main(String[] arg)
{
int n,i;
String name;
menu();
}
}
Loading...
|
Comments :
Post a Comment