简体   繁体   English

在链表的尾部插入一个元素

[英]Insert an element at the tail of a linked list

Have to write the method def insert(self,newElement) which inserts an element at the end of a linked list.必须编写def insert(self,newElement)方法def insert(self,newElement)它在链表的末尾插入一个元素。 If the element already exists in the list then it prints "Value already exists" and does not insert the element.如果该元素已存在于列表中,则它会打印“值已存在”并且不插入该元素。 I have tried this code :我试过这个代码:

class Node:
    def __init__(self,value,next=None):
        self.value=value
        self.next=next

class MyList:
    def __init__(self,a):
        self.head=None
        tail=None
        for i in a:
            n=Node(i,None)
            if self.head is None:
                self.head=n
                tail=n
            else:
                tail.next=n
                tail=n
  
    def showList(self):
        n=self.head
        if n==None:
            print("Empty list")
        while n!=None:
            print(n.value)
            n=n.next
    
    def isEmpty(self):
        n=self.head
        if n is None:
            print("True")
        else:
            print("False")

    def clear(self):
        while(self.head!=None):
            temp=self.head
            self.head=self.head.next
            temp=None

    def insert(self, newElement):
        n = self.head
        newNode = Node(newElement)
        while(n!=None):
            if(n.value==newNode.value):
                print("Value already exists")
            n=n.next
    
        temp = self.head
        while(temp.next != None):
            temp = temp.next
        temp.next = newNode
        print(self.showList())

class tester:
list1 = [1, 2, 3, 4, 5]
linklist = MyList(list1)
linklist.showList()
linklist.insert(5)
 

The problem is the method insert(self,newElement) is able to check if the new element already exists in the list but it is still inserting the element.问题是方法insert(self,newElement)能够检查新元素是否已存在于列表中,但它仍在插入元素。 The method should check if the new element already exists in list or not.该方法应检查新元素是否已存在于列表中。 If it exist then, the method should not insert the new element and if it doesn't exist then it should insert the new element.如果存在,则该方法不应插入新元素,如果不存在,则应插入新元素。 The code I provided above can check if the new element already exist in the list but it is unable to stop inserting the new element.我上面提供的代码可以检查新元素是否已存在于列表中,但无法停止插入新元素。

Have to write the method def insert(self,newElement) which inserts an element at the end of a linked list.必须编写def insert(self,newElement)方法def insert(self,newElement)它在链表的末尾插入一个元素。 If the element already exists in the list then it prints "Value already exists" and does not insert the element.如果该元素已存在于列表中,则它会打印“值已存在”并且不插入该元素。 I have tried this code :我试过这个代码:

class Node:
    def __init__(self,value,next=None):
        self.value=value
        self.next=next

class MyList:
    def __init__(self,a):
        self.head=None
        tail=None
        for i in a:
            n=Node(i,None)
            if self.head is None:
                self.head=n
                tail=n
            else:
                tail.next=n
                tail=n
  
    def showList(self):
        n=self.head
        if n==None:
            print("Empty list")
        while n!=None:
            print(n.value)
            n=n.next
    
    def isEmpty(self):
        n=self.head
        if n is None:
            print("True")
        else:
            print("False")

    def clear(self):
        while(self.head!=None):
            temp=self.head
            self.head=self.head.next
            temp=None

    def insert(self, newElement):
        n = self.head
        newNode = Node(newElement)
        while(n!=None):
            if(n.value==newNode.value):
                print("Value already exists")
            n=n.next
    
        temp = self.head
        while(temp.next != None):
            temp = temp.next
        temp.next = newNode
        print(self.showList())

class tester:
list1 = [1, 2, 3, 4, 5]
linklist = MyList(list1)
linklist.showList()
linklist.insert(5)
 

The problem is the method insert(self,newElement) is able to check if the new element already exists in the list but it is still inserting the element.问题是方法insert(self,newElement)能够检查新元素是否已存在于列表中,但它仍在插入元素。 The method should check if the new element already exists in list or not.该方法应检查新元素是否已存在于列表中。 If it exist then, the method should not insert the new element and if it doesn't exist then it should insert the new element.如果存在,则该方法不应插入新元素,如果不存在,则应插入新元素。 The code I provided above can check if the new element already exist in the list but it is unable to stop inserting the new element.我上面提供的代码可以检查新元素是否已存在于列表中,但无法停止插入新元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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