I want to know if there is any redundancy in the code or if it is missing something. I ran it with several different choices and it works fine but I still want to know are there any more subtle implementations in parts or in full.
import java.io.*;
class data{
String name;
int roll;
int age;
public data(String a,int b,int c){
name=a;roll=b;age=c;
}
public void displaydata(){
System.out.println("Name : "+name+" Roll : "+roll+" Age : "+age);
}
public String retname(){
return name;
}
}
class Link{
data d;
Link next;
public Link(String a,int b,int c){
d=new data(a,b,c);
}
public void displayLink(){
d.displaydata();
}
}
class LinkList{
Link first,last;
public void insertfirst(String a,int b,int c){
Link newLink=new Link(a,b,c);
if(isEmpty())
last=first=newLink;
else{
newLink.next=first;first=newLink;
}
}
public void insertlast(String a,int b,int c){
Link newLink=new Link(a,b,c);
if(isEmpty())
first=last=newLink;
else{
last.next=newLink;last=newLink;
}
}
public Link deletefirst(){
Link temp=first;
if(first==last)
first=last=null;
else
first=first.next;
return temp;
}
public boolean isEmpty(){
return first==null&&last==null;
}
public Link seefirst(){
return first;
}
public void displayList(){
Link current=first;
while(current!=null)
{
current.displayLink();
current=current.next;
}
}
public Link deletelast(){
Link current,prev;
prev=current=first;
while(current.next!=null)
{
prev=current;
current=current.next;
}
Link temp=current;
if(!(first==last)){
last=prev;last.next=null;
}
else
first=last=null;
return temp;
}
public void insertbefore(Link before,String a,int b,int c){
Link newLink=new Link(a,b,c);
Link m=findprevbefore(before.d.retname());
if(before==first){
newLink.next=first;first=newLink;
}
else{
newLink.next=before;m.next=newLink;
}
}
public void insertafter(Link after,String a,int b,int c){
Link newLink=new Link(a,b,c);
newLink.next=after.next;
after.next=newLink;
}
public Link findafter(String s){
Link current=first;
if(!isEmpty()){
while(!(current.d.retname()).equals(s))
if(current.next!=null)
current=current.next;
else{
current=current.next;break;
}
}
return current;
}
public Link findbefore(String s){
Link current=first;
if(!isEmpty()){
while(!(current.d.retname()).equals(s))
if(current.next!=null)
current=current.next;
else{
current=current.next;break;
}
}
return current;
}
public Link findprevbefore(String s){
Link prev,current=first;
prev=current;
while(!(current.d.retname()).equals(s)){
prev=current;
current=current.next;
}
return prev;
}
public Link findprev(Link x){
Link current=first;
if(x==first)
return null;
else{
while(current.next!=x)
current=current.next;
return current;
}
}
public void displayLast(){
Link m=last;
while(m!=null&&m.next!=first){
m.displayLink();
m=findprev(m);
}
}
public Link find(String s){
Link current=first;
if(!isEmpty()){
while(!(current.d.retname().equals(s))){
if(current.next!=null)
current=current.next;
else{
current=current.next;break;
}
}
return current;
}
return null;
}
public Link deleteLink(String x){
if(!isEmpty()){
Link m=find(x);
if(m!=null){
if(m==first&&m==last)
first=last=null;
else if(m==first)
first=first.next;
else if(m==last){
Link z=findprevbefore(x);
z.next=null;last=z;
}
else{
Link z=findprevbefore(x);
z.next=m.next;
}
}
return m;
}
return null;
}
}
class l{
public static void main(String [] args)throws IOException{
DataInputStream wow=new DataInputStream(System.in);
String s;int mm,l,i=0;
Link z;
LinkList ll=new LinkList();
do{
System.out.println("\n 1.INSERT FIRST \n 2.INSERT LAST \n 3.DELETE FIRST \n 4.DELETE LAST \n 5.DISPLAY FIRST \n 6.INSERT AFTER \n 7.INSERT BEFORE \n 8.DISPLAY LAST \n 9.DELETE LINK \n 10.FIND LINK BY NAME \n"+"\n"+"\nEnter a choice bitch!\n");
int ch=Integer.parseInt(wow.readLine());
if(ch>=1||ch<=10){
switch(ch)
{
case 1:System.out.println("\nINSERT FIRST\n"+"\nEnter the number of items to insert first\n");
mm=Integer.parseInt(wow.readLine());
for( i=0;i<mm;i++){
if(i==0)
System.out.println("Enter Name Roll and Age for the students :");
ll.insertfirst(wow.readLine(),Integer.parseInt(wow.readLine()),Integer.parseInt(wow.readLine()));
}
break;
case 2:System.out.println("\nINSERT LAST\n"+"\nEnter the number of items to insert last\n");
mm=Integer.parseInt(wow.readLine());
for(i=0;i<mm;i++){
if(i==0)
System.out.println("Enter Name Roll and Age for the students :");
ll.insertlast(wow.readLine(),Integer.parseInt(wow.readLine()),Integer.parseInt(wow.readLine()));
}
break;
case 3:System.out.println("\nDELETE FIRST\n");
System.out.println("Enter the number of items to delete");
l=Integer.parseInt(wow.readLine());
i=0;
while(i<l&&!ll.isEmpty()){
System.out.println("DELETED FIRST");
Link m=ll.deletefirst();
if(m!=null)
m.displayLink();
i++;
}
break;
case 4:System.out.println("\nDELETE LAST\n");
System.out.println("Enter the number of items to delete");
l=Integer.parseInt(wow.readLine());
i=0;
while(i<l&&!ll.isEmpty()){
System.out.println("DELETED LAST");
Link m=ll.deletelast();
if(m!=null)
m.displayLink();
i++;
}
break;
case 5:System.out.println("\nDISPLAY LIST\n");
System.out.println("\nList\n");
if(!ll.isEmpty())
ll.displayList();
else
System.out.println("LIST EMPTY");
break;
case 6: System.out.println("Insert after a name");
if(!ll.isEmpty()){
z=ll.findafter(wow.readLine());
if(z!=null)
ll.insertafter(z,wow.readLine(),Integer.parseInt(wow.readLine()),Integer.parseInt(wow.readLine()));
else
System.out.println("NOT FOUND");
}
else
System.out.println("List is empty");
break;
case 7:System.out.println("Insert before a name");
if(!ll.isEmpty()){
z=ll.findbefore(wow.readLine());
if(z!=null)
ll.insertbefore(z,wow.readLine(),Integer.parseInt(wow.readLine()),Integer.parseInt(wow.readLine()));
else
System.out.println("NOT FOUND");
}
else
System.out.println("LIST EMPTY");
break;
case 8:ll.displayLast();
break;
case 9:System.out.println("Enter the name to delete");
z=ll.deleteLink(wow.readLine());
if(z!=null){
System.out.println("DELETED");
z.displayLink();
}
else
System.out.println("NOT FOUND");
break;
case 10:System.out.println("Search a link by name");
z=ll.find(wow.readLine());
if(z!=null){
System.out.println("FOUND");
z.displayLink();
}
else
System.out.println("NOT FOUND");
break;
}
}
System.out.println("Again? Y/y");
s=wow.readLine();
}while(s.charAt(0)=='Y'||s.charAt(0)=='y');
}
}