在链表中查找元素
函数签名:
bool search(Node *head, int x)
如果在链表中查找到这个元素返回true,否则false
迭代法
2) 初始化一个节点指针, current = head.
3) 如果current不为NULL执行以下循环
a) current->key 等于当前待查找值key则返回true.
b) current = current->next
4) 否则返回 false
/*Checks whether the value key is present in linked list*/
bool search(struct Node* head, intkey)
{
struct Node* current = head; //Initialize current
while(current !=NULL)
{
if(current->data ==key)
return true;
current = current->next;
}
return false;
}
java:
//Checks whether the value key is present in linked list
public boolean search(Node head, intkey)
{
Node current = head; //Initialize current
while (current != null)<