尝试使用递归计算队列中索引的数量,而无需将其移除。

huangapple go评论74阅读模式
英文:

Trying to count the number of indexes in a queue without removing them with a recursion

问题

我正试图使用递归计算队列中的索引数量。队列是一个列表,由类"Queue"中的函数组成。我目前拥有一个函数,可以计算索引,但同时也会删除它们。我已经尝试了所有方法,但仍然没有成功,所以我请你们帮帮我。我必须补充一点,如果你看到一个以大写字母开头的函数,请忽略它,并且请像它不是以大写字母开头一样处理,谢谢! 尝试使用递归计算队列中索引的数量,而无需将其移除。

public class Queue<T>
{
    private Node<T> first;
    private Node<T> last;

    public Queue()
    {
        this.first = null;
        this.last = null;
    }
    public void Insert(T x)
    {
        Node<T> temp = new Node<T>(x);
        if (first == null)
            first = temp;
        else
            last.setNext(temp);
        last = temp;
    }
    public T Remove()
    {
        T x = first.getValue();
        first = first.getNext();
        if (first == null)
            last = null;
        return x;
    }
    public T Head()
    {
        return first.getValue();
    }
    public boolean IsEmpty()
    {
        return first == null;
    }
    public String toString()
    {
        String s = "[";
        Node<T> p = this.first;
        while (p != null)
        {
            s = s + p.getValue().toString();
            if (p.getNext() != null)
                s = s + ",";
            p = p.getNext();
        }
        s = s + "]";
        return s;
    }
}

public class Node<T>
{
    private T value;
    private Node<T> next;
    public Node(T value)
    {
        this.value = value;
        this.next = null;
    }
    public Node(T value, Node<T> next)
    {
        this.value = value;
        this.next = next;
    }
    public T getValue()
    {
        return value;
    }
    public Node<T> getNext()
    {
        return next;
    }
    public void setValue(T value)
    {
        this.value = value;
    }
    public void setNext(Node<T> next)
    {
        this.next = next;
    }
    public String toString()
    {
        return this.value+ " → " +next;
    }
}//Class

public class Main {

    public static void main(String[] args) {

        Queue<Integer> q = new Queue<Integer>();

        q.Insert(1);
        q.Insert(2);
        q.Insert(3);
        q.Insert(4);
        System.out.println("Queue: " + q);

        countIndex(q);

        System.out.println("Queue after the function: " + q);
    }

    public static int countIndex(Queue<Integer> q) {
        if(q.Head() == null) return 0;
        q.Insert(q.Remove());
        return 1 + countIndex(q);
    }

}//class
英文:

I'm trying to count with a recursion the number of indexes in a queue. A queue is a list and it consists of the functions that are in the class "Queue". What I have right now is a function that counts the indexes BUT also removes them. I've tried EVERYTHING and still no success so I'm asking you guys to help me. I Must add that if you see a function that starts with a capital letter, IGNORE it and behave please like its not with a capital letter, Thx! 尝试使用递归计算队列中索引的数量,而无需将其移除。

public class Queue&lt;T&gt;
{
private Node&lt;T&gt; first;
private Node&lt;T&gt; last;
public Queue()
{
this.first = null;
this.last = null;
}
public void Insert(T x)
{
Node&lt;T&gt; temp = new Node&lt;T&gt;(x);
if (first == null)
first = temp;
else
last.setNext(temp);
last = temp;
}
public T Remove()
{
T x = first.getValue();
first = first.getNext();
if (first == null)
last = null;
return x;
}
public T Head()
{
return first.getValue();
}
public boolean IsEmpty()
{
return first == null;
}
public String toString()
{
String s = &quot;[&quot;;
Node&lt;T&gt; p = this.first;
while (p != null)
{
s = s + p.getValue().toString();
if (p.getNext() != null)
s = s + &quot;,&quot;;
p = p.getNext();
}
s = s + &quot;]&quot;;    
return s;
}
}
public class Node&lt;T&gt;
{
private T value;
private Node&lt;T&gt; next;
public Node(T value)
{
this.value = value;
this.next = null;
}
public Node(T value, Node&lt;T&gt; next)
{
this.value = value;
this.next = next;
}
public T getValue()
{
return value;
}
public Node&lt;T&gt; getNext()
{
return next;
}
public void setValue(T value)
{
this.value = value;
}
public void setNext(Node&lt;T&gt; next)
{
this.next = next;
}
public  String toString()
{
return this.value+ &quot;  &quot; +next;
}
}//Class
public class Main {
public static void main(String[] args) {
Queue&lt;Integer&gt; q = new Queue&lt;Integer&gt;();
q.Insert(1);
q.Insert(2);
q.Insert(3);
q.Insert(4);
System.out.println(&quot;Queue: &quot; + q);
countIndex(q);
System.out.println(&quot;Queue after the function: &quot; + q);
}
public static int countIndex(Queue&lt;Integer&gt; q) {
if(q.Head() == null) return 0;
q.Insert(q.Remove());
return 1 + countIndex(q);
}
}//class

答案1

得分: 1

我不知道为什么你必须使用递归,但是这里有一对用于你的队列类(Queue)的方法,它们使用递归并返回链表(Queue)中节点的数量:

private int rsize(Node<T> p) {
    if (p == null)
        return 0;
    return rsize(p.getNext()) + 1;
}

public int size() {
    return rsize(first);
}
英文:

I don't know why you have to use recursion, but here's a pair of methods for your Queue class that uses recursion and return the number of nodes in your linked list (Queue):

private int rsize(Node&lt;T&gt; p) {
if (p == null)
return 0;
return rsize(p.getNext()) + 1;
}
public int size() {
return rsize(first);

答案2

得分: 1

没有保证,但是类似这样的代码应该可以实现:

public static int countIndex(Queue<Integer> q) {
    return countIndex(q, new Queue<Integer>());
}
private static int countIndex(Queue<Integer> q, 
                              Queue<Integer> temp) 
{
    int size = 0;
    if(q.Head() != null) {
        temp.Insert(q.Remove());
        size = 1 + countIndex(q, temp);
        q.Insert(temp.Remove());
    }
    return size;
}
英文:

No gurantees, but something like this should do it:

    public static int countIndex(Queue&lt;Integer&gt; q) {
return countIndex(q, new Queue&lt;Integer&gt;());
}
private static int countIndex(Queue&lt;Integer&gt; q, 
Queue&lt;Integer&gt; temp) 
{
int size = 0;
if(q.Head() != null) {
temp.Insert(q.Remove());
size = 1 + countIndex(q, temp);
q.Insert(temp.Remove());
}
return size;
}

huangapple
  • 本文由 发表于 2020年9月15日 09:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63893876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定