实验二单链表

来源:互联网 发布:金立gn205软件 编辑:程序博客网 时间:2024/05/16 13:53

一、实验目的

巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。


二、实验内容

建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。 

源代码为:

#include<iostream>    using namespace std;    template <typename T>    struct Node      { T data;        Node<T> *next;      };  /*建立一个由n个学生成绩的顺序表*/   template <typename T>    class LinkList  {    public:     LinkList(); //无参构造函数   LinkList(T score[],int n); //有参构造函数     ~LinkList()    { Node<T> *q;    while(first!=NULL)       { q=first;         first=first->next;          delete q;  }       }    void insert(int i,T x); //在位置i插入元素x    T Delete(int i);//删除位置i的元素    T get(int i);//按位查找    int locate(T x); //按值查找    void print();//输出操作    private:      Node<T> *first; //头指针      };    template<class T>      LinkList<T>::LinkList()      {          first = new Node<T>;          first->next = NULL;      }    template<typename T>      LinkList<T>::LinkList(T score[],int n)    {        Node<T>*s;        first=new Node<T>; first->next=NULL; //初始化一个空链表        for(int i=0;i<n;i++)        {            s=new Node<T>;s->data=score[i];            s->next=first->next;first->next=s;       }    }     template<typename T>      void LinkList<T>::insert(int i,T x)    {        Node<T>*p,*s;int count;        p=first;count=0;        while(p!=NULL&&count<i-1)        {            p=p->next;            count++;        }        if(p==NULL)throw"位置非法";        else{            s=new Node<T>;s->data=x;            s->next=p->next;p->next=s;        }    }      template<typename T>      T LinkList<T>::Delete(int i)            {                Node<T> *q,*p; T x; int count;                p=first;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<typename T>      T LinkList<T>::get(int i)            {                Node<T>*p;int count;                p=first->next;count=1;                while(p!=NULL&&count<i)                {p=p->next;count++;}                if(p==NULL)throw"位置非法";                else return p->data;            }    template<typename T>      int LinkList<T>::locate(T x)            {                Node<T>*p;int count;                p=first->next;count=1;                while(p!=NULL)                {                    if(p->data==x)return count;                    p=p->next;                    count++;   }                return 0;            }    template<typename T>      void LinkList<T>::print()            {                Node<T>*p;                p=first->next;                while(p!=NULL)                {cout<<p->data<<"  ";              p=p->next; }            }    void main()    {        float score[8]={75,98,90,86,56,77,65,99};        LinkList<float>student(score,8);       cout<<" 学生的所有成绩"<<endl;          student.print();          cout<<endl<<"删除在6位置的成绩:"<<student.Delete(6)<<endl<<"删除后所有的成绩为:  "<<endl;          student.print();       cout<<endl<<"在位置4插入成绩66,插入后所有的成绩为:"<<endl;          student.insert(4,66);          student.print();          cout<<endl<<"位置6的成绩为:"<<student.get(6)<<endl;          cout<<endl<<"成绩75所在位置为:"<<student.locate(75)<<endl;    }