数据结构实验三

来源:互联网 发布:大数据集成技术 编辑:程序博客网 时间:2024/06/05 07:32

一、实验目的

巩固线性表的数据结构,学会线性表的应用。
1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。
2.学习运用线性表的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。

二、实验内容

1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
1)用顺序表来实现
头文件mark.h代码:

#include<iostream>using namespace std;const int maxsize = 100000;int num = 0;class mark{public:    mark(){ length = 0; }    mark(float a[], int n);    ~mark(){}    float get(int i);    int locate(float x);    void insert(int i, float x);    float Delete(int i);    int Length(){ return length; }    void printlist();private:    float data[maxsize];    int length;};mark::mark(float a[], int n){    if (n> maxsize)throw"参数非法";    for (int i = 0; i < n; i++)        data[i] = a[i];    length=n;}float mark::get(int i){    if (i>length || i<1)throw"参数非法";    return data[i-1];}int mark::locate(float x){    for (int i = 0; i < length; i++)    if (data[i] == x)return i + 1;    return 0;}void mark::insert(int i, float x){    if (length >= maxsize)throw"上溢";    if (i<1 || i>length + 1)throw"参数非法";    for (int j = length; j>i - 1; j--)        data[j] = data[j - 1];    data[i - 1] = x;    length++;}float mark::Delete(int i){    int x;    if (length == 0)throw"下溢";    if (i<1 || i>length)throw"参数非法";    for (int j = i-1; j < length; j++)    {        x = data[i - 1];        data[j] = data[j+1];    }        length--;    return x;}void mark::printlist(){    for (int i = 0; i < length; i++)        cout<<"the mark of no."<<i+1<<" is:" << data[i]<<endl;}

源文件main.cpp代码:

#include<iostream>#include"mark.h"using namespace std;int main(){    int choice,num;    float score[maxsize];    cout << "welcome to the system of mark!" << endl;    cout << "please input the number of student you want to input:";    cin >> num;    cout << endl;    for (int i = 1; i <= num; i++)    {        cout << "please input the mark of NO." << i << "student:";        cin >> score[i - 1];    }    system("cls");    mark m(score, num);    do{        cout << "1-search" << endl;        cout << "2-insert" << endl;        cout << "3-the number of student" << endl;        cout << "4-delete" << endl;        cout << "5-printlist" << endl;        cout << "6-exit" << endl;        cout << "please choose the number of function : ";        cin >> choice; cout << endl;        system("cls");        switch (choice)        {        case 1:            int choice2, k; float x1;            do{                cout << "1-search by num" << endl;                cout << "2-search by mark" << endl;                cout << "please choose the number of function: ";                cin >> choice2; cout << endl;                system("cls");                if (choice2 == 1)                {                    cout << "please input the num you want to search:";                    cin >> k; cout << endl;                    cout << "the mark of no." << k << ":"<<m.get(k);                    cout << endl;                }                else if (choice2 == 2)                {                    cout << "please input the mark you want to search:";                    cin >> x1; cout << endl;                    cout << "the mark of no" << m.locate(x1) << ":" << x1 << endl;                }                else cout << "something you input is error,please input again!" << endl;            } while (choice2 != 1 && choice2 != 2);            system("pause");            break;        case 2:            int k1; float x;            cout << "please input the location you want to insert:";            cin >> k1;            cout << endl << "please input the mark you want to insert:";            cin >> x;            cout << endl;            m.insert(k1, x);            system("pause");            break;        case 3:cout << "the num of the students:" << m.Length() << endl;            system("pause");            break;        case 4:            int k2;            cout << "please input the num of student you want to delete:";            cin >> k2;            cout << endl;            cout << "the mark of delete is" << m.Delete(k2) << endl;            system("pause");            break;        case 5:cout << "printlist:" << endl;            m.printlist();            cout << endl;            system("pause");            break;        case 6:return 0;        default:            cout << "your input is error,please choose again!" << endl;        }        system("cls");    } while (1);    return 0;}

运行结果如下:

录入学生信息:
这里写图片描述
进入学生成绩系统主页:
这里写图片描述
查询信息:
这里写图片描述
按学号查询成绩:
这里写图片描述
按成绩查询学号:
这里写图片描述
录入学生信息:
这里写图片描述
系统内学生信息的数量:
这里写图片描述
删除学生信息:
这里写图片描述
删除信息前的数据遍历:
这里写图片描述
删除信息后的数据遍历:
这里写图片描述

2)用单链表来实现
头文件mark.h代码:

#include<iostream>using namespace std;const unsigned int maxsize = 10000;int count = 0;struct Node{    float data;    Node *next;};class mark{public:    mark();    mark(float a[], int n);    ~mark();    int length();    float get(int i);    int locate(float x);    void insert(int i, float x);    float Delete(int i);    void printlist();private:    Node *first, *p, *s;};mark::mark(){    first = new Node;    first->next = NULL;}mark::mark(float a[], int n){    first = new Node;    p = first;    p->next = NULL;    for (int i = 0; i < n; i++)    {        s = new Node;        s->data = a[i];        s->next = p->next;        p->next = s;    }}mark::~mark(){    while (first != NULL)    {        s = first;        first = first->next;        delete s;    }}int mark::length(){    int num = 0;    p = first->next;    while (p != NULL)    {        p = p->next;        num++;    }    return num;}float mark::get(int i){    p = first->next;    for (int j = 0; p != NULL&&j < i; j++)        p = p->next;    if (p == NULL)throw"位置";    return p->data;}int mark::locate(float x){    p = first->next;    for (int j = 1; p != NULL; j++)    if (p->data == x)return j;    return 0;}void mark::insert(int i, float x){    p = first->next;    for (int j = 1; j < i - 1 && p != NULL; j++)        p = p->next;    if (p == NULL)throw"位置";    else    {        s = new Node;        s->data = x;        s->next = p->next;        p->next = s;    }}float mark::Delete(int i){    float x;    p = first;    int j = 0;    while (p != NULL&&j < (i - 1))    {        p = p->next;        j++;    }    if (p == NULL)throw"位置";    else    {        s = p->next;        x = s->data;        p->next = s->next;        delete s;        return x;     }}void mark::printlist(){    int i = 1;    p = first->next;    while (p != NULL)    {        cout <<"the mark of no. "<<i<<" is:"<< p->data << endl;        p = p->next;        i++;    }}

源文件main.cpp代码:

#include<iostream>#include"mark.h"using namespace std;int main(){    int choice, count;    float score[maxsize];    cout << "welcome to the system of mark!" << endl;    cout << "please input the number of student you want to input:";    cin >> count;    cout << endl;    system("cls");    for (int i = 1; i <= count; i++)    {        cout << "please input the mark of NO." << i << "student:";        cin >> score[i - 1];    }    system("cls");    mark m(score, count);    do{        cout << "1-search" << endl;        cout << "2-insert" << endl;        cout << "3-the number of student" << endl;        cout << "4-delete" << endl;        cout << "5-printlist" << endl;        cout << "6-exit" << endl;        cout << "please choose the number of function : ";        cin >> choice; cout << endl;        system("cls");        switch (choice)        {        case 1:            int choice2, k; float x1;            do{                cout << "1-search by num" << endl;                cout << "2-search by mark" << endl;                cout << "please choose the number of function: ";                cin >> choice2; cout << endl;                system("cls");                if (choice2 == 1)                {                    cout << "please input the num you want to search:";                    cin >> k; cout << endl;                    cout << "the mark of no." << k << ":" << m.get(k) << endl;                }                else if (choice2 == 2)                {                    cout << "please input the mark you want to search:";                    cin >> x1; cout << endl;                    cout << "the mark of no" << m.locate(x1) << ":" << x1 << endl;                }                else cout << "something you input is error,please input again!" << endl;            } while (choice2 != 1 && choice2 != 2);            system("pause");            break;        case 2:            int k1; float x2;            cout << "please input the location you want to insert:";            cin >> k1;            cout << endl << "please input the mark you want to insert:";            cin >> x2;            cout << endl;            m.insert(k1, x2);            system("pause");            break;        case 3:cout << "the num of the students:" << m.length() << endl;            system("pause");            break;        case 4:            int k2;            cout << "please input the num of student you want to delete:";            cin >> k2;            cout << endl;            cout << "the mark of delete is" << m.Delete(k2) << endl;            system("pause");            break;        case 5:cout << "printlist:" << endl;            m.printlist();            cout << endl;            system("pause");            break;        case 6:return 0;        default:            cout << "your input is error,please choose again!" << endl;        }        system("cls");    } while (1);    return 0;}

运行效果如下:
初始化学生成绩系统:
这里写图片描述
这里写图片描述

进入学生成绩系统菜单:
这里写图片描述

信息检索功能:
这里写图片描述
这里写图片描述

信息插入功能:
这里写图片描述
这里写图片描述

信息删除功能:
这里写图片描述

信息遍历功能:
这里写图片描述

三、总结

1.像取出线性表中第i个元素这样的按位置随机访问的操作,使用顺序表更快一些
2.在线性表中进行插入和删除操作,使用单链表更好一些,不需要移动元素
3.顺序表中每个结点只存放数据元素,而单链表的每个结点除了存放数据元素,还要存储知识元素之间逻辑关系的指针,数据域占据的空间较小,顺序表的存储空间利用率较高
4.顺序表需要预分配一定长度的存储空间,而单链表不需要为其预分配空间,只要有内存空间可以分配,单链表中的元素个数没有限制

0 0
原创粉丝点击