链接列表中的拆分值

问题描述 投票:0回答:1

我正在编写代表重组DNA过程的代码。我在如何使用剪切和连接方法前进的过程中碰壁。我已经编写了一个while循环来搜索我们正在寻找的酶的索引,但是现在我遇到了麻烦。

我的班级代表一个链表,它应执行以下操作:

  • 首先,它获得一条以String表示的DNA链,并将其完整LinkedList的第一个节点中的字符串。
  • 比它在链中搜索特定的酶序列(节点)(cutAndConnect中的while循环)
  • 比它需要在该点和酶的末端切开字符串,切断酶。
  • [它需要将连接放置在新节点中,而将在另一个新节点中位于酶序列之后的strand(String)。
  • 并且它需要对整个链进行此操作,因此一直以来,特定的酶序列出现在链中。

我还添加了我尚未放入任何旅馆的所有方法。 DnaStreng是此项目中使用的接口,但尚未将接口添加到此问题中,因为它基本上只包含所有方法的javascript。

我希望你们中的一些人可以帮助我继续前进。我有以下代码:

public class LinkStreng implements DnaStreng{

    public static LinkStreng STRING_LEEG = new LinkStreng();
    private String dna;
    List<String> dnaList= new LinkedList<String>();
    Node head;
    Node next;

    /* Makes a new String with length 0*/
    public LinkStreng(){
        this("");
    }

    /* Makes a new LinkStreng with the given DNA(in the main class there is a 
     filechooser used.*/
    public LinkStreng(String hetDna){
     dna = hetDna;
     head = null;
     Node firstNode = new Node(hetDna);
    }

    /*Counts the number of Nodes in the list*/
    public int getCount(){
        Node temp = head;
        int count = 0;
        while (temp!= null){
            count++;
            temp = temp.next;
        }
        return count;
    }

    / ** Simulates a cut by a restriction enzyme. Seeks the first occurrence
    * of the enzyme in this strand and removes the enzyme and everything after it
    * coming. Returns the part after the enzyme as a new strand. If the
    * enzyme does not occur in this strength, it remains unchanged and an
    * returned empty strand will be returned.
    * @param enzyme the string to search for
    * @return the part of this strength after the enzyme * /
    public DnaStreng cutWith(String enzym) {
        // Here I get an error that it cannot find the getCount method
        if (dnaList.getCount() >1){
            throw new RuntimeException("Linkstreng heeft meer dan 1 node");
        } else {             
        int enzymBegin = dna.indexOf(enzym);
        if (enzymBegin == -1) return STRENG_LEEG;

        // Enzyme found: cut
        String dnaAchterEnzym = dna.substring(enzymBegin + enzym.length());
        DnaStreng achter = new LinkStreng(dnaAchterEnzym);
        initialiseer(dna.substring(0, enzymBegin));
        return achter;
        }
    }



     / ** Cut this strand wherever the enzyme occurs and connect the
     * pieces by placing the connection between them.
     * @param enzym the string to search for
     * @param connectionthe DNA that will replace the enzyme
     * @return the new strand (the original strand remains unchanged) * /
    public DnaStreng cutAndConnect(String enzym, String connection) {
        // With this getCount I get the same error
        if (dnaList.getCount() > 1){
            throw new RuntimeException("Linkstreng heeft meer dan 1 node");
        }
        String teSplitsen = " " + dna + " ";
        int i = 0;
        while ( i < dna.length()){
        dna.indexOf(enzym, i);
        i++;
        }

        return new LinkStreng();

    }

    /* Gives the number of letters in the strand
    public long lengte() {
      // this method isn't working as well, it returns the wrong number
       return dnaList.toString().length();
    }

    / ** Initializes this strand by inserting the DNA, any
     * previous data is erased. Does not check for valid DNA letters.
     * @param dna the DNA string placed in this strand * /
    public void initialiseer(String hetDna) {

    }



    / ** Adds the addition behind this strand.
     * @param addition the strand being added * /
    public void voegToe(DnaStreng addition) {

    }

    / ** Adds the addition behind this strand.
     * @param addition the strand being added * /
    public void voegToe(String addition) {

    }


    public String toString(){
        Node current = head;
        StringBuilder recombinant = new StringBuilder((CharSequence) dnaList);
        while(current != null){
            recombinant = recombinant.append(head.value);
            current = current.next;
        }
        return recombinant.toString();
    }
}

public class Node {
    public Node next;
    public String value;

    public Node(String s){
        value = s;
        next = null;
    }
java linked-list
1个回答
0
投票

该代码似乎是针对同一目标的矛盾方法的混合。

例如:

List<String> dnaList = new LinkedList<String>();
Node head;
Node next;
  • 一方面,此示例中的第一行建议您尝试使用Java标准库中的LinkedList形式。

  • 另一方面,接下来的两行是尝试对类似列表数据结构进行自定义实现的证据。

还有更多示例说明为什么此代码无法正常工作,我相信这足以令人信服。

[通常,避免使用混乱的方法一次编写整个程序(罗马不是一天建成的!),我强烈建议一次只工作一小段,并为每一位编写测试。

我将演示提议的方法,并逐步介绍您的方法:

  1. 首先,它获得一个表示为String的DNA链,并将整个String放在LinkedList的第一个节点中。

极有可能,您不想将整个字符串存储在一个节点中。您真正想要的是存储一个字符(只是一个字母,而字符串是字母序列)。

public class DNAStrand {
    Node head = null;

    public DNAStrand(final String dnaStrand) {
        if (dnaStrand.isEmpty()) {
            return;
        }

        // Initialise the first node of a list
        Node currentNode = new Node(dnaStrand.charAt(0));

        // Assign the first Node to `head`
        head = currentNode;

        // Iterate through the rest of string storing remaining characters
        for (int i = 1; i < dnaStrand.length(); i++) {
            final Node nextNode = new Node(dnaStrand.charAt(i));
            currentNode.next = nextNode;
            currentNode = nextNode;
        }
    }

    private static class Node {
        public Node next;
        public char value;

        public Node(char s) {
            value = s;
            next = null;
        }
    }

    // Tests are located below this line
    private static void testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList() {
        final DNAStrand dnaStrand = new DNAStrand("acgt");
        if (dnaStrand.head == null) {
            throw new AssertionError("The head is null after creating an object from a string");
        }

        List<Character> actualString = new ArrayList<>();
        Node listPointer = dnaStrand.head;
        do {
            actualString.add(listPointer.value);
            listPointer = listPointer.next;
        } while (listPointer != null);

        if (!"acgt".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
            throw new AssertionError("Wrong value in the first node, expected: 'agct;, got: "
                    + dnaStrand.head.value);
        }
    }

    public static void main(String[] args) {
        testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList();
    }
}
  • 比它在链中搜索特定的酶序列(节点中的字符串)(cutAndConnect中的while循环)
  • 然后需要在该点处以及在酶的末端切下String,将酶切掉。
  • 然后需要将连接放置在一个新节点中,并将位于酶序列之后的strand(String)部分放置在另一个新节点中。
  • 并且它需要对整条链进行操作,因此对于特定的酶序列在链中出现的所有情况都是如此。

本质上,这是对一种方法的描述。我将添加一个cutAndConnect(),它可以满足您的要求以及解释/证明其有效的测试。

public class DNAStrand {
    Node head = null;

    public DNAStrand(final String dnaStrand) {
        if (dnaStrand.isEmpty()) {
            return;
        }

        head = stringToListNodes(dnaStrand);
    }

    private static class Node {
        public Node next;
        public Node previous;
        public char value;

        public Node(char character, final Node previousNode) {
            this.value = character;
            this.next = null;
            this.previous = previousNode;
        }
    }

    private Node stringToListNodes(final String string) {
        // Initialise the first node of a list
        Node currentNode = new Node(string.charAt(0), null);

        // Assign the first Node to `head`
        final Node head = currentNode;

        // Iterate through the rest of string storing remaining characters
        for (int i = 1; i < string.length(); i++) {
            final Node nextNode = new Node(string.charAt(i), currentNode);
            currentNode.next = nextNode;
            currentNode = nextNode;
        }

        return head;
    }

    public void cutAndConnect(final String enzyme, final String linker) {
        if (linker.isEmpty()) {
            throw new IllegalArgumentException("Linked can't be empty");
        }
        Node potentialEnzymeStart = head;
        while (potentialEnzymeStart != null) {
            Node currentNode = potentialEnzymeStart;
            boolean dismatchFound = false;

            for (char enzymeSymbol : enzyme.toCharArray()) {
                if (enzymeSymbol != currentNode.value) {
                    dismatchFound = true;
                    break;
                }
                currentNode = currentNode.next;
            }

            if (!dismatchFound) {
                // The enzyme does match the sequence
                // The DNA has the following structure <left piece> <enzyme> <right piece>. Find the left and right piece nodes
                Node leftPieceEnd = potentialEnzymeStart.previous;
                if (leftPieceEnd == null) {
                    // Replace the current head
                    head = stringToListNodes(linker);
                } else {
                    // Simply connect a node of a doubly linked list
                    final Node linkedInAFormOfList = stringToListNodes(linker);
                    leftPieceEnd.next = linkedInAFormOfList;
                    linkedInAFormOfList.previous = leftPieceEnd;
                }

                Node connectionPoint = leftPieceEnd == null ? head : leftPieceEnd.next;
                for (int i = 0; i < linker.length() - 1; ++i) {
                    connectionPoint = connectionPoint.next;
                }

                Node rightPieceStart = potentialEnzymeStart;
                for (int i = 0; i < enzyme.length(); ++i) {
                    rightPieceStart = rightPieceStart.next;
                }

                if (rightPieceStart != null) {
                    connectionPoint.next = rightPieceStart;
                    rightPieceStart.previous = connectionPoint;
                }
            }

            potentialEnzymeStart = potentialEnzymeStart.next;
        }
    }

    // Tests are located below this line
    private static void testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList() {
        final DNAStrand dnaStrand = new DNAStrand("acgt");
        if (dnaStrand.head == null) {
            throw new AssertionError("The head is null after creating an object from a string");
        }

        List<Character> actualString = new ArrayList<>();
        Node listPointer = dnaStrand.head;
        do {
            actualString.add(listPointer.value);
            listPointer = listPointer.next;
        } while (listPointer != null);

        if (!"acgt".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
            throw new AssertionError("Wrong value in the first node, expected: 'agct`, got: "
                    + dnaStrand.head.value);
        }
    }

    private static void testCutAndConnectCutEntireDNAString() {
        final DNAStrand dnaStrand = new DNAStrand("acgt");
        dnaStrand.cutAndConnect("acgt", "a");
        if (dnaStrand.head == null) {
            throw new AssertionError("The head of a list must not be null");
        }

        if (dnaStrand.head.value != 'a') {
            throw new AssertionError("The head of the list contains wrong value, expected: 'a`, got: " +
                    dnaStrand.head.value);
        }

        if (dnaStrand.head.next != null) {
            throw new AssertionError("The list must have the length 1");
        }
    }

    private static void testCutAndConnectCutTheMiddleOfDNAString() {
        final DNAStrand dnaStrand = new DNAStrand("acca");
        dnaStrand.cutAndConnect("cc", "g");

        List<Character> actualString = new ArrayList<>();
        Node listPointer = dnaStrand.head;
        do {
            actualString.add(listPointer.value);
            listPointer = listPointer.next;
        } while (listPointer != null);

        if (!"aga".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
            throw new AssertionError("Wrong value in the list, expected: 'aga`, got: "
                    + dnaStrand.head.value);
        }
    }

    private static void testCutAndConnectCutMultipleOccurrencesOfAnEnzyme() {
        final DNAStrand dnaStrand = new DNAStrand("accacca");
        dnaStrand.cutAndConnect("cc", "g");

        List<Character> actualString = new ArrayList<>();
        Node listPointer = dnaStrand.head;
        do {
            actualString.add(listPointer.value);
            listPointer = listPointer.next;
        } while (listPointer != null);

        if (!"agaga".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
            throw new AssertionError("Wrong value in the list, expected: 'agaga`, got: "
                    + dnaStrand.head.value);
        }
    }

    public static void main(String[] args) {
        testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList();
        testCutAndConnectCutEntireDNAString();
        testCutAndConnectCutTheMiddleOfDNAString();
        testCutAndConnectCutMultipleOccurrencesOfAnEnzyme();
    }
}

我的所有猜测都正确吗?

通常来说,您发布的问题不符合StackOverflow条件。仅仅是因为这不是一个问题,而是更多的“我的代码无法正常工作”:)

下次,尝试缩小问题范围,并提供一个最小的代码示例。

© www.soinside.com 2019 - 2024. All rights reserved.