将两个班级合并为一个

时间:2015-10-02 05:48:58

标签: java class linked-list circular-list

我想弄清楚是否有办法将我的两个班级合二为一。我的程序只是一个循环链表,我必须使用尽可能少的类。我还想帮助设置一个让我用另一个节点替换节点的方法。这是我的两个班级的想法吗?

第一个:

public class Node {

    static int numOfLists = 0;

    int data;
    Node nextNode;

    Node(int data) {
        this.data = data;
        numOfLists++;

    }
}

第二个:

public class LinkedList {

    // i = head
    // j = tail
    static Node i;
    static Node temp;
    static Node j;
    int num;
    Node nextNode;

    public void addNodes(int data) {
        Node node = new Node(data);
        if (j == null) {
            j = node;
            j.nextNode = j;
        } else {
            i = j;

            while (i.nextNode != j) {
                i = i.nextNode;
            }
            i.nextNode = node;
            node.nextNode = j;
        }
    }

    public void print() {
        i = j;
        boolean list = false;

        do {
            System.out.print((list) ? "--> |" + i.data + "|" : "|" + i.data
                + "|");
            list = true;
            i = i.nextNode;
        } while (i != j);
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.addNodes(3);
        list.addNodes(7);
        list.addNodes(2);
        list.addNodes(1);
        list.print();
    }
}

3 个答案:

答案 0 :(得分:3)

使用两个班级是正确的方法。 每个班级都有不同的,明确的责任, 这些应该是单独的类。 不要把它们混合在一起。 你当然可以,但那将是非常糟糕的OOP, 所以不要这样做。

但是,Node是链表的实现细节,实际上并不需要链表的用户可见。因此,将链接列表类中的Node作为静态内部类移动,并将其设置为私有。这样,根据封装和信息隐藏的良好原则,它将无法从外部看到。

像这样(我还清理了剩下的代码):

public class CircularLinkedList {

    private static class Node {
        private final int data;
        private Node nextNode;

        private Node(int data) {
            this.data = data;
        }
    }

    private Node head;

    public void addNodes(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            head.nextNode = head;
        } else {
            Node runner = head;

            while (runner.nextNode != head) {
                runner = runner.nextNode;
            }
            runner.nextNode = node;
            node.nextNode = head;
        }
    }

    public void print() {
        Node runner = head;
        boolean first = false;

        do {
            System.out.print((first) ? "--> |" + runner.data + "|" : "|" + runner.data + "|");
            first = true;
            runner = runner.nextNode;
        } while (runner != head);
    }
}

答案 1 :(得分:1)

您可以使用嵌套类,如:

public class LinkedList {


    static public class Node {
        static int numOfLists = 0;

        int data;
        Node nextNode;

        Node(int data) {
            this.data = data;
            numOfLists++;

        }
    }
}

最好使用静态嵌套类。常规类的数量减少了一个,但我们现在有一个嵌套类。

答案 2 :(得分:0)

如果要在添加新元素时跳过遍历整个列表,可以选择此选项。你仍然有两个引用,一个用于头部,一个用于尾部。如果遍历列表不是问题,那么使用@janos。

中的解决方案
  public class LinkedList
  {
    Node head;
    Node tail;

    public void addNodes(int data) {
      Node node = new Node(data);
      if (head == null) {
        head = node;
        tail = node;
      } else {
        tail.nextNode = node;
        tail = node;
      }
    }
    public void print() {

      boolean first = true;

      Node currentNode = head;
      while (currentNode != null) {
        System.out.print(!first ? "--> |" + currentNode.data + "|" : "|" + currentNode.data + "|");
        first = false;
        currentNode = currentNode.nextNode;
      }
    }


    private static class Node
    {
      int numOfLists = 0;

      int data;
      Node nextNode;

      Node(int data) {
        this.data = data;
        numOfLists++;

      }
    }
   }