📜  用于在链表中搜索元素的Python程序

📅  最后修改于: 2022-05-13 01:55:03.456000             🧑  作者: Mango

用于在链表中搜索元素的Python程序

编写一个函数,在给定的单链表中搜索给定的键“x”。如果 x 存在于链表中,则该函数应返回 true,否则返回 false。

bool search(Node *head, int x)

例如,如果要搜索的键是 15,链表是 14->21->11->30->10,那么函数应该返回 false。如果要搜索的键是 14,那么函数应该返回 true。
迭代解决方案:

1) Initialize a node pointer, current = head.
2) Do following while current is not NULL
    a) current->key is equal to the key being searched return true.
    b) current = current->next
3) Return false 

以下是上述算法的迭代实现以搜索给定的键。

Python
# Iterative Python program to search 
# an element in linked list
  
# Node class
class Node:
      
    # Function to initialise the 
    # node object
    def __init__(self, data):
      
        # Assign data
        self.data = data 
  
        # Initialize next as null
        self.next = None 
  
# Linked List class
class LinkedList:
    def __init__(self):
  
        # Initialize head as None
        self.head = None 
  
    # This function insert a new node at the
    # beginning of the linked list
    def push(self, new_data):
      
        # Create a new Node
        new_node = Node(new_data)
  
        # 3. Make next of new Node as head
        new_node.next = self.head
  
        # 4. Move the head to point to new Node
        self.head = new_node
  
    # This Function checks whether the value
    # x present in the linked list 
    def search(self, x):
  
        # Initialize current to head
        current = self.head
  
        # Loop till current not equal to None
        while current != None:
            if current.data == x:
  
                # Data found
                return True 
              
            current = current.next
          
        # Data Not found
        return False 
  
# Driver code
if __name__ == '__main__':
  
    # Start with the empty list
    llist = LinkedList()
  
    # Use push() to construct list
    # 14->21->11->30->10 
    llist.push(10);
    llist.push(30);
    llist.push(11);
    llist.push(21);
    llist.push(14);
  
    if llist.search(21):
        print("Yes")
    else:
        print("No")
# This code is contributed by Ravi Shankar


Python
# Recursive Python program to 
# search an element in linked list
  
# Node class
class Node:
      
    # Function to initialize 
    # the node object
    def __init__(self, data):
  
        # Assign data
        self.data = data 
  
        # Initialize next as null
        self.next = None 
  
class LinkedList:
      
    def __init__(self):
  
        # Initialize head as None
        self.head = None 
  
    # This function insert a new node at 
    # the beginning of the linked list
    def push(self, new_data):
      
        # Create a new Node
        new_node = Node(new_data)
  
        # Make next of new Node as head
        new_node.next = self.head
  
        # Move the head to 
        # point to new Node
        self.head = new_node
      
      
    # Checks whether the value key 
    # is present in linked list 
    def search(self, li, key):
          
        # Base case
        if(not li):
            return False
          
        # If key is present in 
        # current node, return true
        if(li.data == key):
            return True
          
        # Recur for remaining list
        return self.search(li.next, key)
      
# Driver Code            
if __name__=='__main__':
  
    li = LinkedList()
      
    li.push(1)
    li.push(2)
    li.push(3)
    li.push(4)
      
    key = 4
      
    if li.search(li.head,key):
        print("Yes")
    else:
        print("No")
# This code is contributed by Manoj Sharma


输出:

Yes

递归解决方案:

bool search(head, x)
1) If head is NULL, return false.
2) If head's key is same as x, return true;
3) Else return search(head->next, x)

以下是上述算法的递归实现,用于搜索给定的键。

Python

# Recursive Python program to 
# search an element in linked list
  
# Node class
class Node:
      
    # Function to initialize 
    # the node object
    def __init__(self, data):
  
        # Assign data
        self.data = data 
  
        # Initialize next as null
        self.next = None 
  
class LinkedList:
      
    def __init__(self):
  
        # Initialize head as None
        self.head = None 
  
    # This function insert a new node at 
    # the beginning of the linked list
    def push(self, new_data):
      
        # Create a new Node
        new_node = Node(new_data)
  
        # Make next of new Node as head
        new_node.next = self.head
  
        # Move the head to 
        # point to new Node
        self.head = new_node
      
      
    # Checks whether the value key 
    # is present in linked list 
    def search(self, li, key):
          
        # Base case
        if(not li):
            return False
          
        # If key is present in 
        # current node, return true
        if(li.data == key):
            return True
          
        # Recur for remaining list
        return self.search(li.next, key)
      
# Driver Code            
if __name__=='__main__':
  
    li = LinkedList()
      
    li.push(1)
    li.push(2)
    li.push(3)
    li.push(4)
      
    key = 4
      
    if li.search(li.head,key):
        print("Yes")
    else:
        print("No")
# This code is contributed by Manoj Sharma

输出:

Yes

有关详细信息,请参阅有关在链接列表(迭代和递归)中搜索元素的完整文章!