数据结构HashTable,C#语言实现

SeqList类:

using System;
using System.Text;

namespace hashtable.List {
    public class SeqList  {
        private T[] element;
        //顺序表的数组容量
        private int size;
        //顺序表的长度
        private int length;

        public SeqList(int size) {
            this.size = size < 64?64:size;
            this.element = new T[this.size];
            this.length = 0;
        }

        public SeqList(T[] value){
            int n = value.Length;
            if(n>0) {
                this.size = 2*n;
                this.element = new T[this.size];
                for(int i=0;i=0 && i=0 && ilength) i = length;
            for(int j=length-1;j>=i;j--){
                element[j+1] = element[j];
            }
            element[i] = x;
            length ++;
        }

        public void insert(T x) {
            this.insert(length,x);
        }


        public bool remove(int i, ref T old) {
            if(length > 0 && i>=0 && i= 0;
        }
        public void clear() {
            this.length = 0;
        }
    }
}

HashTable类:

using hashtable.List;
using System;
using System.Text;

namespace hashtable.Hash {
    public class HashTable {
        private SeqList []  table;
        private int size;
        public HashTable(int size) {
            this.size = size;
            this.table = new SeqList[size];

            for(int i=0;i(64);
            }
        }

        public int hash(int key) {
            return key % size;
        }

        public void insert(int key) {
            int i = hash(key);
            table[i].insert(key);
        }

        public bool remove(int key) {
            return table[hash(key)].remove(key);
        }

        public int search(int key) {
            return table[hash(key)].index(key);
        }

        public bool contains(int key) {
            return search(key) != -1;
        }

        public override string ToString(){
            StringBuilder sb = new StringBuilder();
            sb.Append("哈希表:\n");
            for(int i=0;i

主程序类:

using System;
using hashtable.Hash;

namespace hashtable
{
    class Program
    {
        static void Main(string[] args)
        {
            const int N = 10;
            int[] keys = {9, 4, 12, 3, 1, 14, 74, 6, 16, 96};
            HashTable ht = new HashTable(N);
            for(int i=0;i

程序输出:


数据结构HashTable,C#语言实现_第1张图片
1.png

你可能感兴趣的:(数据结构HashTable,C#语言实现)