3.建立一个由n个学生成绩的双链表,实现对数据的插入,删除,查找等操作。
方法1:建立工程,建立头文件Student.h,建立源文件Student.cpp和包含主函数的源文件Student_main.cpp
源代码:
#ifndef Student_H
#define Student_H
struct Node
{
double data;
Node*prior,*next;
};
class Student
{
public:
Student();
Student(double a[],int n);
~Student();
void Insert(int i,double x);
int Locate(double x);
int Delete(int i);
void Print();
private:
Node*first;
};
#endif
#include<iostream>
using namespace std;
#include"Student.h"
Student::Student()
{
first=new Node;
first->prior=NULL;
first->next=NULL;
}
Student::Student(double a[],int n)
{
Node*r,*s;
first=new Node;
r=first;
for(int i=0;i<n;i++)
{
s=new Node;
s->data=a[i];
r->next=s;
s->prior=r;
r=s;
}
r->prior=NULL;
r->next=NULL;
}
Student::~Student()
{
Node*q=NULL;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
void Student::Insert(int i,double x)
{
Node*p=first,*s=NULL;
int count=0;
while(p!=NULL&&count<-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"此成绩不存在";
else
{
s=new Node;
s->data=x;
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
}
}
int Student::Locate(double x)
{
Node*p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
int Student::Delete(int i)
{
Node*p=first,*q=NULL;
double x;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)throw"此成绩不存在";
else
{
q=p->next;
x=q->data;
(p->prior)->next=p->next;
(p->next)->prior=p->prior;
delete p;
return x;
}
}
void Student::Print()
{
Node*p=first->next;
int i=0;
while(p!=NULL)
{
i=i+1;
cout<<"第"<<i<<"个学生的成绩为:"<<p->data<<endl;
p=p->next;
}
cout<<endl;
}
#include<iostream.h>
#include"Student.h"
void main()
{
double s[5]={85.5,67,48,91,76};
Student S(s,5);
cout<<"学生成绩为:"<<endl;
S.Print();
try
{
S.Insert(3,84);
}
catch(char*s)
{
cout<<s<<endl;
}
cout<<endl;
cout<<"修改后的成绩为:"<<endl;
S.Print();
cout<<endl;
cout<<"成绩为84的位置为:"<<endl;
cout<<S.Locate(84)<<endl;
cout<<endl;
cout<<"删除某一学生成绩前的全部学生成绩:"<<endl;
S.Print();
try
{
S.Delete(2);
}
catch(char*s)
{
cout<<s<<endl;
}
cout<<endl;
cout<<"删除后的全部学生成绩为:"<<endl;
S.Print();
}
运行结果:
方法2:直接建立一个源文件
源代码:
#include<iostream>
using namespace std;
template<class DataType>
struct Node
{
DataType data;
Node<DataType>*prior,*next;
};
template<class DataType>
class Student
{
public:
Student();
Student(DataType a[],int n);
~Student();
void Insert(int i,DataType x);
int Locate(DataType x);
DataType Delete(int i);
void Print();
private:
Node<DataType>*first;
};
template<class DataType>
Student<DataType>::Student()
{
first=new Node<DataType>;
first->prior=NULL;
first->next=NULL;
}
template<class DataType>
Student<DataType>::Student(DataType a[],int n)
{
Node<DataType>*r,*s;
first=new Node<DataType>;
r=first;
for(int i=0;i<n;i++)
{
s=new Node<DataType>;
s->data=a[i];
r->next=s;
s->prior=r;
r=s;
}
r->prior=NULL;
r->next=NULL;
}
template<class DataType>
Student<DataType>::~Student()
{
Node<DataType>*q=NULL;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<class DataType>
void Student<DataType>::Insert(int i,DataType x)
{
Node<DataType>*p=first,*s=NULL;
int count=0;
while(p!=NULL&&count<-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"此成绩不存在";
else
{
s=new Node<DataType>;
s->data=x;
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
}
}
template<class DataType>
int Student<DataType>::Locate(DataType x)
{
Node<DataType>*p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<class DataType>
DataType Student<DataType>::Delete(int i)
{
Node<DataType>*p=first,*q=NULL;
DataType x;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)throw"此成绩不存在";
else
{
q=p->next;
x=q->data;
(p->prior)->next=p->next;
(p->next)->prior=p->prior;
delete p;
return x;
}
}
template<class DataType>
void Student<DataType>::Print()
{
Node<DataType>*p=first->next;
int i=0;
while(p!=NULL)
{
i=i+1;
cout<<"第"<<i<<"个学生的成绩为:"<<p->data<<endl;
p=p->next;
}
cout<<endl;
}
void main()
{
double s[5]={85.5,67,48,91,76};
Student<double>S(s,5);
cout<<"学生成绩为:"<<endl;
S.Print();
try
{
S.Insert(1,84);
}
catch(char*s)
{
cout<<s<<endl;
}
cout<<endl;
cout<<"修改后的成绩为:"<<endl;
S.Print();
cout<<endl;
cout<<"成绩为84的位置为:"<<endl;
cout<<S.Locate(84)<<endl;
cout<<endl;
cout<<"删除某一学生成绩前的全部学生成绩:"<<endl;
S.Print();
try
{
S.Delete(2);
}
catch(char*s)
{
cout<<s<<endl;
}
cout<<endl;
cout<<"删除后的全部学生成绩为:"<<endl;
S.Print();
}
运行结果: