Python 如何搜索和返回链接列表中节点的位置?

Python 如何搜索和返回链接列表中节点的位置?,python,Python,新来的,如果我问得不对,我很抱歉。所以基本上我有一个链表,我想用ll.searchdata通过它的数据搜索一个节点,但我也想返回节点所在的位置,这样我就可以在它后面插入一些东西。我该怎么做 这是用python编写的 def search(self,item): current = self.head found = False while current != None and not found: if current.get_data() == ite

新来的,如果我问得不对,我很抱歉。所以基本上我有一个链表,我想用ll.searchdata通过它的数据搜索一个节点,但我也想返回节点所在的位置,这样我就可以在它后面插入一些东西。我该怎么做

这是用python编写的

def search(self,item):
    current = self.head
    found = False
    while current != None and not found:
        if current.get_data() == item:
            found = True
        else:
            current = current.get_next()
    return found
这是搜索功能的代码

全面实施:

from Node import Node
class linked_list:
    def __init__(self):
        self.head = None    
    def add(self, data):
        node = Node(data)
        if(node != None):
            node.next = self.head
            self.head = node

    def append(self, data):
        node = Node(data)
        if(node != None):
            if(self.head == None):
                self.head = node
            else:
                trav = self.head
                while(trav.next != None):#find last node
                    trav = trav.next
                trav.next = node

    def size(self):
        count = 0
        temp = self.head
        while(temp != None):
            count = count + 1
            temp = temp.next
        return count

    def search(self,item):
        current = self.head
        found = False
        while current != None and not found:
            if current.get_data() == item:
                found = True
            else:
                current = current.get_next()
        return found

    def remove(self, item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.get_data() == item:
                found = True
            else:
                previous = current
                current = current.get_next()
        if previous == None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())


    #insert(pos, item) – adds item to position pos >= 1.
    def insert(self, pos, item):
        size = self.size()
        if(pos <= 0 or (size - pos )< -1):
            print("Not enough items in list to insert in that position. Size =",\
                  size, "position = ", pos)
            return False
        if(pos == 1):
            newNode = Node(item)
            newNode.next = self.head
            self.head = newNode
        else:
            count = 2
            probe = self.head
            while(probe != None and count != pos):
                probe = probe.next
                count += 1
            newNode = Node(item)
            newNode.next = probe.next
            probe.next = newNode
            return True

    #popFirst() – removes and returns the first item in the list.
    def popFirst(self):
        if (self.head == None):
            return None
        else:
            curr = self.head
            self.head = self.head.next
            return curr.data

    #pop() – removes and returns the last item in the list.
    def pop(self):
        if (self.head == None):
            return None
        else:
            curr = self.head
            prev = None
            while(curr.next != None):
                prev = curr
                curr = curr.next
            if(prev == None):#only 1 item in the list
                head = None
                return curr
            else:
                prev.next = None
                return curr

    def printList(self, msg):
        temp = self.head
        print(msg, end = ": ")
        while(temp != None):
            print(temp.data, end=" ")
            temp = temp.next
        print()

    def isEmpty(self):
        return head.next == None

    def peek(self):
        if Node.next == None:
            return False
        else:
            return Node.data

谢谢

python中没有标准的链表实现,因此您必须向我们展示一段代码。对于从函数调用返回多个数据的一般任务,您可以简单地执行。在链表中的位置是不相关的。使用返回节点,您可以在它之后插入。查看您的链表插入方法的工作原理并执行类似操作。如果您想学习提问的正确方法,@yesthisme-您可能需要阅读。返回上一个.next节点。然后您就可以访问节点的数据或与节点相关的任何其他内容。