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;
}

}

data d;

d=new data(a,b,c);
}

d.displaydata();
}

}

public void insertfirst(String a,int b,int c){
if(isEmpty())
else{
}
}
public void insertlast(String a,int b,int c){
if(isEmpty())
else{
}
}
if(first==last)
first=last=null;
else
first=first.next;
return temp;
}
public boolean isEmpty(){
return first==null&&last==null;
}
return first;
}
public void displayList(){
while(current!=null)
{
current=current.next;
}
}
prev=current=first;
while(current.next!=null)
{
prev=current;
current=current.next;
}
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){
if(before==first){
}
else{
}
}
public void insertafter(Link after,String a,int b,int c){
}
if(!isEmpty()){
while(!(current.d.retname()).equals(s))
if(current.next!=null)
current=current.next;
else{
current=current.next;break;
}
}
return current;
}

if(!isEmpty()){
while(!(current.d.retname()).equals(s))
if(current.next!=null)
current=current.next;
else{
current=current.next;break;
}
}
return current;
}
prev=current;
while(!(current.d.retname()).equals(s)){
prev=current;
current=current.next;
}
return prev;
}
if(x==first)
return null;
else{
while(current.next!=x)
current=current.next;
return current;
}
}
public void displayLast(){
while(m!=null&&m.next!=first){
m=findprev(m);
}
}
if(!isEmpty()){
while(!(current.d.retname().equals(s))){
if(current.next!=null)
current=current.next;
else{
current=current.next;break;
}
}
return current;
}
return null;
}
if(!isEmpty()){
if(m!=null){
if(m==first&&m==last)
first=last=null;
else if(m==first)
first=first.next;
else if(m==last){
z.next=null;last=z;
}
else{
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;
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");
if(ch>=1||ch<=10){
switch(ch)
{
case 1:System.out.println("\nINSERT FIRST\n"+"\nEnter the number of items to insert first\n");
for( i=0;i<mm;i++){
if(i==0)
System.out.println("Enter Name Roll and Age for the students :");
}
break;
case 2:System.out.println("\nINSERT LAST\n"+"\nEnter the number of items to insert last\n");
for(i=0;i<mm;i++){
if(i==0)
System.out.println("Enter Name Roll and Age for the students :");
}
break;
case 3:System.out.println("\nDELETE FIRST\n");
System.out.println("Enter the number of items to delete");
i=0;
while(i<l&&!ll.isEmpty()){
System.out.println("DELETED FIRST");
if(m!=null)
i++;
}
break;
case 4:System.out.println("\nDELETE LAST\n");
System.out.println("Enter the number of items to delete");
i=0;
while(i<l&&!ll.isEmpty()){
System.out.println("DELETED LAST");
if(m!=null)
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()){
if(z!=null)
else
}
else
System.out.println("List is empty");
break;
case 7:System.out.println("Insert before a name");
if(!ll.isEmpty()){
if(z!=null)
else
}
else
System.out.println("LIST EMPTY");
break;
case 8:ll.displayLast();
break;
case 9:System.out.println("Enter the name to delete");
if(z!=null){
System.out.println("DELETED");
}
else
break;
case 10:System.out.println("Search a link by name");
if(z!=null){
System.out.println("FOUND");
}
else
break;
}
}
System.out.println("Again? Y/y");
}while(s.charAt(0)=='Y'||s.charAt(0)=='y');
}
}


Your code is extremely hard to read, which makes it hard to concentrate a review on the specific points you want reviewed (although I think that the general structure of your linked list is actually alright. I would probably put data and link together in one class, but other than that it seems fine). When writing code, try to think about the people who are going to read it (this might include you in the future).

The lack of readability has a couple of reasons, many of which can be fixed really easy:

