java - 计算有多少个 "element"出现在双向链表中

标签 java doubly-linked-list find-occurrences

我的 nbSandwichs(int type) 方法有问题 它应该遍历双向链表并计算相同类型的三明治出现的次数,除了最后一个三明治打印 0 之外一切都很好,这是我不明白的,我的检查方法是这样的它不存在,但当我创建 get last 方法时,它实际上确实存在。我的 nbSandwichs 方法中缺少什么条件?我的 while 循环实际上没有到达最后一个节点吗?

谢谢

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

最佳答案

只要current.next != null,您的循环就会对节点进行计数。当 current 是列表中的最后一个节点时,current.next 将为 null,因此不计算在内。

关于java - 计算有多少个 "element"出现在双向链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308422/

相关文章:

java - 想要使用 float 操作按钮将项目添加到 ViewPager2 中包含的特定 fragment 中的回收 View

java - 它是装饰器模式吗?

c++ - 试着自己实现一个双向链表;调用 afficheList() 两次我得到一个无限循环?

SED 替换了一些首次出现(和范围)的模式

java - 使用 split() 方法 JAVA 时利用字符串的不同部分

java - 如何使用 JOOQ 在 PostgreSQL 中插入带有 JSON 列的可更新记录?

c - C中双向链表的实现(删除任意位置的节点)

algorithm - 黑客排名 : Inserting a Node Into a Sorted Doubly Linked List - Kotlin

java - 将字符串中字母的出现次数计数到 26 的 int 数组中

java - 使用 Collections 或我的函数计算 ArrayList 中对象的出现次数