HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Doubly-Linked Circular Linked List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
listcirculardoublylinked

Problem

I'm a C++ programmer learning Java in preparation for an OOP class this fall. That being said, Java is fairly new to me so I'd love to hear feedback on how to improve this code.

A few notes:

  • Code works as expected and is ported from my C++ equivalent code I wrote some time ago.



  • Not all operations that should be implemented have been implemented yet (Search, Remove, etc). Of course, that would be more fitting for BSTs or other data structures for efficiency's sake.



node.java

public class node {
    public node() {
        this.setNext(null);
        this.setPrev(null);
    }

    public node(T k) {
        this.setNext(null);
        this.setPrev(null);
        this.setKey(k);
    }

    public node getNext() {
        return next;
    }

    public void setNext(node next) {
        this.next = next;
    }

    public node getPrev() {
        return prev;
    }

    public void setPrev(node prev) {
        this.prev = prev;
    }

    public T getKey() {
        return key;
    }

    public void setKey(T key) {
        this.key = key;
    }

    private node next;
    private node prev;
    private T key;
}


List.java

```
public class list {
public list() {
head = null;
}

/* Description: Deletes the head node from the linked list and returns the head node's key.
* Running Time: O(1)
* Precondition: Linked list must not be null
Postcondition: The head node is deleted and its key is returned. /
public T pop() {
if(isEmpty()) return null;

T k = head.getKey();

if(head == head.getNext()) {
head = null;
}
else {
node f = head, b = head.getPrev();
b.setNext(f.getNext());
head = head.getNext();
head.setPrev(b);
}

return k;
}

/* Description: Deletes the tail node from the linked list and returns its node's key.
* Running Time: O(1)
* Precondition: Linked list must not be null

Solution

Java style convention

One of the first thing you should do when working in a new language is to look at style convention. You used C++ convention and not the Java one. Class name should be PascalCase and method name should be camelCase. Unless it's a constant, you should not use _ (except in some precise case).

if(isEmpty()) {
        ...
    }
    else {
        ...
    }


Should be :

if(isEmpty()) {
        ...
    } else {
        ...
    }


The else should be on the same line that the last }.
Variable declaration

I always like to start the class with class and instance variables. It will let me know what my class is using. So private node head; should be before the constructor.

I would suggest that you avoid using single letters as a variable name. Reading x and p does not help to know what it's used for. Try to use descriptive name, there is no or almost no length limitation, so be creative!
Documentation

I see that you have good comments for your public methods. I would suggest that you use JavaDoc. It will almost change nothing, just some syntax changes. Here is one example of a JavaDoc :

/**
 * Here is the description of what the method is doing. Some specific things that will      
 * be helpful to the caller like {@link OtherClass#methodName()}
 * @param aParam What your param is or used for in your method
 * @return what your method is returning
 * @see OtherClass
 */


As @tot2 pointed in comments, you can use tools that will generate html from your Javadoc. If you check the Java 7 html documentation, it's all been built with it. It's useful if you use an IDE as it can be easily accessible when you code. In Eclipse, if you put your mouse hover a method/class, it will show you the Javadoc in a well presented way. If you want more information I would suggest you visit this Oracle page.
Bracket

This is a personal choice, but I would suggest to always use brackets even if you only have one line and it's possible to omit brackets.

if(isEmpty()) return;
else if(p == p.getNext()) {
    System.out.println(p.getKey());
}


if(isEmpty()) {
  return;
} else if(p == p.getNext()) {
    System.out.println(p.getKey());
}


This will "save" you time if you need to add a new line of code in the if part. It will also prevent so weird bug if someone try to add a line, but forget that there was no brackets.

Code Snippets

if(isEmpty()) {
        ...
    }
    else {
        ...
    }
if(isEmpty()) {
        ...
    } else {
        ...
    }
/**
 * Here is the description of what the method is doing. Some specific things that will      
 * be helpful to the caller like {@link OtherClass#methodName()}
 * @param aParam What your param is or used for in your method
 * @return what your method is returning
 * @see OtherClass
 */
if(isEmpty()) return;
else if(p == p.getNext()) {
    System.out.println(p.getKey());
}
if(isEmpty()) {
  return;
} else if(p == p.getNext()) {
    System.out.println(p.getKey());
}

Context

StackExchange Code Review Q#55899, answer score: 10

Revisions (0)

No revisions yet.