一、实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
#include<iostream>
using namespace std;
template <class DataType>
struct DulNode
{
DataType data;
DulNode<DataType> *prior,*next;
};
template <typename DataType>
class DUL{
public:
DUL();
DUL(DataType a[], int n); //有参构造函数
~DUL(); //析构函数
int Length(); //单链表长度
void Insert(int i, DataType x); //插入,在位置i插入元素
DataType Get(int i); //按位查找
int Locate(DataType x); //按值查找
DataType Delete(int i); //删除
void Print(); //遍历
private:
DulNode<DataType> *first; //双链表的头指针
int length; //链的长度
};
template <typename DataType>
DUL<DataType>::DUL(DataType a[], int n)
{
length=0;
first=new DulNode<DataType>; //初始化一个空链表
first->next=NULL;
first->prior=NULL;
for (int i=0;i<n;i++)
{
DulNode<DataType> *s =new DulNode<DataType>;
s->data = a[i];
s->next = first->next;
first->next = s; //将节点s插入到头节点之后
}
}
template <typename DataType>
DUL<DataType>::~DUL()
{
while (first->next!=first->prior)
{
DulNode<DataType> *temp = first; //临时指针,存储即将释放的节点的指针
first->prior->next = first->next; //脱链
first->next -> prior = first->prior;
first = first->next; //头指针后移
delete temp; //释放内存
}
delete first;
}
template<typename DataType>
int DUL<DataType>::Length()
{
DulNode<DataType> *p; int Count;
p=first->next;
Count=0;
while(p!=NULL)
{
p=p->next;
Count++;
}
return length;
}
template <typename DataType>
void DUL<DataType>::Insert(int i,DataType x)
{
DulNode<DataType>*p,*s;int Count;
p=first;
Count=0;
while(p!=NULL&&Count<i-1)
{
p=p->next;
Count++;
}
if(p==NULL) throw"位置";
else
{
s=new DulNode<DataType>;
s->data=x;
s->next=p->next;
p->next=s;
}
}
template <typename DataType>
DataType DUL<DataType>::Get(int i)
{
DulNode<DataType> *p;int Count=1;
p = first->next;
while (p != NULL&&Count<i)
{
p = p->next; Count++;
}
if (p == NULL)throw"位置非法";
else return p->data;
}
template <typename DataType>
int DUL<DataType>::Locate(DataType x)
{
DulNode<DataType> *p; int Count;
p = first->next;Count = 1;
while (p!= NULL)
{
if (p->data == x) return Count;
p = p->next;
Count++;
}
return 0;
}
template <typename DataType>
DataType DUL<DataType>::Delete(int i)
{
DulNode<DataType> *p,*q;
p = first->next; int Count,x;Count=1;
while(p!=NULL&&Count<i-1)
{
p=p->next;Count++;
}
if (p==NULL||p->next==NULL) throw"位置非法";
else
{
q = p->next;
x=q->data;
if (p->next!=NULL)
{
if(q->next!=NULL)
q->next->prior=p;
else
{
p->next=NULL;
p->next = q->next;
delete q;
q = NULL;
return x;
}
}
p->next = q->next;
delete q;
q = NULL;
return x;
}
}
template <typename DataType>
void DUL<DataType>::Print()
{
DulNode<DataType> *p;
p=first->next;
while(p->next!=NULL)
{
cout<<p->data<<""<<endl;
p=p->next;
}
}
void main()
{
float goal[10]={99.5,97,58,46.5,87,86,84.5,45,78,89.5};
DUL<float>student(goal,10);
cout<<"学生成绩如下"<<endl;
student.Print();
cout<<"在第三个位置插入0分的成绩"<<endl;
student.Insert(3,0);
cout<<"插入后的成绩表为:"<<endl;
student.Print();
cout<<"查找成绩为89.5的学生位于第"<<student.Locate(89.5)<<endl;
cout<<"查找第四个学生的成绩为:"<<student.Get(4)<<endl;
cout<<"删除第五个学生的成绩"<<endl;
student.Delete(5);
cout<<"删除后的成绩为:"<<endl;
student.Print();
}