微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不重复的情况下在双向链表中插入元素?

如何解决如何在不重复的情况下在双向链表中插入元素?

我正在尝试编写一个代码,在不重复的情况下在双向链表中插入一个元素,但它不起作用。

任何帮助将不胜感激。 这是我的代码

public void insert(int value) {
        Element tmp = new Element(value);
        
        if(this.head == null) {
            this.head = this.rear = tmp;
            return ;
        }
        
        if(this.head.data < value) {
            tmp.next = this.head;
            this.head.prevIoUs = tmp;
            tmp.prevIoUs = null;
            this.head = tmp;
            return ;
        }
        
        if(this.rear.data > value) {
            tmp.prevIoUs = this.rear;
            this.rear.next = tmp;
            this.rear = tmp;
            return ;
        }
        else {
        Element cur = this.head;
        while(cur.next != null && cur.next.data > value)            
            cur = cur.next;
        
        tmp.next = cur.next;
        tmp.prevIoUs = cur;
        cur.next.prevIoUs = tmp;
        cur.next = tmp;
        }
        return ;
    }

解决方法

您应该添加第二种方法:

passw = input("Enter password: ")
passw2 = input("Enter password again: ")
if passw == passw2:
    print("Thank you.")
elif passw.lower() == passw2.lower():
    print("They must be in the same case.")
else: 
    print("Incorrect.")

然后将其添加到插入方法中:

public boolean isInList(int value) {
        Element cur = this.head;
        
        if(this.head == null)
            return false;
        
        while(cur != null) {
            if(cur.data == value)
                return true;
            cur = cur.next;
        }
        return false;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。