多链接列表实施

时间:2015-12-11 05:55:46

标签: java list data-structures nodes

我正在尝试在Java中为项目实现多链接列表。

package project1;  
import java.lang.IllegalArgumentException;
import project1.MLLNode;

class MLList {
    private int nbSets;
    private MLLNode head;

    public MLList(int _nbSets) {
        if (_nbSets <= 0)
            throw(new IllegalArgumentException("- Illegal Number of Sets (" +
      _nbSets + ")!"));
        nbSets = _nbSets;
        head = MLLNode.create(0,null);
    }

    public boolean isEmpty(MLLNode.LType ltype) {
        return(head.getNext(ltype) == null);
    }

    public void display(MLLNode.LType ltype) {
       System.out.print("[ ");
        MLLNode node = head.getNext(ltype);
        while(node != null) {
            node.display();
            System.out.print(" ");
            node = node.getNext(ltype);
        }
        System.out.print("]");
    }


    public boolean insert(int id, Object data) {
        MLLNode newnode = MLLNode.create(id, data);
        if(isEmpty(MLLNode.LType.ALL)){
            head.setNext(MLLNode.LType.ALL, newnode);
            newnode.setNext(MLLNode.LType.ALL, null);
        }else{
            MLLNode first = head.getNext(MLLNode.LType.ALL);
            head.setNext(MLLNode.LType.ALL, newnode);
            newnode.setNext(MLLNode.LType.ALL, first);
        }
        if(isEmpty(MLLNode.LType.XOR)){
            head.setNext(MLLNode.LType.XOR, newnode);
            newnode.setNext(MLLNode.LType.XOR, null);
        }else{
            MLLNode first = head.getNext(MLLNode.LType.XOR);
            head.setNext(MLLNode.LType.XOR, newnode);
            newnode.setNext(MLLNode.LType.XOR, first);
        }
        if(isEmpty(MLLNode.LType.ANY)){
            head.setNext(MLLNode.LType.ANY, newnode);
            newnode.setNext(MLLNode.LType.ANY, null);
        }else{
            MLLNode first = head.getNext(MLLNode.LType.ANY);
            head.setNext(MLLNode.LType.ANY, newnode);
            newnode.setNext(MLLNode.LType.ANY, first);
         }
        return true;
    }
}

public Object delete(int id) {
        if (!isEmpty(MLLNode.LType.ALL)){
            MLLNode previous = head;
            MLLNode current = head.getNext(MLLNode.LType.ALL);
            int obj_id = 0;
            while(obj_id != id && current != null){
                previous = current;
                current = current.getNext(MLLNode.LType.ALL);
                obj_id++;
            }
            if (current != null){
                previous.setNext(MLLNode.LType.ALL, current.getNext(MLLNode.LType.ALL));
            }else{
                previous.setNext(MLLNode.LType.ALL, null);
            }
            return current;
        }

        if (!isEmpty(MLLNode.LType.XOR)){
            MLLNode previous = head;
            MLLNode current = head.getNext(MLLNode.LType.XOR);
            int obj_id = 0;
            while(obj_id != id && current != null){
                previous = current;
                current = current.getNext(MLLNode.LType.XOR);
                obj_id++;
            }
            if (current != null){
                previous.setNext(MLLNode.LType.XOR, current.getNext(MLLNode.LType.XOR));
            }else{
                previous.setNext(MLLNode.LType.XOR, null);
            }
            return current;
        }

        if (!isEmpty(MLLNode.LType.ANY)){
            MLLNode previous = head;
            MLLNode current = head.getNext(MLLNode.LType.ANY);
            int obj_id = 0;
            while(obj_id != id && current != null){
                previous = current;
                current = current.getNext(MLLNode.LType.ANY);
                obj_id++;
            }
            if (current != null){
                previous.setNext(MLLNode.LType.ANY, current.getNext(MLLNode.LType.ANY));
            }else{
                previous.setNext(MLLNode.LType.ANY, null);
            }
            return current;
        }

        return null;

              }



public Object find(MLLNode.LType ltype, int id) {

    }

我们得到了这个代码,我们创建了插入,删除方法,我们即将创建find。 我们还给出了节点构造函数,如下面的注释中所示:

package project1;
import java.util.EnumMap;

class MLLNode {

    public enum LType {ANY, ALL, XOR};
    public int count;
    public int id;
    public Object data;

    private EnumMap<LType,MLLNode> next;

    private MLLNode(int _id, Object _data) {
        id = _id;
        data = _data;
        count = 0;
        next = new EnumMap<LType,MLLNode>(LType.class);
        next.put(LType.ANY,null);
        next.put(LType.ALL,null);
        next.put(LType.XOR,null);
    }

    public static MLLNode create(int _id, Object _data) {
        return(new MLLNode(_id,_data));
    }

    public MLLNode getNext(LType ltype) {
        return(next.get(ltype));
    }

    public MLLNode setNext(LType ltype, MLLNode ref) {
        next.put(ltype, ref);
        return(ref);
    }

    public void display() {
        System.out.print(id + " (" + count + ")");
    }
} 

现在我们的列表是ANY,ALL和XOR。 当我们插入一个节点时,如果计数器命中1,则当它命中2时它与ANY列表一起从列表中断开,当它命中3时它连接到ALL列表,当它命中4时它断开连接,当它命中5时连接到XOR 。 当我们删除节点时,如果计数器命中0,它将删除该节点,如果计数器击中上述数字之一,它就会执行它在insert方法中执行的操作。 我们如何利用MLList类中的计数器来实现这些限制呢?

提前致谢

0 个答案:

没有答案