您的位置:首页 > 大数据 > 物联网

实验二:线性表的实验【物联网1132-11】

2014-10-18 16:26 302 查看
《数据结构》实验二: 线性表实验

实验目的

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

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

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

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

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

实验内容1:

【顺序表】实现“建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作,分别输出结果。”代码如下:

#include<iostream.h>
const int MaxSize = 100;

template<class T>
class SeqList
{
public:
SeqList();
SeqList(T a[],int n);
T Get(int i);
void Insert(int i,T x);
T Delete(int i);
void PrintList();
private:
T data[MaxSize];
int length;
};

template<class T>
SeqList<T>::SeqList(T a[],int n)
{
if(n>MaxSize)throw"错误";
for (int i=0;i<n;i++)
data[i]=a[i];
length=n;
}

template<class T>
T SeqList<T>::Get(int i)
{
if (i<1&&i>length)throw"错误";
else
cout<<data[i-1]<<endl;
return 0;
}

template<class T>
void SeqList<T>::Insert(int i,T 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++;
}

template<class T>
T SeqList<T>::Delete(int i)
{
if(length==0)throw"错误";
if(i<1||i>length)throw "错误";
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return 0;
}

template<class T>
void SeqList<T>::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
}

void main()
{
int score[5]={10,20,40,50,60};
SeqList<int>ScoreList(score, 5);
cout<<"一开始的第5个数是:"<<endl;
ScoreList.Get(5);
cout<<"成绩表如下:"<<endl;
ScoreList.PrintList();
cout<<endl;
ScoreList.Insert(3,30);
cout<<"插入后的成绩表是:"<<endl;
ScoreList.PrintList();
cout<<endl;
ScoreList.Delete(4);
cout<<"删除后的成绩表是:"<<endl;
ScoreList.PrintList();
cout<<endl;
}


【单链表】实现“建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作,分别输出结果。”代码如下:

#include<iostream>
using namespace std;

template<class T>
struct Student
{
T data;
Student<T> * next;
};

template<class T>
class LinkList
{
public:
LinkList();
LinkList(T a[],int n);
~LinkList();
void Insert(int i,T x);
T Delete(int i);
void PrintList();
private:
Student<T> * first;
};

template<class T>
LinkList<T>::LinkList()
{
first=new Student<T>;
first->next=NULL;
}

template<class T>
LinkList<T>::LinkList(T a[],int n)
{
Student<T> *s;
first=new Student<T>;
first->next=NULL;
int i;
for(i=0;i<n;i++)
{
s=new Student<T>;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}

template <class T>
LinkList<T> :: ~LinkList( )
{
Student<T> *q;
while (first != NULL)
{
q = first;
first = first->next;
delete q;
}
}

template<class T>
void LinkList<T>::Insert(int i,T x)
{
Student<T> *p = first , *s;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"输入错误";
else{
s=new Student<T>;
s->data=x;
s->next=p->next;
p->next=s;
}
}

template<class T>
T LinkList<T>::Delete(int i)
{
Student<T> *p =first, *q;
T 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;
}
}

template<class T>
void LinkList<T>::PrintList()
{
Student<T> *p = first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

void main( )
{
int score[5]={10,20,40,50,60};
LinkList<int>ScoreList(score, 5);
ScoreList.PrintList();
ScoreList.Insert(4,30);
ScoreList.PrintList();
ScoreList.Delete(4);
ScoreList.PrintList();
}


实验内容2:【博客另附有博文介绍】

实验内容3:

【实现两个集合的相等判定、并、交和差运算】

要求: 1)自定义数据结构。2)自先存储结构,并设计算法。在VC中实现。
#include<iostream>
using namespace std;

template<class T>
struct Student
{
T data;
Student<T> * next;
};

template<class T>
class LinkList
{
public:
LinkList();
LinkList(T a[],int n);
void Compare();
void PrintList();
private:
Student<T> * first;
};

template<class T>
LinkList<T>::LinkList()
{
first=new Student<T>;
first->next=NULL;
}

template<class T>
LinkList<T>::LinkList(T a[],int n)
{
Student<T> *s;
first=new Student<T>;
first->next=NULL;
int i;
for(i=0;i<n;i++)    //头插法
{
s=new Student<T>;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}

template<class T>
void LinkList<T>::PrintList()
{
Student<T> *p = first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

template<class T>
void LinkList<T>::Compare()
{
Student<T> *p = first->next;
int w;
int f=60;
if(p->data==f)
{
f=f-10;
p=p->next;
w=1;
}else
{
w=2;
}
if(w==1)
{
cout<<"与成绩1组一致"<<endl;
}else
{
cout<<"与成绩1组不一致"<<endl;
}
}

void main( )
{
int score[6]={10,20,30,40,50,60};
LinkList<int>ScoreList(score, 6);
cout<<"第1个成绩数组是:";
ScoreList.PrintList();
int score1[6]={90,20,30,40,50,60};
LinkList<int>ScoreList1(score1, 6);
cout<<"第2个成绩数组是:";
ScoreList1.PrintList();
cout<<endl;
cout<<"接下来进行对比"<<endl<<"-----------------"<<endl;
ScoreList.Compare();
ScoreList1.Compare();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: