patternjavaMinor
Double-ended linked list
Viewed 0 times
listdoublelinkedended
Problem
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(befor
```
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(befor
Solution
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
The lack of readability has a couple of reasons, many of which can be fixed really easy:
Usability
Your input gathering isn't very robust, which is a bit annoying:
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.
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
lis not an acceptable class name in general), and method names are written in camelCase (eginsertbefore->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 ofif (first == last)and switched statements (or at leastif (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.
Context
StackExchange Code Review Q#85781, answer score: 4
Revisions (0)
No revisions yet.