实验二 顺序表和单链表

来源:互联网 发布:java简单课程设计 编辑:程序博客网 时间:2024/06/03 16:01
#include<iostream>using namespace std;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;};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<i||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;}void main(){int a[10]={100,92,83,94,95,76,77,68,91,80};Score S(a,10);cout<<"执行插入数据前的10个数据为:"<<endl;S.PrintList();try{S.Insert(3,65);}catch(char *s){cout<<s<<endl;}cout<<"执行插入操作后11个数据为:"<<endl;S.PrintList();cout<<"值为91的元素位置为:";cout<<S.Locate(91)<<endl;cout<<"执行删除第一个元素操作,删除前的数据为:"<<endl;S.PrintList();try{S.Delete(1);}catch(char *s){cout<<s<<endl;}cout<<"删除后的数据为:"<<endl;S.PrintList();}
 
<pre class="cpp" name="code">#include<iostream>using namespace std;template<class DataType>struct Node{ DataType data;    Node<DataType> * next;};template<class DataType>class Score{public:Score();Score(DataType a[],int n);~Score();int Locate(DataType x);void Insert(int i,DataType x);DataType Delete(int i);void PrintList();private:Node<DataType> * first;};template<class DataType>Score<DataType>::Score(){first=new Node<DataType>;first->next=NULL;}template<class DataType>Score<DataType>::Score(DataType a[],int n){Node<DataType> *r,*s;first=new Node<DataType>;r=first;for(int i=0;i<n;i++){s=new Node<DataType>;s->data=a[i];r->next=s;r=s;}r->next=NULL;}template<class DataType>Score<DataType>::~Score(){Node<DataType> *q=NULL;    while(first!=NULL){q=first;first=first->next;delete q;}}template<class DataType>void Score<DataType>::Insert(int i,DataType x){Node<DataType> *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<DataType>;s->data=x;s->next=p->next;p->next=s;}}template <class DataType>DataType Score<DataType>::Delete(int i){Node<DataType>*p=first,*q=NULL;DataType 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 DataType>int Score<DataType>::Locate(DataType x){Node<DataType>*p=first->next;int count=1;while(p!=NULL){if(p->data==x)return count;p=p->next;count++;}return 0;}template<class DataType>void Score<DataType>::PrintList(){Node<DataType>*p=first->next;while(p!=NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;}void main(){int a[10]={99,82,77,90,73,100,83,68,98,99};Score<int>S(a,10);cout<<"执行插入前个数据为:"<<endl;S.PrintList();try{S.Insert(3,97);}catch(char *s){cout<<s<<endl;}cout<<"执行插入后个数据为:"<<endl;S.PrintList();cout<<"值为90的位置为:";cout<<S.Locate(90)<<endl;cout<<"执行删除前的数据为:"<<endl;S.PrintList();try{S.Delete(2);}catch(char *s){cout<<s<<endl;}cout<<"执行删除后的数据为:"<<endl;S.PrintList();}


0 0
原创粉丝点击