《数据结构》实验二: 线性表实验
相关知识:
线性表的顺序存储结构称为顺序表。
顺序表是用一段地址连续的存储单元依次存储线性表的数据元素。
通常用一维数组来实现顺序表,也就是把线性表中相邻的元素存储在数组相邻的位置,使数据元素的序号和存放它的数组,小标之间的一一对应关系。(数组的下标是从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(); }
终于在参考了书上资料,以及同学的帮助,完成了这次实验。(还不知有没错误?)
敲代码真不是一件容易的事情,就好好学习,多点看书才行。加油↖(^ω^)↗
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。