📜  使用链接列表的学生记录管理系统

📅  最后修改于: 2021-05-30 13:56:25             🧑  作者: Mango

先决条件:链接列表

问题:创建一个可以执行以下操作的学生记录管理系统:

  • 插入学生记录
  • 删除学生记录
  • 显示学生记录
  • 搜索学生记录

学生记录应包含以下内容

  • 学生的名字
  • 学生卷数
  • 招收学生的课程
  • 学生总成绩

方法:利用对链表操作的基本知识,例如插入,删除链表中的元素,可以创建学生记录管理系统。以下是要实现的功能说明:

  • Check Record(检查记录):这是一项实用函数,用于创建在插入之前检查记录是否已经存在的检查记录。它使用检查链表中具有给定数据的节点的概念。
  • 创建记录:就像在“空链接”列表中创建新节点或在非“空”链接列表中插入新节点一样简单。
  • 搜索记录:搜索记录类似于在链接列表中搜索键。学生记录键中的是卷号,因为卷号对于每个学生都是唯一的。
  • 删除记录:删除记录类似于从链接列表中删除键。这里的关键是卷号。 Delete record是一个整数返回函数,如果未找到具有给定卷号的记录,则返回-1 ,否则删除具有给定键的节点并返回0
  • 显示记录:它显示记录类似于打印“链接”列表的所有元素。

异常处理

尽管异常处理的实现非常简单,但是在设计这样的系统之前,必须考虑以下几点:

  • 卷号必须用作区分两个不同记录的,因此在插入记录时,请检查该记录是否已存在于我们的数据库中,如果已经存在,则立即向用户报告该记录已存在并将该记录插入数据库中。
  • 记录应按排序顺序插入,以使卷号成为键,并使用排序后的链表中的插入节点。

下面是上述方法的实现:

// C++ program for the above approach
#include 
using namespace std;
  
// Node Class
class Node {
public:
    int roll;
    string Name;
    string Dept;
    int Marks;
    Node* next;
};
  
// Stores the head of the Linked List
Node* head = new Node();
  
// Check Function to check that if
// Record Already Exist or Not
bool check(int x)
{
    // Base Case
    if (head == NULL)
        return false;
  
    Node* t = new Node;
    t = head;
  
    // Traverse the Linked List
    while (t != NULL) {
        if (t->roll == x)
            return true;
        t = t->next;
    }
  
    return false;
}
  
// Function to insert the record
void Insert_Record(int roll, string Name,
                   string Dept, int Marks)
{
    // if Record Already Exist
    if (check(roll)) {
        cout << "Student with this "
             << "record Already Exists\n";
        return;
    }
  
    // Create new Node to Insert Record
    Node* t = new Node();
    t->roll = roll;
    t->Name = Name;
    t->Dept = Dept;
    t->Marks = Marks;
    t->next = NULL;
  
    // Insert at Begin
    if (head == NULL
        || (head->roll >= t->roll)) {
        t->next = head;
        head = t;
    }
  
    // Insert at middle or End
    else {
        Node* c = head;
        while (c->next != NULL
               && c->next->roll < t->roll) {
            c = c->next;
        }
        t->next = c->next;
        c->next = t;
    }
  
    cout << "Record Inserted "
         << "Successfully\n";
}
  
// Function to search record for any
// students Record with roll number
void Search_Record(int roll)
{
    // if head is NULL
    if (!head) {
        cout << "No such Record "
             << "Avialable\n";
        return;
    }
  
    // Otherwise
    else {
        Node* p = head;
        while (p) {
            if (p->roll == roll) {
                cout << "Roll Nmuber\t"
                     << p->roll << endl;
                cout << "Name\t\t"
                     << p->Name << endl;
                cout << "Department\t"
                     << p->Dept << endl;
                cout << "Marks\t\t"
                     << p->Marks << endl;
                return;
            }
            p = p->next;
        }
  
        if (p == NULL)
            cout << "No such Record "
                 << "Avialable\n";
    }
}
  
// Function to delete record students
// record with given roll number
// if it exist
int Delete_Record(int roll)
{
    Node* t = head;
    Node* p = NULL;
  
    // Deletion at Begin
    if (t != NULL
        && t->roll == roll) {
        head = t->next;
        delete t;
  
        cout << "Record Deleted "
             << "Successfully\n";
        return 0;
    }
  
    // Deletion Other than Begin
    while (t != NULL && t->roll != roll) {
        p = t;
        t = t->next;
    }
    if (t == NULL) {
        cout << "Record does not Exist\n";
        return -1;
        p->next = t->next;
  
        delete t;
        cout << "Record Deleted "
             << "Successfully\n";
  
        return 0;
    }
}
  
// Function to display the Student's
// Record
void Show_Record()
{
    Node* p = head;
    if (p == NULL) {
        cout << "No Record "
             << "Available\n";
    }
    else {
        cout << "Index\tName\tCourse"
             << "\tMarks\n";
  
        // Until p is not NULL
        while (p != NULL) {
            cout << p->roll << "    \t"
                 << p->Name << "\t"
                 << p->Dept << "\t"
                 << p->Marks << endl;
            p = p->next;
        }
    }
}
  
// Driver code
int main()
{
    head = NULL;
    string Name, Course;
    int Roll, Marks;
  
    // Menu-driven program
    while (true) {
        cout << "\n\t\tWelcome to Student Record "
                "Management System\n\n\tPress\n\t1 to "
                "create a new Record\n\t2 to delete a "
                "student record\n\t3 to Search a Student "
                "Record\n\t4 to view all students "
                "record\n\t5 to Exit\n";
        cout << "\nEnter your Choice\n";
        int Choice;
  
        // Enter Choice
        cin >> Choice;
        if (Choice == 1) {
            cout << "Enter Name of Student\n";
            cin >> Name;
            cout << "Enter Roll Number of Student\n";
            cin >> Roll;
            cout << "Enter Course of Student \n";
            cin >> Course;
            cout << "Enter Total Marks of Student\n";
            cin >> Marks;
            Insert_Record(Roll, Name, Course, Marks);
        }
        else if (Choice == 2) {
            cout << "Enter Roll Number of Student whose "
                    "record is to be deleted\n";
            cin >> Roll;
            Delete_Record(Roll);
        }
        else if (Choice == 3) {
            cout << "Enter Roll Number of Student whose "
                    "record you want to Search\n";
            cin >> Roll;
            Search_Record(Roll);
        }
        else if (Choice == 4) {
            Show_Record();
        }
        else if (Choice == 5) {
            exit(0);
        }
        else {
            cout << "Invalid Choice "
                 << "Try Again\n";
        }
    }
    return 0;
}


输出:以下是学生记录管理系统提供的各种功能的输出的屏幕截图:

  • 创建记录:
  • 显示记录:
  • 删除记录:
  • 搜索记录:
  • 学生记录:
想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”