《数据结构》实验二:线性表综合实验——(4)静态链表

来源:互联网 发布:网络用语2016最新 编辑:程序博客网 时间:2024/06/13 22:29

《数据结构》实验二:

                线性表综合实验

一.实验目的

     巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

 

二.实验时间

   准备时间为第3周到第4周,具体集中实验时间为第4周第2次课。2个学时。

 

三..实验内容

1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。

要求如下:

1)用顺序表来实现。

2)用单链表来实现。

3)用双链表实现。

4)用静态链表实现。

5)用间接寻址实现。

分开写程序,可以一个方法分别写一博客文章上交作业。

 

2.实现两个集合的相等判定、并、交和差运算。要求:

  1)自定义数据结构

  2)自先存储结构,并设计算法。在VC中实现。

以上三题,第1题必须完成。第2和第3题可以作为选做题。

四.实验报告

1.在博客中先写上实习目的和内容,画出主要操作运算算法图,然后分别上传程序代码。插入调试关键结果截图。

 

2.单独写一个博文,比较总结线性表的几种主要存储结果。


1)用静态链表来实现。(C++)

1、操作运算算法图

2、实验代码:


头文件:
#include<iostream>#include<iomanip>#include<string>using namespace std;

Student 结构体:
struct Student{string name;int number;int chinese;int english;int sum;};

Node结构体:
const int max = 50;struct Node{Student data;int next;}List[max];static Node *first;static Node *avail;static int p;//存放长度

赋值函数:
Student student(string na,int n,int c,int e){Student s;s.name = na;s.number = n;s.chinese = c;s.english = e;s.sum = c+e;return s;}

录入数据函数:
void Enter(Student a[],int n){   int i;first = new Node;Node * r= first;for(i=0;i<n;i++){r->next = i+1;r = &List[r->next];r->data = a[i];}p=i+1;r->next = -1;avail = &List[p+1];Node * s = avail;for(i = 0;i<max;i++){s->next = p+1+i;s = &List[s->next];}}

插入数据函数:(指定位置)
void Insert(Student a,int n){  Node * s = first;Node * r = avail;for(int i=0;i<n-1;i++){s=&List[s->next];}avail = &List[p+1];List[p].data = a;List[p].next = s->next;s->next = p;p++;}

遍历函数:
void Printlist(){Node *q = first;Node *s = NULL;for(int y=0;y<p-1;y++){q = &List[q->next];s = &List[q->next];for(int z=y+1;z<p-1;z++){if(q->data.sum < s->data.sum){Student t;t = q->data;q->data = s->data;s->data = t;}s = &List[s->next];}}Node * r = first;int i = 1;    cout<<setw(4)<<"序号"<<setw(10)<<"姓名"<<setw(8)<<"学号"<<setw(10)<<"语文成绩"<<setw(10)<<"英语成绩"<<setw(8)<<"总分"<<endl; do{r = &List[r->next];cout<<setw(4)<<i<<setw(10)<<r->data.name<<setw(8)<<r->data.number<<setw(10)<<r->data.chinese<<setw(10)<<r->data.english<<setw(8)<<r->data.sum<<endl;         i++;}while(r->next!=-1);}

删除节点函数:
void Delete(int n){Node * s = NULL;Node * r = first;for(int i=0;i<n-1;i++){r=&List[r->next];}s=&List[r->next];p = p-1;r->next = s->next;avail = s;p--;cout<<setw(10)<<s->data.name<<setw(8)<<s->data.number<<setw(10)<<s->data.chinese<<setw(10)<<s->data.english<<setw(8)<<s->data.sum<<endl;}

修改函数:
void Change()  {      int j;int k;int i;int n;string m;      do{       cout<<"请输入需要修改的序号。"<<endl;      cin>>i;      Node * r = first;for(int t=0;t<i;t++){r = &List[r->next];}    cout<<"若修改名字,请输入1;若修改学号,请输入2;"<<endl;      cout<<"若修改语文成绩,请输入3;若修改英语成绩,请输入4!"<<endl;      cin>>j;      cout<<"请输入正确的信息!"<<endl;      switch(j)      {      case 1:          cin>>m;r->data.name=m;break;      case 2:          cin>>k;r->data.number=k;break;      case 3:        cin>>k;r->data.chinese=k;break;      case 4:          cin>>k;r->data.english=k;break;      default:          cout<<"输入有误,请重新操作!"<<endl;      }      r->data.sum=r->data.chinese+r->data.english;      cout<<setw(10)<<r->data.name<<setw(8)<<r->data.number<<setw(10)<<r->data.chinese<<setw(10)<<r->data.english<<setw(8)<<r->data.sum<<endl;      cout<<"若需继续修改请输入1,否则输入0!"<<endl;      cin>>n;      }while(n==1);  } 

查找数据函数(按位置):
void Get(int n)  {  Node * r = first;for(int i=0;i<n;i++){r = &List[r->next];}    cout<<setw(10)<<r->data.name<<setw(8)<<r->data.number<<setw(10)<<r->data.chinese<<setw(10)<<r->data.english<<setw(8)<<r->data.sum<<endl;  } 

查找数据函数(按学号或姓名):
void Locate()  {  int t=0;    do{     int i;int j,q;string m;  Node * r = first;    cout<<"若依据学号查找,请输入1;若依据姓名查找,请输入2!"<<endl;      cin>>i;      switch(i)      {      case 1:          cout<<"请输入学生学号!"<<endl;          cin>>j; for(q=0;q<p+1;q++){if(r->data.number == j){cout<<setw(10)<<r->data.name<<setw(8)<<r->data.number<<setw(10)<<r->data.chinese<<setw(10)<<r->data.english<<setw(8)<<r->data.sum<<endl;}    r = &List[r->next];}        break;       case 2:          cout<<"请输入学生姓名!"<<endl;          cin>>m; for(q=0;q<p+1;q++){r = &List[r->next];if(r->data.name == m){cout<<setw(10)<<r->data.name<<setw(8)<<r->data.number<<setw(10)<<r->data.chinese<<setw(10)<<r->data.english<<setw(8)<<r->data.sum<<endl;}        }        break;       default:cout<<"输入有误,请重新操作!"<<endl;      }      cout<<"若需继续查找请输入1,否则输入0!"<<endl;      cin>>t;      }while(t==1);  }

主函数:
void main(){ //第一次录入数据Student a[5];a[0] = student("张三",01,85,95);    a[1] = student("里斯",02,75,85);a[2] = student("王果",03,86,87);a[3] = student("范德萨",04,93,96);a[4] = student("郭芙蓉",05,84,82);Enter(a,5);    Printlist();      cout<<setw(1)<<endl; //在指定位置插入数据Student b = student("温热",06,85,79);Insert(b,2);Printlist();    cout<<setw(1)<<endl; //删除数据Delete(3);Printlist();    cout<<setw(1)<<endl;//修改数据Change();Printlist();    cout<<setw(1)<<endl; //按位置来查找Get(3);//按学号或名字来查找Locate();}

3、实验截图

第一次录入数据



在指定位置插入数据



删除数据



修改数据



按位置来查找



按学号或名字来查找


















阅读全文
0 0
原创粉丝点击