数据结构实验(二) 静态链表

一.实验目的
     巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
 
二..实验内容
  建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
(用静态链表来实现)
#include <iostream>
using namespace std;
const int Maxsize = 100;

template < class Datatype>
struct Node
{
	Datatype data;
	int next;
};

template <class Datatype>
class Static
{
public:
	Static();
	Static(Datatype a[], int n);
	~Static() {  };
	int Length();
	Datatype Get(int i);
	int Locate(Datatype x);
	void Insert(int i, Datatype x);
	Datatype Delete(int i);
	void Printlist();
private:
	Node<Datatype> data[Maxsize];
	int avail;
	int first;
	int length;
};

template <class Datatype>
Static<Datatype>::Static()
{
	first = 0;
	avail = 1;
	data[0].next = -1;
	for (int i = 1; i < Maxsize - 1; i++)
	{
		data[i].next = i + 1;

	}
	data[Maxsize - 1] = -1;
	length = 0;
}

template <class Datatype>
Static<Datatype>::Static(Datatype a[], int n)
{
	int s;
	length = 0;
	if (n > Maxsize || n < 0)  throw "错误";
	for(int i=0;i<Maxsize;i++)
	{
		data[i].next = i + 1;
	}
	data[Maxsize].next = -1;
	first = 0;
	avail = 1;
	data[first].next = -1;
	for (int j = 0; j < n; j++)
	{
		s = avail;
		avail = data[avail].next;
		data[s].data = a[j];
		data[s].next = data[first].next;
	    data[first].next = s;
		length++;
	}
}

template <class Datatype>
int Static<Datatype>::Length()
{
	return length;
}

template <class Datatype>
Datatype Static<Datatype>::Get(int i)
{
	int p = first;
	if (i<0 || i>length) throw"错误";
	for (int j = 1; j <= i; j++)
	{
		if (data[p].next == -1)  throw "错误";
		p = data[p].next;
	}
	return data[p].data;
}

template <class Datatype>
int Static<Datatype>::Locate(Datatype x)
{
	int p = first;
	int count = 0;
	while (data[p].next != -1)
	{
		if (data[p].data == x) { return count; }
		p = data[p].next;
		count++;
	}
	return 0;
}

template <class Datatype>
void Static<Datatype>::Insert(int i, Datatype x)
{
	if (i<0 || i>Maxsize) throw "位置";
	int p = first;
	int s = avail;
	data[s].data = x;
	avail = data[s].next;
	int count = 0;
	for (count  = 0; count < i - 1; count++)
	{
		p = data[p].next;
	}
	length++;
	data[s].next = data[p].next;
	data[p].next = s;
}

template <class Datatype>
Datatype Static<Datatype>::Delete(int i)
{
	if (i<0 || i>length) throw "位置";
	int p = first;
	int count = 0;
	while (data[p].next != -1 && count < i - 1)
	{
		p = data[p].next;
		count++;
	}
	int q = data[p].next;
	Datatype x = data[q].data;
	data[p].next = data[q].next;
	data[q].next = avail;
	avail = q;
	length--;
	return x;
}

template <class Datatype>
void Static<Datatype>::Printlist()
{
	int p = data[first].next;
	while (p!=-1)
	{
		cout << data[p].data << "  ";
		p = data[p].next;
	}
}

int main()
{
	int b[10] = { 100,95,90,85,80,75,70,65,60,55 };
	Static<int>c(b, 10);
	cout << "显示所有学生的成绩" << endl;
	c.Printlist();
	cout << endl;
	cout << "学生总数为:" << c.Length() << endl;
	cout << "查找的第六位学生的成绩为:" << endl;
	cout << c.Get(6) << endl;
	cout << "查找成绩为90的学生:" << endl;
	cout << c.Locate(90) << endl;
	cout << "在第五个学生前插入成绩为73的学生" << endl;
	 c.Insert(5, 73);
	cout << "插入后显示所有学生成绩" << endl;
	c.Printlist();
	cout << endl;
	cout << "删除第一个学生成绩后剩下学生的成绩为:" << endl;
	c.Delete(1);
	c.Printlist();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值