数据结构实验三 单链表实现学生成绩
1.单链表实现学生成绩
源代码:
- #include
- using namespace std;
- template
- struct Node
- {
- T data;
- Node *next;
- };
- template
- class Score
- {
- public:
- Score(); //无参构造函数,建立只有头结点的空链表
- Score(T a[],int n); //有参构造函数,建立有n个元素的单链表
- ~Score(); //析构函数
- int Length(); //求单链表的长度
- T Get(int i); //按位置查找,在单链表中查找第i个结点的元素值
- int Locate(T x); //按值查找,在单链表中查找值为x的元素序号
- void Insert(int i,T x); //在第i个位置插入元素值为x的结点
- T Delete(int i); //在单链表中删除第i个结点
- void PrintList();
- private:
- Node *first;
- };
- template
- Score::Score()
- {
- first=new Node; //生成头结点
- first->next=NULL; //头结点的指针域置空
- }
- template //尾插法建立单链表
- Score::Score(T a[],int n)
- {
- Node *r,*s;
- first=new Node;
- r=first; //生成头结点
- for(int i=0;i;s->data=a[i]; //为每个数组元素建立一个结点
- r->next=s;r=s; //将结点S插入到终端结点之后
- }
- r->next=NULL; //单链表建立完毕,将终端结点的指针域置空
- }
- template
- Score::~Score()
- {
- Node *q;
- while(first!=NULL) //释放单链表的每一个结点的存储空间
- {
- q=first; //暂存被释放结点
- first=first->next; //first指向被释放结点的下一结点
- delete q;
- }
- }
- template
- void Score::Insert(int i,T x)
- {
- Node *p=first,*s;
- int count=0;
- while(p!=NULL && countnext;
- count++;
- }
- if(p==NULL) throw"location";
- else{
- s=new Node;s->data=x;
- s->next=p->next;p->next=s;
- }
- }
- template
- T Score::Delete(int i)
- {
- Node *p=first,*q;
- T x;
- int count=0;
- while(p!=NULL && countnext;
- count++;
- }
- if(p==NULL||p->next==NULL)
- throw"location";
- else{
- q=p->next;x=q->data;
- p->next=q->next;
- delete q;
- return x;
- }
- }
- template
- int Score::Locate(T x)
- {
- Node *p=first->next;
- int count=1;
- while(p!=NULL)
- {
- if(p->data==x) return count;
- p=p->next;
- count++;
- }
- return 0;
- }
- template
- void Score::PrintList()
- {
- Node *p=first->next;
- while(p!=NULL)
- {
- cout<data<<" ";
- p=p->next;
- }
- cout<
- int Score::Length()
- {
- Node *p;
- int count;
- p=first->next;count=0;
- while(p!=NULL)
- {
- p=p->next;
- count++;
- }
- return count;
- }
- template
- T Score::Get(int i)
- {
- Node *p;
- int count;
- p=first->next;count=1;
- while(p!=NULL&&count<i>next;
- count++;
- }
- if(p==NULL)throw"location";
- else return p->data;
- }
- 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[]={34,45,56,67,78,83,89,90,95};
- Scores(a,9);
- int flag,i,x,t,l;
- flag=0;
- while(flag==0)
- {
- cout<<"please input the command(1~8):"<>t;
- switch(t)
- {
- case 1:
- l=s.Length();
- cout<<"the length is:"<>i;
- x=s.Get(i);
- cout<<"the number is:"<>x;
- i=s.Locate(x);
- cout<<"the location is:"<<i>>i;
- cout<<"the insert number is:";
- cin>>x;
- s.Insert(i,x);
- cout<<"insert successfully!"<>i;
- s.Delete(i);
- cout<<"delete successfully!"<</i></i>
运行结果:
主界面
输入1 输出表长
输入2 按位查找成绩
输入3 按成绩查找位置
输入6 输出单链表的所有成绩
输入4,把98插入到位置2
输入5,将位于位置5的成绩删除
输入7,输出主界面
输入8,退出程序