數據結構學生成績管理系統

在現代教育中,學生成績的管理已經成為了一個不可或缺的部分。藉助數據結構,一個高效、可靠的學生成績管理系統可以被輕鬆實現。

一、數據結構的選擇

在構建學生成績管理系統時,選擇合適的數據結構尤為重要。為了方便管理,我們可以選擇使用哈希表來存儲學生信息、課程信息和成績信息。哈希表又可以分為兩種類型:基於鏈接和基於線性探測的哈希表。基於鏈接的哈希表使查找和插入操作更為高效,而基於線性探測的哈希表則更適合內存佔用較小的場景。

以下是基於鏈接的哈希表的示例代碼:

class HashTable {
  private:
    vector<LinkedList> table;
    int size;
    int capacity;
  
  public:
    HashTable(int capacity) {
        this->table.assign(capacity, LinkedList());
        this->size = 0;
        this->capacity = capacity;
    }

    void insert(string key, int value) {
        int hashValue = hash(key);
        LinkedList& list = table[hashValue];
        if (list.insert(key, value)) {
            size++;
        }
    }

    int get(string key) {
        int hashValue = hash(key);
        LinkedList& list = table[hashValue];
        return list.get(key);
    }

    bool remove(string key) {
        int hashValue = hash(key);
        LinkedList& list = table[hashValue];
        if (list.remove(key)) {
            size--;
            return true;
        }
        return false;
    }

    int getSize() const {
        return size;
    }

    int getCapacity() const {
        return capacity;
    }

  private:
    int hash(string key) const {
        const int P = 31;
        const int M = capacity;
        int hashValue = 0;
        int pPow = 1;
        for (int i = 0; i < key.length(); i++) {
            hashValue = (hashValue + (key[i] - 'a' + 1) * pPow) % M;
            pPow = (pPow * P) % M;
        }
        return hashValue;
    }
};

以上代碼實現了一個基於鏈接的哈希表,其中用到了另一個數據結構——鏈表,用於解決哈希衝突的問題。

二、業務邏輯實現

在硬件和數據結構的基礎上,學生成績管理系統還需要實現各種業務邏輯,如插入成績、查詢成績、刪除成績等功能。以下是一些常見的功能實現代碼:

1. 插入成績

void insertGrade(HashTable& table, string studentId, string courseId, int grade) {
    string key = studentId + ":" + courseId;
    table.insert(key, grade);
}

2. 查詢成績

int getGrade(HashTable& table, string studentId, string courseId) {
    string key = studentId + ":" + courseId;
    return table.get(key);
}

3. 刪除成績

bool removeGrade(HashTable& table, string studentId, string courseId) {
    string key = studentId + ":" + courseId;
    return table.remove(key);
}

三、可拓展性考慮

在實現數據結構時,需要考慮系統的可拓展性。在學生數量增加或者新的課程加入時,系統應該能夠自動進行擴容,而不影響原有數據。以下是一個哈希表的自動擴容實現:

class HashTable {
  private:
    vector<LinkedList> table;
    int size;
    int capacity;
  
  public:
    HashTable(int capacity) {
        this->table.assign(capacity, LinkedList());
        this->size = 0;
        this->capacity = capacity;
    }

    void insert(string key, int value) {
        int hashValue = hash(key);
        LinkedList& list = table[hashValue];
        if (list.insert(key, value)) {
            size++;
        }
        if (size >= capacity) {
            resize();
        }
    }

    int get(string key) {
        int hashValue = hash(key);
        LinkedList& list = table[hashValue];
        return list.get(key);
    }

    bool remove(string key) {
        int hashValue = hash(key);
        LinkedList& list = table[hashValue];
        if (list.remove(key)) {
            size--;
            if (size < capacity / 2) {
                resize();
            }
            return true;
        }
        return false;
    }

    int getSize() const {
        return size;
    }

    int getCapacity() const {
        return capacity;
    }

  private:
    int hash(string key) const {
        const int P = 31;
        const int M = capacity;
        int hashValue = 0;
        int pPow = 1;
        for (int i = 0; i < key.length(); i++) {
            hashValue = (hashValue + (key[i] - 'a' + 1) * pPow) % M;
            pPow = (pPow * P) % M;
        }
        return hashValue;
    }

    void resize() {
        int newCapacity = capacity * 2;
        vector<LinkedList> newTable(newCapacity);
        for (int i = 0; i < capacity; i++) {
            LinkedList& list = table[i];
            Node* node = list.getFirstNode();
            while (node != nullptr) {
                int hashValue = hash(node->key);
                LinkedList& newList = newTable[hashValue];
                newList.insert(node->key, node->value);
                node = node->next;
            }
        }
        capacity = newCapacity;
        table = newTable;
    }
};

以上代碼實現了哈希表的自動擴容功能,當哈希表中的元素數量超過容量時,會將哈希表的容量擴大為原來的兩倍。

原創文章,作者:KYZYD,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/375111.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
KYZYD的頭像KYZYD
上一篇 2025-04-29 12:49
下一篇 2025-04-29 12:49

相關推薦

  • 數據結構與算法基礎青島大學PPT解析

    本文將從多個方面對數據結構與算法基礎青島大學PPT進行詳細的闡述,包括數據類型、集合類型、排序算法、字符串匹配和動態規劃等內容。通過對這些內容的解析,讀者可以更好地了解數據結構與算…

    編程 2025-04-29
  • 使用Python對學生分數進行等級輸出

    本文將介紹如何使用Python編寫程序,實現輸入學生分數,輸出成績等級的功能。通過本文的學習,您將深入了解Python的相關知識,同時也能夠掌握如何使用Python進行編程。 一、…

    編程 2025-04-28
  • 用Python字典統計學生成績

    學生成績是評價學生學習成果的重要指標,利用Python語言統計學生成績是Python應用的重要實戰,本文將從多個方面詳細闡述如何用Python字典統計學生成績。 一、創建學生成績字…

    編程 2025-04-27
  • Python如何計算學生成績

    Python是一種多用途、強大的編程語言,它可以被用於各種不同的應用程序。在教育領域,Python通常用於計算學生成績。在本文中,我們將介紹Python如何計算學生成績,以及如何使…

    編程 2025-04-27
  • Python方陣:一種便捷高效的數據結構

    Python方陣是一種非常流行的數據結構,它在各種應用場景中得到了廣泛的應用和發展。本文將從多個方面介紹Python方陣的優點、用法和實現方法,供讀者參考。 一、Python方陣的…

    編程 2025-04-27
  • 學生成績查詢系統

    本文將從多個方面對學生成績查詢系統做詳細的闡述,涉及設計、開發和維護等方面。 一、需求分析 在設計和開發一個學生成績查詢系統之前,需要先進行需求分析。主要包括以下方面: 1、系統用…

    編程 2025-04-27
  • 學生html美食靜態網頁代碼評析

    一、HTML結構 <html> <head> <title>學生美食家</title> <link rel=”styleshe…

    編程 2025-04-23
  • MySQL 數據結構的詳細闡述

    一、存儲引擎 MySQL 數據庫使用不同的存儲引擎來支持不同的需求,如性能、事務支持、並發性等。目前,MySQL 支持的存儲引擎有 MyISAM、InnoDB、Memory、CSV…

    編程 2025-04-23
  • 騰訊雲學生服務器教程

    騰訊雲是很多開發者和個體用戶都熟知的雲計算服務品牌,它提供了豐富的雲服務器,數據庫,存儲,安全,人工智能等服務,其中學生服務器是騰訊雲面向高校學生推出的雲服務器產品,它不僅支持完全…

    編程 2025-04-22
  • MySQL底層數據結構詳解

    一、B+樹索引 1、B+樹是一種平衡樹,它是一種多路查找樹,每個節點可以存儲多個索引值和相應數據的地址。MySQL使用B+樹作為索引結構,B+樹的優勢在於磁盤I/O瓶頸的優化,它的…

    編程 2025-04-18

發表回復

登錄後才能評論