Wrong order in sorted linked list using jack language (nand2tetris)

I am tasked to write an List find and insertInOrder method. But the insertInOrder is not correctly inserting elements into the list in sorted order. Correct behavior is essential to ensure that the list remains sorted after each insertion.

The code is in jack and the syntax is a bit more primitive than python, which I am used to.

Upon insertion:

  • If the list is empty or the new element is smaller than the first element, it should be placed at the beginning. (2)
  • If the new element is larger than all existing elements, it should be appended at the end. (2,4)
  • Otherwise, it should be inserted at the appropriate position within the list. (2,3,4)

I also have a main file where i test to insert values into the list.

The correct output in the VMemulator should be:

-> 3 -> 5 -> 6

-> 2 -> 3 -> 5 -> 6

-> 2 -> 3 -> 4 -> 5 -> 6

-> 2 -> 3 -> 4 -> 5 -> 6 -> 10

-> 5 -> 6 -> 10

Not Found!

-> 2 -> 3 -> 4 -> 5 -> 6 -> 10

but i get:

-> 3 -> 5 -> 6

-> 3 -> 2 -> 5 -> 6

-> 3 -> 2 -> 4 -> 5 -> 6

-> 3 -> 2 -> 4 -> 5 -> 6 -> 10

-> 5 -> 6 -> 10

Not Found!

-> 2 -> 3 -> 4 -> 5 -> 6 -> 10

so the problem is probably something relating to how i handle the head of the list but i have a hard time figuring out where my code is incorrect.

Any help or input would be great!

    field int data;
    field List next;

    /* Creates a new List object. */
    constructor List new(int car, List cdr) {
        let data = car;
        let next = cdr;
        return this;
    }

    /* Disposes this List by recursively disposing its tail. */
    method void dispose() {
        if (~(next =  null)) {          //if the nezt node is not null
            do next.dispose();          //dispose recursively
        }
        do Memory.deAlloc(this);        // Use an OS routine to recycle the memory held by this object.
        return;
    }

    /* Prints the list recursively*/
    method void print() {
        do Output.printString(" -> ");
        do Output.printInt(data);      // Print the current node's data.
        if (~(next =  null)) {
            do next.print();
        }
        return;
    }

    /* Inserts the argument in the right position of the list (ascending order) */
    method void insertInOrder(int ins) {
        var List prev, curr, insert;

        let prev = this;                             //Start with the current node 
        let curr = prev.getnext();                   //Get the next node
        
        while ((~(curr = null)) & (ins > prev.getdata())){               // Loop until we find where to insert
            if (ins < curr.getdata()){
                let insert = List.new(ins, curr);
                do prev.setnext(insert);
                return;
            }
            let prev = prev.getnext();
            let curr = prev.getnext();
        }
        let insert = List.new(ins, curr);   // Handle insertion at end or in empty list
        do prev.setnext(insert);
        return;
    }

    /* Searches the argument in the list, if found, it returns the corresponding List object */
    method List find(int toFind) {
        var List temp;
        var boolean found;
        let temp = this;
        let found = false;

        while ((~(temp = null))) {              // Corrected loop condition
            if (toFind = temp.getdata()) { // Corrected comparison
                let found = true;
                return temp;                // Return immediately if found
            }
            let temp = temp.getnext();      // Move to the next node
        }
        return null;                        // Return null if not found
    }

    /* Helper method to get the next node */
    method List getnext(){
        return next;
    }

    /* Helper method to set the next node */
    method void setnext(List object){
        let next = object;
        return;
    }

    /* Helper method to get the data of this node */
    method int getdata(){
        return data;
    }

}

    function void main(){
        var List v, w, x;
        let v = List.new(3,null);    // Create a new List v object with data 3 and set the next pointer to null (empty list)
        do v.insertInOrder(6);       // Insert elements into the list in order
        do v.insertInOrder(5); 
        
        do v.print();                //v=3,5,6
        do Output.println();

        do v.insertInOrder(2);       // Insert (2)
        
        do v.print();                //v=2,3,5,6
        do Output.println();

        do v.insertInOrder(4);      // Insert(4)
        
        do v.print();               //v=2,3,4,5,6
        do Output.println();

        do v.insertInOrder(10);     // Insert another element (10)
        
        do v.print();               //v=2,3,4,5,6,10
        do Output.println();


        let w = v.find(5);          // Try to find element with value 5
        if (~ (w = null)){
            do w.print();           //w=5,6,10
        }
        else{
            do Output.printString("Not Found!");
        }
        do Output.println();

        let x = v.find(7);          // Try to find element with value 7 (not in the list)
        if (~ (x = null)){
            do x.print();
        }
        else{
            do Output.printString("Not Found!");
        }
        do Output.println();

        let x = v.find(2);      // Try to find element with value 2
        if (~ (x = null)){
            do x.print();       //x=2,3,4,5,6,10
        }
        else{
            do Output.printString("Not Found!");
        }
        do Output.println();

        return; 
    }
} 

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật