• c++ 学习之 利用哈希建立一个 集合


    #define CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    
    class SET {
    	int* set; 			//set用于存放集合元素 
    	int card; 			//card为能够存放的元素个数 
    	int used; 			//used为已经存放的元素个数
    public:
    	SET(int card); 		//card为能够存放的元素个数 
    	~SET();
    	int size(); 			//返回集合已经存放的元素个数 
    	int insert(int v); 		//插入v成功时返回1,否则返回0 
    	int remove(int v); 	//删除v成功时返回1,否则返回0 
    	int has(int v); 		//元素v存在时返回1,否则返回0 
    };
    
    SET::SET(int card) :set(new int[card] {}), card(card), used(0)
    {
    	for (int k = 0; k < card; k++)
    	{
    		set[k] = -1;   // 假设不存储 - 1 
    	}
    }
    
    int SET::insert(int v)
    {
    	if (used == card || has(v)==1 ) return 0;
    	int hash = v % card;
    	while (set[hash] != -1)
    	{
    		hash = ++hash == card ? 0 : hash;
    	}
    	set[hash] = v;
    	used++;
    	return 1;
    }
    
    int SET::remove(int v)
    {
    	if (has(v) == 0  )
    	{
    		return 0;
    	}
    
    	int hash = v % card;
    	int position = hash;
    	while (set[position] != -1)   // 这个可以避免落掉极端情况
    	{
    		if (set[position] == v)
    		{
    			set[position] = -1;
    			used--;
    			return 1;
    		}
    		position = ++position == card ? 0 : position;
    	}
    
    	return 0;
    
    	
    }
    int SET::has(int v)
    {
    	int hash = v % card;
    	int position = hash;
    	while (set[position] != -1)   // 这个可以避免落掉极端情况
    	{
    		if (set[position] == v)
    		{
    			return 1;
    		}
    		if (position == hash)
    		{
    			return 0;
    		}
    		position = ++position == card ? 0 : position;
    	}
    
    	return 0;
    
    }
    
    SET::~SET()
    {
    	for (int k = 0; k < card; k++)
    	{
    		printf("%d ", set[k]);
    	}
    	delete []set;
    }
    
    int main() { 
    	
    	SET a(3);
    	a.insert(2);
    	a.insert(8);
    	a.insert(7);
    	cout << a.remove(9) << endl;
    	
    	return 0;
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
  • 相关阅读:
    什么是 Potatoz NFT 系列?
    数据结构——第一章 算法
    避雷器雷击计数器检验
    Python 既是解释型语言,也是编译型语言
    XLua中lua读写cs对象的原理
    python各种编辑器、APP、软件下载
    Lane Detection Model Based on Spatio-TemporalNetwork with Double ConvGRUs
    Dbeaver 启动提示 Java 版本过低,和 arduino 的 java 依赖发生冲突,java 版本管理
    顺序表初始化代码问题
    开发工具介绍
  • 原文地址:https://blog.csdn.net/wniuniu_/article/details/133500279