5
\$\begingroup\$

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');
}
}
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

Readability

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Apr 3, 2015 at 14:26
  • \$\begingroup\$ @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. \$\endgroup\$
    – tim
    Commented Apr 3, 2015 at 15:10
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Apr 3, 2015 at 15:38
  • \$\begingroup\$ @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. \$\endgroup\$
    – tim
    Commented Apr 3, 2015 at 15:48
  • \$\begingroup\$ @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. \$\endgroup\$
    – tim
    Commented Apr 3, 2015 at 15:50
1
\$\begingroup\$

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

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');
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Thanks! I will keep this as an standard the next time I post code! I thought optional is optional, just one statement, doesn't need braces. I will keep that in mind as well. \$\endgroup\$ Commented Apr 5, 2015 at 12:56
  • \$\begingroup\$ There are many syntactically valid constructions that aren't a good idea. This is one example why omitting braces is dangerous. On the other hand, here is one suggestion for mitigating the risk, but putting the body on the same line. I don't recommend that for you, since your code hygiene needs work. \$\endgroup\$ Commented Apr 5, 2015 at 13:03
  • \$\begingroup\$ Apart from formatting issues, does this code's logic work fine? \$\endgroup\$ Commented Apr 5, 2015 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.