实验二 线性表实验

来源:互联网 发布:java电话本管理系统 编辑:程序博客网 时间:2024/06/06 04:00

  线性表实验

一..实验目的

     巩固线性表的数据结构,学会线性表的应用。

1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。

2.学习运用线性表的知识来解决实际问题。

3.进一步巩固程序调试方法。

4.进一步巩固模板程序设计。

二.实验时间

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

三..实验内容

1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。

要求如下:

1)用顺序表来实现:通过创建多文件

a.创建头文件“student.h”

#ifndef student_H
#define student_H
const int MaxSize=10;
class Student
{
public:
    Student(){length=0;}
Student(int a[],int n);
~Student(){}
void Insert(int i,int x);
int deletes(int i);
int locate(int x);
void printlist();
private:
int data[MaxSize];
int length;
};
#endif

b.创建源程序文件“student.cpp”

#include<iostream>
using namespace std;
#include "student.h"


Student::Student(int a[],int n)
{
if(n>MaxSize)throw"参数非法";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void Student::Insert(int i,int x)
{
if(length>=MaxSize)throw"上溢";
if(i<1||i>length++)throw"位置非法";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++; 
}
int Student::deletes(int i)
{
if(length==0)throw"下溢";
if(i<1||i>length)throw"位置非法";
int x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
int Student::locate(int x)
{
for(int i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void Student::printlist()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}

c.建#include<iostream>
using namespace std;
#include "student.cpp"
void main()
{
int r[5]={1,2,3,4,5};
Student l(r,5);
cout<<"执行插入操作前数据为"<<endl;
l.printlist();
try
{
l.Insert(2,3);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"执行插入操作后数据为:"<<endl;
l.printlist();
cout<<"值为3的元素位置为:";
cout<<l.locate(3)<<endl;
cout<<"执行删除的第一个元素操作,删除前数据为"<<endl;
l.printlist();
try
{
l.deletes(1);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"删除后数据为:"<<endl;
l.printlist();
}立一个主函数“student-main.cpp”

d.实验结果:


出现这个问题不会解决T_T

2)用单链表来实现:创建多文件

#ifndef Student_H
#define Student_H
template<class Ty>
struct node                           
{
Ty data;
node<Ty>*next;
};
template <class Ty>
class Sgrade
{
public:
        Sgrade();
Sgrade(Ty a[],int n);
~Sgrade();
int length();
Ty get(int i);
int locate(Ty x);
void insert(int i,Ty x);
Ty Delete(int i);
void printlist();
private:
node<Ty>*first;
};
#endif

b.创建源程序文件“Student.cpp”

#include <iostream>
using namespace std;
#include"Student.h"
template <class Ty>
Sgrade<Ty>::Sgrade()
{
first=new node<Ty>;
first->next=NULL;
}
template <class Ty>
Sgrade<Ty>::Sgrade(Ty a[],int n)
{
node<Ty>*r,*s;
first=new node<Ty>;
r=first;
for(int i=0;i<n;i++)
{
s=new node<Ty>;
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
template <class Ty>
Sgrade<Ty>::~Sgrade()
{
node<Ty>*q;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template <class Ty>
void Sgrade<Ty>::insert(int i,Ty x)
{
node<Ty>*p=first,*s;
int count=0;
while (p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"位置";
else
{
s=new node<Ty>;
s->data=x;
s->next=p->next;
p->next=s;
}
}
template <class Ty>
Ty Sgrade<Ty>::Delete(int i)
{
node<Ty>*p,*q;
Ty x;
int count=0;
p=first;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL || p->next==NULL)throw"位置";
else
{
q=p->next;
x=q->data;
delete q;
return x;
}
}
template <class Ty>
int Sgrade<Ty>::locate(Ty x)
{
node<Ty>*p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template <class Ty>
 void Sgrade<Ty>::printlist()
{
node<Ty>*p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

c.创建头文件“Student1.cpp”

#include <iostream>
using namespace std;
#include"Student.cpp"
void main()
{
int r[5]={11,12,13,14,15};
Sgrade<int>R(r,5);
    cout<<"执行插入操作前的数据为:"<<endl;
R.printlist();
R.insert(2,16);
cout<<"执行插入操作后的数据为:"<<endl;
R.printlist();
cout<<"查找值为13的元素位置:"<<endl;
cout<<R.locate(13)<<endl;
cout<<"执行删除操作前的数据为:"<<endl;
R.printlist();
R.Delete(3);
cout<<"执行删除操作后的数据为:"<<endl;
R.printlist();
}



实验总结:

通过这次实验,进一步的了解了线性表的数据结构,学会线性表的应用,对代码又进一步的熟悉了。

但是仍存在问题不会解决。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 学信网学籍档案没照片怎么办 信访局不给答复怎么办 发票跳了一个号怎么办 报税用的ca证书怎么办 报税u盘丢了怎么办 地税ca证书丢了怎么办 深圳ca证书丢了怎么办 武汉国税报税证书过期怎么办 江苏大学专业选修课挂了怎么办 电信翼企享福卡怎么办 教育部学籍在线验证报告过期怎么办 身份证被别人注册了学信网怎么办 大专文凭查不到学籍该怎么办 学信网上查不到学历怎么办 学信网上没有学历照片怎么办 学信网上没照片怎么办 学历认证报告丢了怎么办 学历认证弄丢了怎么办 手机系统安全证书有问题怎么办 台式电脑的浏览器证书出错怎么办 网上银行k宝密码忘了怎么办 工行证书介质已被锁定怎么办 学历认证是假的怎么办 怕被公司查学历怎么办 淘宝玩具没有怎么办3c 家庭遭遇小三我该怎么办 老公出轨把小三带回家了怎么办 小三怀孕了怎么办准生证 小三怀孕了起诉怎么办 不知情做了小三怎么办 发现自己被三了怎么办 被扇巴掌脸肿了怎么办 分到上海市金鼎学校怎么办 被列入维稳对象怎么办? 资金涉及诈骗案冻结了怎么办 小米浏览器浏览记录找不到了怎么办 米聊账号封了怎么办 管家婆创业版管理员忘记密码怎么办 手机不记得密码了怎么办 手机不记得开锁密码怎么办 oppo手机不记得密码怎么办