《数据结构》实验二:线性表实验
一..实验目的
巩固线性表的数据结构,学会线性表的应用。
1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。
2.学习运用线性表的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二.实验时间
准备时间为第2周到第4周,具体集中实验时间为第4周第2次课。2个学时。
三..实验内容
1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
1)用顺序表来实现。
2)用单链表来实现。
1)顺序表
<span style="color: rgb(204,0); font-style: italic; font-weight: bold; font-family: Arial,Helvetica,sans-serif; background-color: rgb(255,255,255);"><span style="font-size:24px;">头文件SeqList.h</span></span>
<strong>#ifndef SeqList_H
#define SeqList_H
const int Maxsize=100;
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;
};
#endif</strong>
SeqList.cpp
<strong>#include<iostream>
using namespace std;
#include"SeqList.h"
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;
}</strong>
Seqlist-main.cpp
#include<iostream> using namespace std; #include "SeqList.h" void main() { int r[10]={95,89,75,69,70,90,98,87,79,82}; Score L(r,10); cout<<"执行插入操作的前的数据为:"<<endl; L.Printlist(); try { L.Insert(5,50); } catch(char *s) { cout<<s<<endl; } cout<<"执行插入操作后的数据为:"<<endl; L.Printlist(); cout<<"值为87的元素位置为:"; cout<<L.Locate(81)<<endl; cout<<"执行删除第一个元素操作,删除前数据为:"<<endl; L.Printlist(); try { L.Delete(1); } catch(char *s) { cout<<s<<endl; } cout<<"删除后的数据为:"<<endl; L.Printlist();
} }
2)单链表
<span style="color: rgb(51,51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255,255);">#define LINKLIST_H </span>
#include <iostream> using namespace std; template <class score> class Node { public: score data; Node<score>*next; }; template <class score> class LinkList { public: LinkList(); LinkList(score a[],int n); ~LinkList(); int length(); score Get(int i); int Locate(score x); void Insert(int i,score x); score Delete(int i); void Print(); private: Node<score>*first; }; #endif
LinkList.cpp
#include "LinkList.h" #include<stdlib.h> template <class score>
LinkList<score>::LinkList() { first=new Node<score>; first->next=NULL; } template <class score> LinkList<score>::LinkList(score a[],int n) { first=new Node<score>; Node<score>*r,*s; r=first; for(int i=0;i<n;i++) { s=new Node<score>; s->data=a[i]; r->next=s; r=s; } r->next=NULL; } template <class score> LinkList<score>::~LinkList() { while(first!=NULL) { Node<score>*q; q=first; first=first->next; delete q; } } template <class score> int LinkList<score>::length() { Node<score>*p; p=first->next; int count=0; while(p!=NULL) { p=p->next; count++; } return count; } template <class score> score LinkList<score>::Get(int i) { Node<score>*p=first->next; int count=1; while(p!=NULL&&count<i) { p=p->next; count++; } if(p==NULL) throw 1.2; return p->data; } template <class score> int LinkList<score>::Locate(score x) { Node<score>*p=first->next; int count=1; while(p!=NULL) { if(p->data==x)return count; p=p->next; count++; } return 0; } template <class score> void LinkList<score>::Insert(int i,score x) { int count=0; Node<score>*p=first; while(p!=NULL&&count<i-1) { p=p->next; count++; } if(p==NULL) throw 1; else { Node<score>*s=new Node<score>; s->data=x; s->next=p->next; p->next=s; } } template<class score> score LinkList<score>::Delete(int i) { score x; Node<score>*p=first,*q; int count=0; while(p!=NULL&&count<i-1) { p=p->next; count++; } if(p==NULL||p->next==NULL) throw "位置"; q=p->next; x=q->data; p->next=q->next; delete q; return x; } template <class score> void LinkList<score>::Print() { Node<score>*p=first->next; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; }
LinkList-main.cpp
#include "LinkList.cpp" void main() { int score[]={85,95,92,97,88,81}; LinkList<int> linklist(score,7); cout<<"每个学生的成绩分别为:"; linklist.Print(); cout<<"学生个数:"<<linklist.length()<<"个"<<endl; cout<<"成绩表中92分位于第"<<linklist.Locate(94)<<"个位置上"<<endl; try { cout<<"成绩表中第三个分数为:"; cout<<linklist.Get(3)<<endl; cout<<"在成绩表中的第三个位置插入成绩93"<<endl; cout<<"插入前:"; linklist.Print(); cout<<"插入后:"; linklist.Insert(3,93); linklist.Print(); cout<<"把第五个成绩删除"<<endl; cout<<"删除前:"; linklist.Print(); cout<<"删除的成绩为:"<<linklist.Delete(5)<<endl; cout<<"删除后:"; linklist.Print(); } catch(double) { cout<<"获取的位置不存在!"<<endl; exit(1); } catch(char*) { cout<<"删除的位置不存在!"<<endl; exit(1); } catch(int) { cout<<"插入位置不存在!"<<endl; exit(1); }
运行结果: } (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|