java - java中字符列表中的字符搜索方法

标签 java list search recursion char

我正在搜索添加的字符列表,并返回"is"或“否”答案来确定某个字符是否出现在我给定的列表中。我对此很陌生,不太确定从哪里开始 - 任何帮助将不胜感激,谢谢

class Csc2001Node
{
protected char ch;
protected Csc2001Node next;

/*
* Construct a Csc2001Node with the given character value 
* @param c - The character 
*/
public Csc2001Node (char c)
{
    this.ch = c;
    this.next = null;
}
}   

public class Csc2001LinkedListRec
{  /* A reference to the head of the list */
protected Csc2001Node head;
/*
* Construct a new empty list 
*/
public Csc2001LinkedListRec()
{
    head=null;
}

public Csc2001Node getHead()
{
    return head;
}


 /*
 * Finds the size of a list.
 * @param head The head of the current list
 * @return The Size of the Current List
 */
 private int size(Csc2001Node head) {
    if (head == null) {
        return 0;
    } else {
        return 1 + size(head.next);
    }
 }

 /*
 * Wrapper method for finding the size of a list.
 * @return The size of the list
 */
 public int size() {
    return size(head);
 }


 /*
 * Adds a new node to the end of a list.
 * @param head The head of the current list
 * @param c The character for the new node
 */
 private void add(Csc2001Node head, char c) {
    // If the list has just one character, add to it.
    if (head.next == null) {
        head.next = new Csc2001Node(c);
    } else {
        add(head.next, c); // Add to rest of list.
    }
 }

 /*
 * Wrapper method for adding a new node to the end of a list.
 * @param c The character for the new node
 */
 public void add(char c) {
    if (head == null) {
        head = new Csc2001Node(c); // List has 1 node.
    } else {
        add(head, c);
    }
 }


 /*
 *Test to see if this list is empty 
 *@returns true or false
 */ 
 public boolean isEmpty()
 {
    return (head == null);
 }


 /*
 * Replaces all occurrences of oldch with newch.
 * @post Each occurrence of oldch has been replaced by newch.
 * @param head The head of the current list
 * @param oldch The character being removed
 * @param newch The character being inserted
 */
 private void replace(Csc2001Node head, char oldch, char newch) {
    if (head != null) {
        if (oldch == head.ch) {
            head.ch = newch;
        }
        replace(head.next, oldch, newch);
    }
 }

 /*
 Wrapper method for replacing oldch with newch.
 * @post Each occurrence of oldch has been replaced by newch.
 * @param oldch The character being removed
 * @param newch The character being inserted
 */
 public void replace(char oldch, char newch) {
    replace(head, oldch, newch);
 }




 //added methods


 //method to recursively print the characters
 private String  recursePrintList(Csc2001Node head) {
    // TODO Auto-generated method stub
 if (head == null)
    return "List is empty";
 else
    while(head.next!=null){
        return head.ch+ recursePrintList(head.next);
    }
 return head.ch + " ";
 }

 /*
 * Wrapper to print out list
 *@return the list
 */

 public void recursePrintList(){
    System.out.println(recursePrintList(head));
 }




 //method to insert char d before char c
 private Csc2001Node insertBefore(char d, Csc2001Node head, char c) {
    // TODO Auto-generated method stub
     {   
            if(head==null){
                return head = new Csc2001Node(d);
            }                       
             else if(head.next.ch == c){
                 head = head.next;
                 head.next = new Csc2001Node(d);
                 head.next.next = head;
             }

        }
    return head;    

}

/*Wrapper method for inserting a char before another char
 * 
 */
public void insertBefore(char c, char d){
    head = insertBefore(c, head, d);
}

 public boolean search(char c) {


    return false;
 }


}

最佳答案

类似于:

search(entry, ch)
    if entry == null
        return false
    if entry.ch == ch
        return true
    return search(entry.next, ch)

...将是一个递归解决方案。以及类似的东西:

search(entry, ch)
    while entry != null
        if entry.ch == ch
            return true
        entry = entry.next
    return false

...将是一个迭代解决方案。我不会向您提供 Java 代码,而只会提供伪代码,因为这看起来像是一项家庭作业。

关于java - java中字符列表中的字符搜索方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29208568/

相关文章:

javax 邮件 : UTF-8 encoding issue

java - 在 JasperReport 中创建/传递 Java bean 数据源

python - 我的元组中的那些小 "u"是什么? ( python 2.7)

excel - 计算特定文本在数组中的次数

php - 在列中搜索每个空格分隔值中的字符串

java - 获取所有 XML 节点的路径

java - 基本属性访问器 :167 - IllegalArgumentException in class in Struts

java - Java中的LinkedList参数

python - 在 [ :index] 上使用动态索引列出切片

android - 您可以使用 Intent 搜索 Google Play 吗?