Unordered symbol table (linked list implementation, JAVA, algorithm (d)) __c language

Source: Internet
Author: User

Introduction to Symbol table: link keys and values. You can insert a key value pair into a symbol table and you want to find the corresponding value later using the key.

As a simple data structure, the linked list is also the first learning, it can be used to implement the queue and stack of other data structure. Symbol table using a linked list is simply to achieve a simple, each of its nodes to save the corresponding key-value pairs, you can reach the traversal key and find the operation of the value. Most of the operations of the list are linear, such as unordered inserts (although this is the head node insertion, but because of the need to traverse the list to query whether the duplicate key, also reached the linear complexity), delete, query ...

Package com.lizi.datastructure.symboltable;
Import java.util.ArrayList; The import java.util.list;//iterator collects all nodes using the unordered symbol table implemented by the linked list//list in the standard library, and cannot allow the key or value to be null public class Sequentialsearchst<key,value
    > {private node first=null;//head node int size=0;
        Private class node{key key;
        Value value;
        Node Next;
            Public Node (Key key,value value,node next) {This.key=key;
            This.value=value;
        This.next=next;
        }}//Given the key to find the value, if there is a key is empty, then the loop may be interrupted, if the value is null, then there is no consistent with the null key returned results,//So do not allow null key or value public value get (key key) {
        For (Node X=first, X!=null x=x.next) {if (Key.equals (X.key)) return x.value;
    return null; ///Find the given key, and then update its value, otherwise create a new node at the head node public void put (key key,value Value) {if key==null| |
        Value==null) return;
                for (Node X=first; X!=null x=x.next) {if (Key.equals (X.key)) {x.value=value;
     Return       } first=new Node (key, value, a);
    size++;
        //delete the corresponding key value pairs node, return its value public value Delete (key key) {node Pre=first;
        Value Value=null; For (Node Now=first Now!=null;pre=now, Now=now.next) {if (Key.equals (Now.key)) {Value=now.val
                Ue
                size--;
                if (Key.equals (First.key)) First=now.next;
            else Pre.next=now.next;
    } return value; }//unordered All keys are added to the iterator public iterable<key> keys () {list<key> keys=new arraylist<key> (size)
        ;
        for (Node X=first; X!=null x=x.next) {keys.add (X.key);
    return keys;
    Public boolean contains (key key) {return get (key)!=null;
    public Boolean IsEmpty () {return size==0;
    public int size () {return size; public void print () {System.out.priNtln ();
        for (Node X=first; X!=null x=x.next) {System.out.print (x.value+ "");
 }
    }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.