简体   繁体   English

java实现中的单链表

[英]singly linked-list in java implementation

I have being given a task to implement the following method in Java: SubList(S, L) returns true if list S is a sublist of list L.我被赋予了在 Java 中实现以下方法的任务:如果列表 S 是列表 L 的子列表,则 SubList(S, L) 返回 true。

I need to use the following notation when I process lists as an abstract object in the recurrence relations.当我将列表作为递归关系中的抽象对象处理时,我需要使用以下符号。 Given a list L, we write L = [H|R] where H is the head of the list and R is the rest of the list.给定一个列表 L,我们写 L = [H|R] 其中 H 是列表的头部,R 是列表的其余部分。 For an empty list L, we write L = [].对于空列表 L,我们写 L = []。 For example, if L = [1, 2, 3, 4, 5], H = 1, and R = [2, 3, 4, 5].例如,如果 L = [1, 2, 3, 4, 5],H = 1,并且 R = [2, 3, 4, 5]。

Hence my tasks are:因此我的任务是:

a) Provide the recurrence relations for SubList(S, L) using the above list notation. a) 使用上述列表符号提供 SubList(S, L) 的递推关系。

b) Implement the recursive algorithm in Java using the recurrence relations. b) 使用递推关系在 Java 中实现递推算法。

Being stuck on this task for a day and a half now and still having trouble how to do this.现在被困在这个任务上一天半了,但仍然无法做到这一点。 Appreciate, if anyone can help me to solve this problem.感谢,如果有人能帮我解决这个问题。

This is the java code I have been given to work with.这是我要使用的 Java 代码。

class SLLNode {

    public Object info;     // This is the data
    public SLLNode next;    // this is the address of the next node
    public SLLNode() {      // Here's how we construct an empty list.
        next = null;
    }
    public SLLNode(Object el) {
        info = el; next = null;
    }
    public SLLNode(Object el, SLLNode ptr) {
        info = el; next = ptr;
    }

}

class SLList {

    protected SLLNode head = null;
    public SLList() {
    }

    public void setToNull() {
        head = null;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public Object first() {
        return head.info;
    }

    public SLLNode head() {
        return head;
    }

    public void printAll() {
       for (SLLNode tmp = head; tmp != null; tmp = tmp.next)
            System.out.print(tmp.info.toString());
    }

    public void add(Object el) {
        head= new SLLNode(el,head);
    }

    public Object find(Object el) {
        SLLNode tmp = head;
        for ( ; tmp != null && !el.equals(tmp.info); tmp = tmp.next);
        if (tmp == null)
             return null;
        else return tmp.info;
    }

    public boolean member(Object el) {
            SLLNode tmp = head;
            for ( ; tmp != null && !el.equals(tmp.info); tmp = tmp.next);
            if (tmp == null)
                 return false;
            else return true;
    }

    public Object deleteHead() { // remove the head and return its info;
        Object el = head.info;
        head = head.next;
        return el;
    }

    public void delete(Object el) {    // find and remove el;  
        if (head != null)              // if non-empty list;
             if (el.equals(head.info)) // if head needs to be removed;
                  head = head.next;
             else {
                  SLLNode pred = head, tmp = head.next;
                  for ( ; tmp != null && !(tmp.info.equals(el));
                          pred = pred.next, tmp = tmp.next);
                  if (tmp != null)     // if found
                        pred.next = tmp.next;
             }
    }

}
SubList([], L)      = true (1)
SubList(H|[], H|*)  = true (2)
SubList(H|[], [])   = false (3)
SubList(SH|[], H|R) = SubList(SH|[], H|[]) OR SubList(SH|[], R) (4)
SubList(SH|SR, L)   = SubList(SH|[], L) AND SubList(SR, L) (5)

In english, it means that if L contains the first element of your sublist S AND that it contains the remaining elements of the sublist, then your SubList method is gonna return true.在英语中,这意味着如果 L 包含您的子列表 S 的第一个元素并且它包含子列表的其余元素,那么您的 SubList 方法将返回 true。

The recursion here visible on the 4th and 5th lines, where you see that we are invoking the same function again and again until SR contains only one element.此处的递归在第 4 行和第 5 行可见,您会看到我们一次又一次地调用相同的函数,直到 SR 只包含一个元素。

Ex:前任:

L = [1,2,5]
S = [1,5]
SubList(S, L) = SubList([1,5], [1,2,5])
= SubList(1|[5], 1|[2,5]) (4+5)
= SubList(1|[], 1|[2,5]) AND SubList([5], 1|[2,5])
= SubList(1|[], 1|[2,5]) AND (SubList(5|[], 1|[]) OR SubList(5|[], [2,5]))
= SubList(1|[], 1|[2,5]) AND (SubList(5|[], 1|[]) OR (SubList(5|[], 2|[]) OR SubList(5|[], [5])))
= SubList(1|[], 1|[2,5]) AND (SubList(5|[], 1|[]) OR (SubList(5|[], 2|[]) OR SubList(5|[], 5|[])))
= true AND (false OR (false OR true))
= true AND true 
= true

Possible Java implementation:可能的 Java 实现:

public SLList(SLNode h) {
   this.head = h;
}

public SLList singletonList(SLNode n) {
    return new SLList(new SLNode(n.info));
}

public SLList remainingList(SLNode n) {
    if(n == null) return new SLList();
    return new SLList(n);
}

private static boolean subList(SLList s, SLList l) {
   if(s.isEmpty()) return true;
   if(l.isEmpty()) return false;
   if(s.head.next == null && l.first().equals(s.first())) return true;
   return (subList(singletonList(s.head), singletonList(l.head)) || 
     subList(singletonList(s.head), remainingList(l.head.next))) && 
     subList(remainingList(s.head.next), l);
}

I haven't tested the code, there might be some null checks missing but the important thing is that you get the idea of how recursion works.我还没有测试过代码,可能缺少一些空检查,但重要的是您了解递归的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM