英文:
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<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
答案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<T> 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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论