C++实现字典数据结构

本文使用C++构建了一个字典数据结构,未使用STL,实现了一个学生成绩录入系统,进而实现了字典数据对象的如下功能:

  1. 新建一个字典;
  2. 检查字典非空;
  3. 得到字典的数据长度;
  4. 插入一个数对;
  5. 按学生姓名删除对应的字典数据;
  6. 按分数查找所有符合的学生姓名;
  7. 按姓名查找对应成绩;
  8. 遍历输出链表;

该程序使用了pair容器,整个程序基于线性表数据结构(链表描述)。由3个文件构成,头文件定义了字典类,函数定义文件定义了字典类的成员函数,测试文件测试了字典数据结构,同时对输入错误的情况进行了纠错。编译环境基于Xcode。
头文件源码如下:

//
//  dictionarycreat.hpp
//  字典数据结构
//
//  Created by lq on 2019/9/23.
//  Copyright © 2019 Mr.liang. All rights reserved.
//

#ifndef dictionarycreat_hpp
#define dictionarycreat_hpp
#include 
#include 
using namespace std;
//使用typedef简化pair类型的声明
typedef pair student;

struct node//定义链表节点
{
    node* next;
    node* last;
    student data;//data为一个pair类型的数据
};

class dictionary_creat
{
private:
    node* head;
    node* tail;
    int length;//链表长度
public:
    dictionary_creat();
    ~dictionary_creat();
    
    //检查是否为空,链表为空返回true,否则返回false
    bool empty_check();
    //返回链表长度
    int get_size();
    //插入数对(pair类型)
    void insert(student & element);
    //按姓名删除对象
    void erase(const string name);
    //按分数查找所有符合的学生姓名
    student find(const float grade);
    //按姓名查找对应成绩
    student find(const string name);
    //遍历输出链表
    void show()const;
};


#endif /* dictionarycreat_hpp */

成员函数定义文件如下:

//
//  dictionarycreat.cpp
//  字典数据结构
//
//  Created by lq on 2019/9/23.
//  Copyright © 2019 Mr.liang. All rights reserved.
//

#include "dictionarycreat.hpp"
#include 
#include 
using namespace std;

dictionary_creat::dictionary_creat()
{
    head = new node;
    tail = new node;
    head->next = tail;
    head->last = nullptr;
    tail->last = head;
    tail->next = nullptr;
    length = 0;
}

dictionary_creat::~dictionary_creat()
{
    cout<<"bye"<data = make_pair(element.first, element.second);
    tail->next = new node;
    node* p = tail;
    tail = p->next;
    tail->last = p;
    tail->next = nullptr;
    length++;
}

void dictionary_creat::erase(const string name)
{
    node* p = head->next;
    while(p!=tail)
    {
        if(!(p->data).first.compare(name))
        {
            node* temp = p->last;
            temp->next = p->next;
            (p->next)->last = temp;
            delete p;
            break;
        }
        p = p->next;
    }
    length--;
}

student dictionary_creat::find(const float grade)
{
    node* p = head->next;
    while(p!=tail)
    {
        if((p->data).second == grade)
        {
            cout<<"the student name for "<data).first;
        }
        p = p->next;
    }
    return p->data;
}


student dictionary_creat::find(const string name)
{
    node* p = head->next;
    while(p!=tail)
    {
        if(!(p->data).first.compare(name))
        {
            cout<<"the grade for "<data).second;
        }
        p = p->next;
    }
    return p->data;
}

void dictionary_creat::show()const
{
    node* p =head->next;
    while(p!=tail)
    {
        cout<<"student name: "<data.first
        <<"student grade: "<data.second<next;
    }
}

测试文件源码如下:

//
//  main.cpp
//  字典数据结构
//
//  Created by lq on 2019/9/23.
//  Copyright © 2019 Mr.liang. All rights reserved.
//

#include 
#include "dictionarycreat.hpp"
#include 

using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    dictionary_creat students;
    student stu_info;
    string stu_name;
    float stu_grade;
    int member;
    cout<<"please input sum of students: "<>member;
    for(int i = 0;i>stu_name;
        while(cin.fail())
        {
            cin.clear();
            while(cin.get()!='\n')
            {
                continue;
            }
            cout<<"please input again."<>stu_name;
        }
        cout<<"please input student grade:"<>stu_grade;
        while(cin.fail())
        {
            cin.clear();
            while(cin.get()!='\n')
            {
                continue;
            }
            cout<<"please input again."<>stu_grade;
        }
        stu_info = make_pair(stu_name, stu_grade);
        students.insert(stu_info);
    }
    students.show();
    cout<<"the result of empty check is "<

输出结果如下:

please input sum of students: 
3
round #1:
please input student name:
Bob
please input student grade:
150
round #2:
please input student name:
Mike
please input student grade:
Mike
please input again.
143
round #3:
please input student name:
Alice
please input student grade:
141
student name: Bob student grade: 150
student name: Mike student grade: 143
student name: Alice student grade: 141
the result of empty check is 0
the grade for Mike is 143student name: Bob student grade: 150
student name: Alice student grade: 141
bye
Program ended with exit code: 0

如果您想了解更多C++/Java/机器学习相关的知识,欢迎扫描下方的二维码,关注“梁公子的备忘录”,每天一篇相关的技术分享,期待与您一起学习,共同进步!

在这里插入图片描述

你可能感兴趣的:(C++高级语法,数据结构)