• your indentation is completely random. This makes it extremely hard to see where what blocks end (use any IDE to fix this, eg in Netbeans, highlight your code and press ctrl + alt + f).
• your variables names are often very short. eg a, b, c, d, x, s, m, l, ll, ch, mm, z. None of those are acceptable variable names.
• try to follow general naming conventions: class names start with an upper-case character (and l is not an acceptable class name in general), and method names are written in camelCase (eg insertbefore -> insertBefore.
• your spacing is off (you have too few spaces, which makes your code too dense). This can also be fixed with any IDE.
• you often put more than one statement on a line, eg newLink.next=before;m.next=newLink;. This makes it hard to read, and easy to overlook a statement.
• there is a lot of duplication in your main method, try to extract that to a method.
• don't declare multiple variables on one line (it makes it easy to overlook one).
• declare fields private.
• you often go for the negative check instead of the more straight-forward check (eg if (!(first == last)) instead of if (first == last) and switched statements (or at least if (first != last).
• don't call people names in your code.

Usability

Your input gathering isn't very robust, which is a bit annoying:

• if I enter a string instead of a number, the program quits, instead of informing me that I entered something wrong.
• if I just press enter when asked the somewhat confusing question Again? Y/y, the program also quits.
• if I just press enter or enter something wrong at the beginning, the program also just quits.

I think you get the idea. It would be better if you would catch those exceptions, report the problem to the user, and ask for input again.

• Ok, I will be rectifying those but right now my concerns are the actual functions that I have implemented in the LinkList class. Are they doing what they are intended to to or shall I add or remove something from those? Apr 3 '15 at 14:26
• @user4275686 I assumed that they would work (non-working code is off-topic here). In general, if your code is more readable, it's a lot easier to see bugs (and more people will actually read your code instead of just skimming it), so I think my answer is still quite relevant. Also, you might want to look into unit tests, which are a great tool to test the correctness of your code.
– tim
Apr 3 '15 at 15:10
• Are there any standard references where I can look into how to format codes properly as well as how to check for obvious errors? I am new to programming, so what are unit tests? Apr 3 '15 at 15:38
• @user4275686 this java code convention is a little dated, but basically still valid. But it might be a bit boring. I would recommend that you install an IDE (either NetBeans or Eclipse). This has two advantages: It formats the code for you (so you can see what properly formated code looks like), and they contain basic code quality checking (on every line where you violate basic principles, there will be a little icon next to it). You can also install additional code quality tools such as checkstyle, pmd, and findbugs.
– tim
Apr 3 '15 at 15:48
• @user4275686 unit tests are automated tests to test the correctness of methods (as in, they can be run automatically, you have to write the tests themselves). In Java, JUnit is most commonly used, and is also integrated in NetBeans (and Eclipse, I think), which makes it really easy to set up and run tests.
– tim
Apr 3 '15 at 15:50

Your haphazard indentation makes the code basically unintelligible. I've reformatted all of your code below, changing nothing but indentation, line spacing, and splitting multiple statements on a single line into multiple lines.

Furthermore, it is considered dangerously bad practice to omit optional braces.

import java.io.*;

class {
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;
}
}

data d;

public Link(String a,int b,int c) {
d=new data(a,b,c);
}

d.displaydata();
}
}

public void insertfirst(String a,int b,int c) {
if(isEmpty())
else {
}
}

public void insertlast(String a,int b,int c) {
if(isEmpty())
else {
}
}

if(first==last)
first=last=null;
else
first=first.next;
return temp;
}

public boolean isEmpty() {
return first==null&&last==null;
}

return first;
}

public void displayList() {
while(current!=null) {
current=current.next;
}
}

prev=current=first;
while(current.next!=null) {
prev=current;
current=current.next;
}
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) {
if(before==first) {
}
else {
}
}

public void insertafter(Link after,String a,int b,int c) {
}

if(!isEmpty()) {
while(!(current.d.retname()).equals(s))
if(current.next!=null)
current=current.next;
else {
current=current.next;
break;
}
}
return current;
}

if(!isEmpty()) {
while(!(current.d.retname()).equals(s))
if(current.next!=null)
current=current.next;
else {
current=current.next;
break;
}
}
return current;
}

prev=current;
while(!(current.d.retname()).equals(s)) {
prev=current;
current=current.next;
}
return prev;
}

if(x==first)
return null;
else {
while(current.next!=x)
current=current.next;
return current;
}
}

public void displayLast() {
while(m!=null&&m.next!=first) {
m=findprev(m);
}
}

if(!isEmpty()) {
while(!(current.d.retname().equals(s))) {
if(current.next!=null)
current=current.next;
else {
current=current.next;
break;
}
}
return current;
}
return null;
}

if(!isEmpty()) {
if(m!=null) {
if(m==first&&m==last)
first=last=null;
else if(m==first)
first=first.next;
else if(m==last) {
z.next=null;
last=z;
}
else {
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;
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");
if(ch>=1||ch<=10) {
switch(ch) {
case 1:
System.out.println("\nINSERT FIRST\n"+"\nEnter the number of items to insert first\n");
for( i=0;i<mm;i++) {
if(i==0)
System.out.println("Enter Name Roll and Age for the students :");
}
break;
case 2:
System.out.println("\nINSERT LAST\n"+"\nEnter the number of items to insert last\n");
for(i=0;i<mm;i++) {
if(i==0)
System.out.println("Enter Name Roll and Age for the students :");
}
break;
case 3:
System.out.println("\nDELETE FIRST\n");
System.out.println("Enter the number of items to delete");
i=0;
while(i<l&&!ll.isEmpty()) {
System.out.println("DELETED FIRST");
if(m!=null)
i++;
}
break;
case 4:
System.out.println("\nDELETE LAST\n");
System.out.println("Enter the number of items to delete");
i=0;
while(i<l&&!ll.isEmpty()) {
System.out.println("DELETED LAST");
if(m!=null)
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()) {
if(z!=null)
else
}
else
System.out.println("List is empty");
break;
case 7:
System.out.println("Insert before a name");
if(!ll.isEmpty()) {
if(z!=null)
else
}
else
System.out.println("LIST EMPTY");
break;
case 8:
ll.displayLast();
break;
case 9:
System.out.println("Enter the name to delete");
if(z!=null) {
System.out.println("DELETED");
}
else
break;
case 10:
if(z!=null) {
System.out.println("FOUND");
}
else