静态链表的基本操作及其应用(实验2.3)

一.实验目的

   巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

二.  实验内容

1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

#include <iostream>  
#include<string>  
using namespace std;  
const int MaxSize = 10;  
  
struct Student      //定义Student结构体  
{  
    string name;  
    int Chinese;  
    int Math;  
    int English;  
    int Sum;  
};  
ostream& operator << (ostream& os, const Student &ob) //重载左移运算符,使其能直接输出自定义类型“Student”  
{  
    os << ob.name << "\t";  
    os << ob.Chinese << "\t";  
    os << ob.Math << "\t";  
    os << ob.English << "\t";  
    os << ob.Sum << "\t";  
    return os;  
}  
template <class DataType>  
struct SNode  
{  
    DataType data;  
    int next;   //指针域(也称游标)  
 };  
  
template <class DataType>  
class StaticLink  
{  
public:  
      
    StaticLink(DataType a[], int n);  
    ~StaticLink();  
    int Length();  
    DataType Get(int index);  
    int Locate(string x);  
    void Insert(DataType x, int index);  
    DataType Delete(int i);  
    void PrintList();  
  
private:  
    int  first;  // 定义头指针  
    int  avail;  
    SNode<Student> SList[MaxSize];        //定义结点数组  
    int length;  
  
};  
  
template <class DataType>  
void StaticLink<DataType> ::PrintList()  
{  
    int s = first;  
    cout << "-------------------------------------" << endl;  
    cout << "姓名" << "\t" << "语文" << "\t" << "数学" << "\t" << "英语" << "\t" << "总分" << "\t" << endl;  
    for (int i = 0; i <length; i++)  
    {     
        s = SList[s].next;  
        cout << SList[s].data  << endl;   
    }  
    cout << "-------------------------------------" << endl;  
  
}  
  
template <class DataType>  
int StaticLink<DataType>::Length()  
{  
    return length;  
}  
template <class DataType>  
DataType StaticLink<DataType>::Get(int index)  
{  
    int s = first;  
    for (int i= 0; i <index; i++)  
    {  
        s = SList[s].next;  
    }  
    return SList[s].data;  
      
}  
  
template <class DataType>  
int StaticLink<DataType>::Locate(string x)  
{  
    int i = 1;  
    int s = first;  
    for (; i < length; i++)  
    {  
        s = SList[s].next;  
        if (SList[s].data.name == x)  
            break;  
    }  
    return i;  
}  
  
template <class DataType>  
void StaticLink<DataType>::Insert(DataType x, int index)  
{  
  
    int s = first;  
    for (int i = 1; i <index; i++)  
    {  
        s = SList[s].next;    
    }  
    int i = avail;  //此i变量与循环变量i无关  
    avail = SList[avail].next;  
    SList[i].data = x;  
    SList[i].next = SList[s].next;  
    SList[s].next = i;  
    SList[i].data.Sum = SList[i].data.Chinese + SList[i].data.Math + SList[i].data.English; //赋值Sum  
    length++;  
}  
  
  
template <class DataType>  
StaticLink<DataType>::StaticLink(DataType a[], int n)  
{     
    avail = 1; first = 1; length = 0;     
    for (int i = 0; i < MaxSize; i++)        //初始化空间  
    {  
        SList[i].next= i + 1;  
    }  
    SList[MaxSize].next = -1;  
    for (int i = n-1; i >=0; i--)    //逆向赋值,避免逆序输入  
    {     
        int s = avail;  
        avail = SList[avail].next;  
        SList[s].data = a[i];  
        SList[s].next = SList[first].next;  
        SList[first].next = s;  
        length++;  
    }             
}  
  
  
template <class DataType>  
DataType StaticLink<DataType>::Delete(int i)  
{  
    int q = 1;  
    int s = first;  
    if (i<1 || i>length)      //判断输入位置是否异常  
        throw "删除位置异常";  
    else  
    {     
        for (; q < i; q++)       //省略第一个条件  
        {  
            s = SList[s].next;  
        }  
        q = SList[s].next;  
        DataType x = SList[q].data;  
        SList[s].next = SList[q].next;  
        SList[q].next = avail;  
        avail = q;  
        length--;  
        return x;  
    }  
  
}  
  
template <class DataType>  
StaticLink <DataType>::~StaticLink()  
{  
    length = 0;  
    cout << "链表已成功删除。" << endl;  
  
}  
int main()  
{  
  
    Student stu[MaxSize]; //最大容纳量  
    int num;  
    string TempName;  
    cout << "请输入学生人数:";  
    cin >> num;  
  
    for (int i = 0; i < num; i++)  
    {  
        cout << "输入“break”退出输入" << endl;  
        cout << "请输入第" << i + 1 << "名学生姓名:";  
        cin >> TempName;  
        if (TempName == "break")  
        {  
            num = i;  
            break;  
        }  
        stu[i].name = TempName;  
        cout << "请输入第" << i + 1 << "名学生语文成绩:";  
        cin >> stu[i].Chinese;  
        cout << "请输入第" << i + 1 << "名学生数学成绩:";  
        cin >> stu[i].Math;  
        cout << "请输入第" << i + 1 << "名学生英语成绩:";  
        cin >> stu[i].English;  
        stu[i].Sum = stu[i].Chinese + stu[i].Math + stu[i].English;  
        cout << "-------------------------------------" << endl;  
    }  
  
    StaticLink<Student> demo(stu, num);  
    demo.PrintList();  
    cout << "链表总长为:";  
    cout << demo.Length() << endl;  
    cout << "-------------------------------------" << endl;  
  
    cout << "查找第三名学生成绩:" << endl;  
    cout << "-------------------------------------" << endl;  
    cout << "姓名" << "\t" << "语文" << "\t" << "数学" << "\t" << "英语" << "\t" << "总分" << "\t" << endl;  
    cout << demo.Get(3) << endl;  
    cout << "-------------------------------------" << endl;  
  
    cout << "查找姓名为“小叶”的位置:" << demo.Locate("小叶") << endl;  
    cout << "-------------------------------------" << endl;  
  
    cout << "链表总长为:";  
    cout << demo.Length() << endl;  
    cout << "-------------------------------------" << endl;  
    cout << "删除第3位同学的成绩:" << demo.Delete(3).name<<endl;  
    demo.PrintList();  
    return 0;  
}  
输入

输出

三、实验心得

静态表和单链表十分相似,所谓是静态就是它用了一个数组去模拟了一个链表,有了单链表在插入和删除不用移动元素的优点,但是也有顺序表不能扩展的缺点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值