数据结构实验二(单链表)

来源:互联网 发布:活体脑细胞结局知乎 编辑:程序博客网 时间:2024/06/05 22:55
#include <iostream>using namespace std;template <class DataType>struct Node{    DataType data;    Node <DataType> *next;};template <class DataType>class Linklist{    public:    Linklist();    Linklist(DataType a[],int n);    ~Linklist(){};    int Length();    DataType Get(int i);    int Locate(DataType x);    void Insert(int i,DataType x);    DataType Delete (int i);    void PrintList();    private:    Node<DataType>*first;};template <class DataType>void Linklist<DataType>::PrintList(){    Node<DataType>*p;    p=first->next;    while(p!=NULL)      {          cout<<p->data;          p=p->next;      }}template<class DataType>int Linklist<DataType>::Length(){    Node<DataType>*p=NULL;    p=first->next;    int count=0;    while(p!=NULL)      {         p=p->next;         count++;      }      return count;}template <class DataType>DataType Linklist<DataType>::Get(int i){    Node<DataType>*p;    p=first->next;    int count=1;    while(p!=NULL && count<i)      {          p=p->next;          count++;      }    if(p==NULL) throw"位置";    else return p->data;}template <class DataType>int Linklist<DataType>::Locate(DataType x){    Node<DataType>*p;    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 Linklist<DataType>::Insert(int i,DataType x){    Node<DataType>*p;    p=first;    int count=0;    while(p!= NULL && count<i-1)      {          p=p->next;          count++;      }    if (p==NULL) throw "位置";    else    {        Node<DataType>*s;        s= new Node<DataType>;        s->data=x;        s->next=p->next;        p->next=s;    }}template <class DataType>Linklist <DataType>::Linklist(){    first= new Node<DataType>;    first->next=NULL;}template <class DataType>Linklist<DataType>::Linklist(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>DataType Linklist<DataType>::Delete(int i){    Node<DataType> *p;    int x;    int count=0;    p=first;    while(p!=NULL && count<i-1)     {         p=p->next;         count++;     }     if(p==NULL || p->next==NULL) throw "位置";     else     {         Node<DataType> *q;        q= new Node<DataType>;        q->next=p->next;        x=q->data;        delete q;        return x;     }}int main(){    int score[10]={100,95,90,88,86,85,80,76,75,70};    Linklist<int>a(score,10);    a.PrintList();  //遍历学生分数    cout << "------------" << endl;    cout<<a.Get(2)<<endl; //取第二个位置的学生分数    cout << "------------" << endl;    a.Insert(3, 100); //在第三个位置插入学生分数    cout << "------------" << endl;    a.PrintList();  //再次遍历    cout << "------------" << endl;    cout << a.Length() << endl; //获取分数表长度    cout << "------------" << endl;    cout << a.Locate(96) << endl;  //获取分数为96的位置    cout << "------------" << endl;    cout<<a.Delete(1)<<endl; //删除第一个分数    cout << "------------" << endl;    a.PrintList(); //再次遍历    cout << "------------" << endl;    return 0;}