zoukankan      html  css  js  c++  java
  • 数据结构:单向链表系列5--在链表中查找元素

    在链表中查找元素

    函数签名:

    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, int key)
    {
        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, int key) 
        { 
            Node current = head;    //Initialize current 
            while (current != null) 
            { 
                if (current.data == key) 
                    return true;    //data found 
                current = current.next; 
            } 
            return false;    //data not found 
        }

    c#

      // Checks whether the value key is present in linked list 
        public bool search(Node head, int key) 
        { 
            Node current = head; // Initialize current 
            while (current != null) 
            { 
                if (current.data == key) 
                    return true; // data found 
                current = current.next; 
            } 
            return false; // data not found 
        } 

    递归法:

    bool search(head, x)
    1)如果head是NULL, return false.
    2) 如果head数据域的值和待查找的一致,返回true;
    2) 否则返回 search(head->next, x) 

    c语言:

    /*
        Checks whether the value key is present in linked list
     */
    bool searchRecursive(struct Node* head, int key)
    {
        if(head == NULL)
        {
            return false;
        }
    
        //If key is present in current node,
        //return true
        if(head->data == key)
            return true;
    
        //recursive for remaining list
        return searchRecursive(head->next, key);
    }

     java:

      // Checks whether the value key is present 
        // in linked list 
        public boolean search(Node head, int key) 
        { 
            // Base case 
            if (head == null) 
                return false; 
      
            // If key is present in current node, 
            // return true 
            if (head.data == key) 
                return true; 
      
            // Recur for remaining list 
            return search(head.next, key); 
        } 

    c#

    // Checks whether the value key is present 
        // in linked list 
        public bool search(Node head, int key) 
        { 
            // Base case 
            if (head == null) 
                return false; 
      
            // If key is present in current node, 
            // return true 
            if (head.data == key) 
                return true; 
      
            // Recur for remaining list 
            return search(head.next, key); 
        } 

     文章来源:https://www.geeksforgeeks.org/search-an-element-in-a-linked-list-iterative-and-recursive/

  • 相关阅读:
    centos7-关闭 rpcbind 服务
    nginx进行获取阿里云slb真实ip配置操作
    rsync同步时,删除目标目录比源目录多余文件的方法(--delete)
    nfs安装
    Selenium+PhantomJS使用时报错原因及解决方案
    python json转对象 指定字段名称
    大地坐标系和空间直角坐标系的转换
    python日志输出的内容修改为json格式
    Java String的intern方法
    python 超时重试的方法 signal手段
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11439263.html
Copyright © 2011-2022 走看看