《数据结构》实验二: 线性表实验
一..实验目的
巩固线性表的数据结构,学会线性表的应用。
1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。
2.学习运用线性表的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二..实验内容
1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
1)用顺序表来实现。
#include<iostream>
using namespace std;
const int Maxsize=50;
class SeqList
{
public:
SeqList(){length=0;}
SeqList(int a[],int n);
~SeqList(){}
void Insert(int i,int x);
int Delete(int i);
int Locate(int x);
void PrintList();
private:
int data[Maxsize];
int length;
};
SeqList::SeqList(int a[],int n)
{
if(n>Maxsize)throw"参数不符合要求";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void SeqList::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 SeqList::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 SeqList::Locate(int x)
{
for(int i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void SeqList::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
int m[5]={60,75,89,93,99};
SeqList L(m,5);
cout<<"插入操作前的数据为:"<<endl;
L.PrintList();
try
{
L.Insert(4,68);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"插入操作后的数据为:"<<endl;
L.PrintList();
cout<<"值为68的元素位置为:";
cout<<L.Locate(68)<<endl;
cout<<"删除第一个元素,操作前的数据为:"<<endl;
L.PrintList();
try
{
L.Delete(1);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"删除后数据为:"<<endl;
L.PrintList();
}
2)用单链表来实现。
头文件:
#ifndef score_H
#define score_H
class score
{
public:
score();
score(int a[],int n);
~score();
void Insert(int i,int x);
int Locate(int x);
int Delete(int i);
void Print();
private:
Node(int *first);
};
struct Node
{
int data;
Node(int *next);
};
#endif
#include<iostream>
using namespace std;
#include"score.h"
score::score()
{
first=new Node(int);
first->next-NULL;
}
score::score(int a[],int i)
{
Node(int *r,*s);
r=first;
for(int i=0;i<n;i++)
{
s=new Node(int);
s->data=a[i];
r->next=a;
r=s;
}
r->next=NULL;
}
void score::Insert(int i,int x)
{
Node(int *p=first,*s=NULL);
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"位置非法";
else
{
s=new Node(int);
s->data=x;
s->next=p->next;
p->next=s;
}
}
int score::Delete(int i)
{
Node(int *p=first,*q=NULL);
int 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->next=q->next;
delete q;
return x;
}
}
int score::Locate(int x)
{
Node(int *p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x)
return count;
p=p->next;
count++;
}
return 0;
}
void score::Print()
{
Node(int *p=first->next);
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
#include<iostream>
using namespace std;
#include"score.cpp"
void main()
{
int r[4]={56,87,90};
score<int>L(r,4);
cout<<""插入操作前数据为:"<<endl;
L.Print();
try
{
L.Insert(3,89);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"插入操作后数据为:"<<endl;
L.Print();
cout<<"值为87的元素的位置是:"<<endl;
cout<<L.Locate(2)<<endl;
L.Print();
try
{
L.Delete(2);
}
catch(char *2)
{
cout<<s<<endl;
}
cout<<"删除操作后数据为:"<<endl;
L.Print();
}