码迷,mamicode.com
首页 > 其他好文 > 详细

ALGORITHM3.1 Sequential search (in an unordered linked list)

时间:2018-06-07 21:52:14      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:port   algorithm   first   order   code   lis   value   public   get   

import edu.princeton.cs.algs4.*;

public class SequentialSearchST<Key, Value>
{
    private Node first;
    
    private class Node
    {
        Key key;
        Value val;
        Node next;
        
        public Node(Key key, Value val, Node next)
        {
            this.key = key;
            this.val = val;
            this.next = next;
        }
    }
    
    public Value get(Key key)
    {
        for(Node x = first; x != null; x = x.next)
        {
            if(key.equals(x.val))
                return x.val;
        }
        return null;
    }
    
    public void put(Key key, Value val)
    {
        for(Node x = first; x != null; x = x.next)
        {
            if(key.equals(x.key))
            {
                x.val = val;
                return;
            }
            first = new Node(key, val, first);
        }
    }
}

 

ALGORITHM3.1 Sequential search (in an unordered linked list)

标签:port   algorithm   first   order   code   lis   value   public   get   

原文地址:https://www.cnblogs.com/w-j-c/p/9152783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!