简体   繁体   中英

Counting specific nodes in linked list Java

I have to count nodes in Circular Doubly Linked List in interval [-100;100]. I know how to do that when i'm implementing a node. Here's code :

public void insert(int val){
        ....
        if((val >= -100) && (val <= 100)){
            number++;
        }
.....

But when i'm deleting a node at given position (pos), i have no idea how to check the value of that node, so i don't know if "number" stays the same or it decreases. Here is code of deleting node:

public void deleteAtPos(int pos){
            if (pos == 1){
            if(size == 1){
                start = null;
                end = null;
                size = 0;
                number = 0;
                return;
            }
            start = start.getLinkNext();
            start.setLinkPrev(end);
            end.setLinkNext(start);
            size--;
            return;
        }
        if (pos == size){
            end = end.getLinkPrev();
            end.setLinkNext(start);
            start.setLinkPrev(end);
            size--; 
        }
        }
        Node ptr = start.getLinkNext();
        for (int i = 2; i <= size; i++){
            if (i == pos){
                Node p = ptr.getLinkPrev();
                Node n = ptr.getLinkNext();
                p.setLinkNext(n);
                n.setLinkPrev(p);
                size--;
                return;
            }
            ptr = ptr.getLinkNext();
        }

    }

据我了解,您可以通过添加简单地检查节点的值

ptr.getval() or start.getval()   //depending on the value of pos(aasuming getval is your function to retrieve node data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM