算法3.1 顺序查找(基于无序链表)(algs4)

符号表是一种存储键值对的数据结构,支持两种操作:插入(put),即将一组新的键值对存入表中;查找(get),即根据给定的键得到相应的值。                                          

                                      算法3.1 顺序查找(基于无序链表)API

public class SequentialSearchST<Key,Value>

private Node first                                链表首节点

 

private class Node                        私有内部Node类                                                                           

Key key                                      保存键

Value val                                     保存值

Node next

public Node(Key key,Value val,Node next)

public Value get(Key key)                                   查找给定的键,返回相关联的值

public void put(Key key,Value val)                        查找给定的键,找到则更新其值,否则在表中新建结点                      

public int size()                                                  表中的键值对数量

public void delete (Key key)                                从表中删去键key及其对应的值

public boolean contains(Key key)                        键key在表中是否有对应的值

public Iterable<Key> Keys()                                表中所有键的集合

package _3_Searching;

import edu.princeton.cs.algs4.Queue;

/** 算法3.1 顺序查找(基于无序链表)
 * 符号表最主要的目的就是将一个键和一个值联系起来。
 * 符号表的实现使用了一个私有内部Node类来在链表中保存键和值。
 * get()的实现会顺序地搜索链表查找给定的键(找到则返回相关联的值)。
 * put()的实现也会顺序地搜索链表查找给定的键,如果找到则更新相关联的值,
 * 否则它会用给定的键值对创建一个新的结点并将其插到链表的开头。
*/
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.key))
				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);         /*未命中,新建结点*/
	}
	public int size()                         /*表中的键值对数量*/
	{
		int N=0;
		for(Node x=first;x!=null;x=x.next)
			N++;
		return N;
	}
	public void delete (Key key)              /*从表中删去键key及其对应的值*/
	{
		for(Node x=first;x!=null;x=x.next)
		{
			if(key.equals(x.next.key))
				x.next=x.next.next;
		}
	}
	public boolean contains(Key key)            /*键key在表中是否有对应的值*/
	{
		for(Node x=first;x!=null;x=x.next)          
			if(key.equals(x.key))               
				return true;
		return false;
	}
	public Iterable<Key> Keys()            /*表中所有键的集合*/
	{
		Queue<Key> q=new Queue<Key>();
		for(Node x=first;x!=null;x=x.next)
			q.enqueue(x.key);
		return q;
	}
}

                                         《算法3.1 顺序查找(基于无序链表)(algs4)》

package _3_Searching;

import java.util.Scanner;

/** 符号表的用例-计频器
 * 从输入中得到一列字符串并记录每个字符串的出现次数,然后遍历所有键并找出出现频率最高的键
*/
public class FrequencyCounter
{

	public static void main(String[] args)
	{
		int minLen=2;            /*最小键长*/
		Scanner sc=new Scanner(System.in);
		SequentialSearchST<String,Integer>  st=new SequentialSearchST<String,Integer> ();
		while(true)
		{
			String s=sc.nextLine();
			if(s.equals("eof"))
				break;
			if(s.length()<minLen) /*忽略较短的单词*/
				continue;
			if(!st.contains(s))
				st.put(s,1);
			else 
				st.put(s, st.get(s)+1);
		}
		String max=" ";
		st.put(max, 0);
		for(String i:st.Keys())
			if(st.get(i)>st.get(max))
				max=i;
		System.out.println(max+" "+st.get(max));
	}

}

                                                                                       
《算法3.1 顺序查找(基于无序链表)(algs4)》                

                          《算法3.1 顺序查找(基于无序链表)(algs4)》

    原文作者:查找算法
    原文地址: https://blog.csdn.net/qq_34803572/article/details/77606397
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