《数据结构》:实验 线性表实验
来源:互联网 发布:美图秀秀没有mac版吗 编辑:程序博客网 时间:2024/04/28 15:32
一..实验目的
巩固线性表的数据结构,学会线性表的应用。
1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。
2.学习运用线性表的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二.实验时间
准备时间为第2周到第4周,具体集中实验时间为第4周第2次课。2个学时。
三..实验内容
1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
1)用顺序表来实现。
#include<iostream>
using namespace std;
const int Maxsize = 60;
class Score
{
public:
Score(){ length = 0; }
Score(int a[], int n);
~Score(){ }
int Locate(int x);
void Insert(int i, int x);
int Delete(int i);
void Printlist();
private:
int data[Maxsize];
int length;
};
Score::Score(int a[], int n)
{
if (n>Maxsize) throw"参数非法";
for (int i = 0; i<n; i++)
data[i] = a[i];
length = n;
}
void Score::Insert(int i, int x)
{
if (length >= Maxsize) throw"上溢";
if (i<1 || i>length + 1) throw"位置非法";
for (int j = length; j >= i; j--)
data[j] = data[j - 1];
data[i - 1] = x;
length++;
}
int Score::Delete(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 Score::Locate(int x)
{
for (int i = 0; i<length; i++)
if (data[i] == x) return i + 1;
return 0;
}
void Score::Printlist()
{
for (int i = 0; i<length; i++)
cout << data[i] << " ";
cout << endl;
}
void main()
{
int r[5] = { 11,12,13,14,15 };
Score L(r, 5);
cout << "执行插入操作的前的数据为:" << endl;
L.Printlist();
try
{
L.Insert(3,12);
}
catch (char *s)
{
cout << s << endl;
}
cout << "执行插入操作后的数据为:" << endl;
L.Printlist();
cout << "值为12的元素位置为:";
cout << L.Locate(12) << endl;
cout << "执行删除第一个元素操作,删除前数据为:" << endl;
L.Printlist();
try
{
L.Delete(1);
}
catch (char *s)
{
cout << s << endl;
}
cout << "删除后的数据为:" << endl;
L.Printlist();
int s;
cin >> s;
}
执行结果如下:
2)用单链表来实现。
#include<iostream>
using namespace std;
template<class DataType>
struct Node
{
DataType data;
Node<DataType>*next;
};
template<class DataType>
class LinkList
{
public:
LinkList();
LinkList(DataType a[],int n);
~LinkList();
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
Node<DataType>*first;
};
template<class DataType>
LinkList<DataType>::LinkList()
{
first=new Node<DataType>;
first->next=NULL;
}
template<class DataType>
LinkList<DataType>::LinkList(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;r=s;
}
r->next=NULL;
}
template<class DataType>
LinkList<DataType>::~LinkList()
{
Node<DataType>*q;
while(first !=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<class DataType>
void LinkList<DataType>::Insert(int i,DataType x)
{
Node<DataType>*p=first,*s;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"位置";
else
{
s=new Node<DataType>;
s->data=x;
s->next=p->next;
p->next=s;
}
}
template<class DataType>
DataType LinkList<DataType>::Delete(int i)
{
Node<DataType>*p,*q;
DataType 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;
p->next=q->next;
delete q;
return x;
}
}
template<class DataType>
int LinkList<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>
void LinkList< DataType>::PrintList()
{
Node<DataType>*p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void main()
{
int Score[5]={11,12,13,14,15};
LinkList<int> ScoreList(Score, 5);
cout<<"链表数据为:"<<endl;
cout<<"执行插入操作前数据为:"<<endl;
ScoreList.PrintList( );
try
{
ScoreList.Insert(3, 12);
}
catch (char *s)
{
cout<<s<<endl;
}
cout<<"执行插入操作后数据为:"<<endl;
ScoreList.PrintList( );
cout<<"值为12的元素位置为:";
cout<<ScoreList.Locate(12)<<endl;
cout<<"执行删除操作前数据为:"<<endl;
ScoreList.PrintList( );
try
{
ScoreList.Delete(4);
}
catch (char *s)
{
cout<<s<<endl;
}
cout<<"执行删除操作后数据为:"<<endl;
ScoreList.PrintList( );
}
执行结果如下:
- 《数据结构》:实验 线性表实验
- 《数据结构》实验二:线性表的实验
- 《数据结构》实验二:线性表实验
- 《数据结构》实验二: 线性表实验
- 《数据结构》实验二 线性表的实验
- 《数据结构》实验二:线性表实验
- 《数据结构》实验二:线性表的实验
- 数据结构实验二 :线性表的实验
- 《数据结构》实验二 线性表的实验
- 《数据结构》实验二:线性表实验(1)
- 数据结构实验二 线性表实验
- 《数据结构》实验二: 线性表实验
- 《数据结构》实验二:线性表的实验
- 《数据结构》实验二:线性表的实验
- 《数据结构》实验二: 线性表实验
- 数据结构实验二 :线性表的实验
- 数据结构实验二:线性表实验
- 《数据结构》实验二: 线性表实验
- 第10周项目2.1传纸条
- 《Effective C++》阅读笔记
- Java并发原语——线程、互斥与同步
- Kafka集群安装
- Eclipse设置、调优、使用
- 《数据结构》:实验 线性表实验
- 【烤馕】诗一首
- NCFS cache层 cache/no_cache.cc
- Java NIO 系列教程
- js中的‘==’和‘===’
- PHP的错误处理方式(开发和上线)
- STM32 直流电机
- hdu1561——The more, The Better
- Access那些坑