构造Student类,实现HashMap类,HashMap的数据结构为 数组+ 链表


编程实现:

a.请依照数据构造Student类

b.实现HashMap类,HashMap的数据结构为 数组+ 链表

Hash算法: 学号的个位为hash值

int Hashcode(int regID);

类方法:

1)根据学号查询,参数为int regID, 返回为Student对象

Student* Get(int id);

2)新增,参数为Student对象,返回值为true/false

bool Put(Student *stu);

c.读取数据文件,格式化为Student对象,并添加到HashMap里

d.输入学号,查询该学生信息




数据文件内容(姓名,学号,成绩):

郑英,20216793,94

尹超,20196587,79

吴霞,20228110,92

贺勇,20206374,73

蔡静,20216559,68

袁娜,20194393,67

范洋,20217574,97

曾艳,20208340,92

姜强,20224847,90

段娟,20196621,63

萧艳,20217718,76

段平,20200305,69

侯丽,20226277,68

蒋军,20214391,82

李艳,20229130,85

邹洋,20194265,66

金艳,20207604,64

傅涛,20204410,64

萧敏,20214338,87
#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;

// 学生类
class Student {
public:
    string name;
    int regID;
    int score;

    Student(string n, int id, int s) {
        name = n;
        regID = id;
        score = s;
    }
};

// HashMap类
class HashMap {
private:
    static const int SIZE = 10; // HashMap大小 使用学号(regID)的个位数字作为哈希值,哈希值为 0-9
    list<Student*> table[SIZE]; // 数组+链表实现

    // Hash算法:学号的个位为hash值
    int Hashcode(int regID) {
        return regID % 10;
    }

public:
    // 根据学号查询学生对象
    Student* Get(int id) {
        int index = Hashcode(id);
        for (auto it = table[index].begin(); it != table[index].end(); ++it) {
            if ((*it)->regID == id)
                return *it;
        }
        return nullptr;
    }

    // 新增学生对象
    bool Put(Student* stu) {
        int index = Hashcode(stu->regID);
        table[index].push_back(stu);
        return true;
    }
};

int main() {
    HashMap hashMap;

    // 读取数据文件
    ifstream file("data.txt");
    if (!file) {
        cout << "无法打开数据文件" << endl;
        return 0;
    }

    // 格式化文件内容为Student对象,并添加到HashMap里
    string line;
    while (getline(file, line)) {
        string name, regIDStr, scoreStr;
        name = line.substr(0, line.find(','));
        line = line.substr(line.find(',') + 1);
        regIDStr = line.substr(0, line.find(','));
        line = line.substr(line.find(',') + 1);
        scoreStr = line;

        int regID = stoi(regIDStr);
        int score = stoi(scoreStr);

        Student* stu = new Student(name, regID, score);
        hashMap.Put(stu);
    }

    // 输入学号,查询学生信息
    int searchID;
    cout << "请输入学号:";
    cin >> searchID;

    Student* result = hashMap.Get(searchID);
    if (result != nullptr) {
        cout << "姓名:" << result->name << endl;
        cout << "学号:" << result->regID << endl;
        cout << "成绩:" << result->score << endl;
    } else {
        cout << "未找到该学生信息" << endl;
    }

    return 0;
}

for (auto it = table[index].begin(); it != table[index].end(); ++it)

it 的具体类型是 list<Student*>::iterator。这意味着 it 是一个指向 list<Student*> 容器中元素的迭代器。

list<Student*>::iterator 是 std::list 容器中的迭代器类型。它是一个用于遍历 list<Student*> 容器中元素的特殊类型。

使用 it 迭代器,您可以通过解引用操作符 * 来访问 list<Student*> 容器中的元素。例如,(*it)->regID 可以用来访问 it 指向的学生对象的 regID 属性。

需要注意的是,迭代器类型取决于容器的类型和实现方式。在这个特定的代码示例中,我们假设 table 是一个包含 std::list<Student*> 元素的数组,因此迭代器的类型是 list<Student*>::iterator。但如果 table 是其他类型的容器或者使用不同的实现方式,迭代器的类型可能会有所不同。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

航的:

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;

class Student {
public:
	string name;
	int id;
	int score;
	Student* next;
	Student(string name, int id, int score) :name(name), id(id), score(score), next(nullptr) {}
	~Student() {}
};

class HashMap {
public:
	Student* s[10];

	HashMap() {
		for (int i = 0; i < 10; i++) {
			s[i] = nullptr;
		}
	}
	int Hashcode(int regID);
	Student* Get(int id);
	bool Put(Student* stu);
};

int HashMap::Hashcode(int regID) {
	return regID % 10;
}

Student* HashMap::Get(int id) {
	int index = Hashcode(id);
	Student* tmp = s[index];
	while (tmp != nullptr) {
		if (tmp->id != id) {
			tmp = tmp->next;
		}
		else {
			break;
		}
	}
	return tmp;
}

bool HashMap::Put(Student* stu) {
	int index = Hashcode(stu->id);
	if (s[index] == nullptr) {
		s[index] = stu;
	}
	else {
		Student* tmp = s[index];
		while (tmp->next != nullptr) {
			if (stu->id == tmp->id) {
				return false;
			}
			else {
				tmp = tmp->next;
			}
		}
		tmp->next = stu;
	}
	
	//cout << "Put!!!!" << endl;
	return true;
}

int main() {
	string filename = "D:\\360MoveData\\Users\\JXWH\\Desktop\\student.txt";
	HashMap hashmap;
	//cin >> filename;
	vector<Student*> stuVec;
	ifstream file(filename);
	if (file.is_open()) {
		string line;
		while (getline(file, line)) {
			stringstream ss(line);
			string name, regIDStr, scoreStr;
			getline(ss, name, ',');
			getline(ss, regIDStr, ',');
			getline(ss, scoreStr, '\0');

			int regID = stoi(regIDStr);
			int score = stoi(scoreStr);

			Student* tmp = new Student(name, regID, score);
			stuVec.push_back(tmp);

			hashmap.Put(tmp);
		}
		file.close();
	}
	int id;
	cout << "请输入学号:" << end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值