Blebhebhe
Blebhebhe

Reputation: 93

Counting how many of that "element" appear in a doubly linked list

I have a problem with my nbSandwichs(int type) method It is supposed to go through the doubly linked list and count how many times a sandwitch of the same type appear, everything is good except for the last sandwich that prints 0 which is something that I don't understand, my check method is saying that it doesn't exist but when I created a get last method, it actually does exist. What condition is missing in my nbSandwichs method ? Does my while loop actually doesn't get to the last node??

Thank you

main class : 
    Sandwich s1 = new Sandwich(1);
        Sandwich s1 = new Sandwich(1);
        Sandwich s2 = new Sandwich(15);
        Sandwich s3 = new Sandwich(15);
        Sandwich s4 = new Sandwich(4);
        Sandwich s5 = new Sandwich(15);

        APreparer a1 = new APreparer();
        a1.addfirst(s1); 
        a1.addfirst(s2);
        a1.addfirst(s3);
        a1.addfirst(s4);
        a1.addfirst(s5);

        System.out.println(a1.nbSandwichs(15)); // PRINTS : 3 OK 
        System.out.println(a1.nbSandwichs(1)); // PRINTS : 0 NOT OK 


    public class Sandwich {

        private int type;

        public Sandwich(int type) {
            this.type = type;
            commandes[type]++;
        }

    public class APreparer {

        private UneCommande first;
        private UneCommande last;

        public void addfirst(Sandwich sandwich) {
            UneCommande nouvelle = new UneCommande(sandwich);
            if (first == null) {
                first = nouvelle;
                last = nouvelle;

            } else {
                first = first.addFirst(sandwich);
            }
        }

    int nbSandwichs(int type) {
        if (first == null) {
            return 0;
        } else {
            return first.nbSandwichs(type);
        }
    }

    }


    public class UneCommande {

        private Sandwich sandwich;
        private UneCommande next;
        private UneCommande previous;

        public UneCommande(Sandwich sandwich) {
            this.sandwich = sandwich;
        }

        public UneCommande addFirst(Sandwich sandwich) {
            UneCommande current = this;
            UneCommande newSand = new UneCommande(sandwich);
            newSand.next = current;
            this.previous = newSand;

            return newSand;
        }
int nbSandwichs(int type) {
        int counter = 0;
        UneCommande current = this;

        if (!(check(type))) {
            return 0;
        } else {
            while (current.next != null) {
                if (current.sandwich.getType() == type) {
                    counter++;
                }
                current = current.next;
            }
        }
        return counter;
    }

    boolean check(int type) {
        UneCommande current = this;
        while (current != null) {
            if (current.sandwich.getType() == type) {
                System.out.println("EXIST");
                return true;
            }
            current = current.next;
        }

        return false;
    }
}

Upvotes: 0

Views: 323

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49813

Your loop counts nodes as long as current.next != null. When current is the last node in your list, current.next will be null, and thus not counted.

Upvotes: 1

Related Questions