《数据结构》实验二:线性表的实验
《数据结构》实验二:?????线性表实验 相关知识: 线性表的顺序存储结构称为顺序表。 顺序表是用一段地址连续的存储单元依次存储线性表的数据元素。 通常用一维数组来实现顺序表,也就是把线性表中相邻的元素存储在数组相邻的位置,使数据元素的序号和存放它的数组,小标之间的一一对应关系。(数组的下标是从0开始的,而线性表中元素的序号是从1开始的,线性表中第i个元素存储在数组中下标为i-1的位置) 一..实验目的 ???? 巩固线性表的数据结构,学会线性表的应用。 1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。 2.学习运用线性表的知识来解决实际问题。 3.进一步巩固程序调试方法。 4.进一步巩固模板程序设计。 二.实验时间 ?? 准备时间为第2周到第4周,具体集中实验时间为第4周第2次课。2个学时。 三..实验内容 1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。 要求如下: 1)用顺序表来实现。 头文件源代码
const int maxsize=100; class score { public: Score(){length=0;} Score(int a[],int n); ~Score(){} void Insert(int i,int x); int Delete(int i); int Locate(int x); void Printlist(); private: int data[Maxsize]; int length; }; 成员函数源代码
#include"head.h" #include<iostream> using namespace std; 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 [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; } 主函数源代码
#include<iostream> using namespace std; int main() { int a[5]={60,47,86,88,90}; score t (a,5); cout <<"5个学生的成绩分别为:"<<endl; t.Score(); try { t.Insert(2,99); } catch (char *S){ cout<<s<<endl; } cout<<"执行插入操作后的数据为:"<<endl; t.Printlist(); cout<<"分数为为99的元素位置为:"; cout<<t.Locate(99)<<endl; cout<<"执行删除第一个元素操作,删除前数据为:"<<endl; t.Printlist(); try { t.Delete(1); } catch(char *s) { cout<<s<<endl; } cout<<"删除后所有人的成绩为:"<<endl; t.Printlist(); 2)用单链表来实现。
#include<iostream> using namespace std; template<class Score> struct Node { Score data; Node<Score> * next; }; template<class Score> class Linklist { public: Linklist(); Linklist(Score a[],int n); ~Linklist(); int Locate(Score x); void Insert(int i,Score x); Score Delete(int i); void Printlist(); private: Node<Score> * first; }; template<class Score> Linklist<Score>::Linklist() { first=new Node<Score>; first->next=NULL; } template<class Score> Linklist<Score>::Linklist(Score a[],int n) { Node<Score> *r,*s; first=new Node<Score>; 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() { Node<Score>*q=NULL; while(first!=NULL) { q=first; first=first->next; delete q; } } template<class Score> void Linklist<Score>::Insert(int i,Score x) { Node<Score>*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<Score>;s->data=x; s->next=p->next;p->next=s; } } template<class Score> Score Linklist<Score>::Delete(int i) { Node<Score>*p=first,*q=NULL; Score 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 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>::Printlist() { Node<Score>*p=first->next; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; } void main() { int r[10]={72,49,82,61,83,100,98,70,73,84}; Linklist<int>L(r,10); cout<<"执行插入操作前的数据为:"<<endl; L.Printlist(); try { L.Insert(3,89); } catch(char *s) { cout<<s<<endl; } cout<<"执行插入操作后的数据为:"<<endl; L.Printlist(); cout<<"值为100的元素的位置为:"; cout<<L.Locate(100)<<endl; cout<<"执行删除操作前的数据为:"<<endl; L.Printlist(); try { L.Delete(1); } catch(char *s) { cout<<s<<endl; } cout<<"执行删除操作后的数据为:"<<endl; L.Printlist(); } 终于在参考了书上资料,以及同学的帮助,完成了这次实验。(还不知有没错误?) 敲代码真不是一件容易的事情,就好好学习,多点看书才行。加油↖(^ω^)↗ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |