实验二 静态链表实现学生成绩

本文介绍了一种基于数组实现的静态链表的数据结构,并提供了一个具体的模板类实现。通过这个实现,文章展示了如何进行基本操作如插入、删除、查找等,并通过一个成绩管理系统的例子演示了其使用方法。


源代码
#include
using namespace std;

const int MaxSize = 100;     //自定义表长

template
class Node {
public:
    T data;
    int next;      //存储指向下一个节点的数组的下标
};

template
class staticLink {
public:
	staticLink();
    staticLink(T a[],int n);
    ~staticLink(){};
    int Length(){return length;}   //返回单链表的长度
    T Get(int i);                  //按位查找,查找第i个节点的元素
    int Locate(T x);               //按值查找,查找链表中第一个值为x的元素,并返回序号
    bool Insert(int i, T x);       //插入元素,在第i个位置插入值x
    bool Delete(int i);            //删除节点,删除第i个节点
    void PrintList();              //遍历节点
private:
    int first;
    int avail;
    int length;
    Node SList[MaxSize];
};

template
staticLink::staticLink() 
{
	first=0;
	avail=1;
	sList[0].next=-1;
	for(int i=1;i
staticLink::staticLink(T a[],int n) 
{
    for (int i = 0; i < MaxSize; i++)
    {
        SList[i].next =i+1;
    }
    length = 0;
    SList[MaxSize-1].next=-1;
    avail = 2;
    first = 1;
    SList[first].next = -1;            //利用头插法插入元素
    for (int j = 0; j < n;j++)         //判断链中是否已满
    {
        if (avail==-1) {   
            break;
        }
        int s = avail;                    //空链后移
        avail = SList[avail].next;        //新链上值
        SList[s].data = a[j];             //上链
        SList[s].next = SList[first].next;
        SList[first].next = s;
        length++;
    }
}

template
T staticLink::Get(int i) 
{
    if (i <= 0 || i > length) 
	{
        throw"location error";
    }
    int s = first;
    for (int j = 0; j < i; j++) 
	{
        s = SList[s].next;
    }
    return SList[s].data;
}

template
int staticLink::Locate(T x) 
{
    int count = 0;
    int s = first;
    while (count
bool staticLink::Insert(int i, T x) 
{
    if (SList[avail].next==-1) 
	{
        return false;
    }
    int s = first;
    int temp = avail;
    SList[temp].data = x;            //空链头针后移
    avail = SList[avail].next;
    int count=0;
    while (count < i-1 && count < length) 
	{
        s = SList[s].next;
        count++;
    }
    SList[temp].next = SList[s].next;
    SList[s].next = temp;
    length++;
    return true;
}
template
bool staticLink::Delete(int i) 
{
    if (i <= 0 || i > length) 
	{
        return false;
    }
    int count = 0;
    int s = first;
    while (count < i-1 && count < length) 
	{
        s = SList[s].next;
        count++;
    }
    int q = SList[s].next;
    SList[s].next = SList[q].next;
    SList[q].next = avail;
    avail = q;
    length--;
    return true;
}

template
void staticLink::PrintList() 
{
    int s = SList[first].next;
    for (int i = 1; i <= length; i++) 
	{
        cout << SList[s].data << " ";
        s = SList[s].next;
    }
}

int main() 
{
		cout<<"\t ****************学生成绩静态链表的实现**************\n";
		cout<<"\t ****************************************************\n";
		cout<<"\t *------------------------------------------*********\n";
		cout<<"\t *****************[1]——输出表长********************\n";
		cout<<"\t *****************[2]——按位查找********************\n";
		cout<<"\t *****************[3]——按值查找********************\n";
		cout<<"\t *****************[4]——插入************************\n";
		cout<<"\t *****************[5]——删除************************\n";
		cout<<"\t *****************[6]——遍历************************\n";
		cout<<"\t *****************[7]——输出主菜单******************\n";
		cout<<"\t *****************[8]——退出************************\n";
		cout<<"\t *------------------------------------------*********\n";
		cout<<"\t ****************************************************\n";
	
		int a[8] = {78,87,89,82,80,91,98,94 };
		staticLink  sList(a, 8);
		int flag,i,x,t;
		flag=0;	
		while(flag==0)
		{
			cout<<"please input the command(1~8):"<>t;
			switch(t)
			{
			case 1:			
				cout<<"the length is:"<>i;
				x=sList.Get(i);
				cout<<"the number is:"<>x;
				i=sList.Locate(x);
				cout<<"the location is:"<>i;
				cout<<"the insert number is:";
				cin>>x;
				sList.Insert(i,x);
				cout<<"insert successfully!"<>i;
				sList.Delete(i);
				cout<<"delete successfully!"<

运行结果
运行程序,进入主界面

输入1,输出表长

输入6,输出静态链表

输入2,查找第四个位置的成绩

输入3,查找成绩87所在的位置

输入4,在第四个位置插入成绩90

输入5,删除第八个位置的成绩


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值