yaron
yaron

Reputation: 1

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

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

Upvotes: 0

Views: 140

Answers (2)

Kevin Anderson
Kevin Anderson

Reputation: 4592

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;
    }

Upvotes: 1

CryptoFool
CryptoFool

Reputation: 23079

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);

Upvotes: 1

Related Questions