第二章线性表设计2【物联网1132-11】

来源:互联网 发布:淘宝购买的模板找不到 编辑:程序博客网 时间:2024/05/18 03:56

以下程序是对于“程序的实际代码没有深刻了解”就打出来的诸多问题的代码。


#include<iostream>using namespace std;template<class T>struct Student{T data;Student<T> * next;};template<class T>class LinkList{public:LinkList();LinkList(T a[],int n);~LinkList();void Insert(int i,T x);T Delete(int i);void PrintList();private:Student<T> * first;};template<class T>LinkList<T>::LinkList(){first=new Student;first->next=NULL;}template<class T>LinkList<T>::LinkList(T a[],int n){first=new Student;first->next=NULL;for(i=0;i<n;i++){s=new Student;s->data=a[i];s->next=first->next;first->next=s;}}template<class T>void LinkList<T>::Insert(int i,T x){p=first;count=0;while(p!=NULL&&count<i-1){p=p->next;count++;}if(p==NULL)throw"输入错误"else{s=new Student;s->data=x;s->next=p->next;p->next=s;}}template<class T>T LinkList<T>::Delete(int i){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<class T>void LinkList<T>::PrintList(){p=first->next;while(p!=NULL){cout<<p->data;p=p->next;}}void main( )  {  int score[5]={10, 20, 40, 50,60};      LinkList<int> ScoreList(score, 5);ScoreList.PrintList();ScoreList.Insert(4,30);ScoreList.PrintList();ScoreList.Delete(4);ScoreList.PrintList();}


以下是经过老师的指导教育和自己的听课反思改好的代码。


#include<iostream>using namespace std;template<class T>struct Student{T data;Student<T> * next;};template<class T>class LinkList{public:LinkList();LinkList(T a[],int n);~LinkList();void Insert(int i,T x);T Delete(int i);void PrintList();private:Student<T> * first;};template<class T>LinkList<T>::LinkList(){first=new Student<T>;first->next=NULL;}template<class T>LinkList<T>::LinkList(T a[],int n){Student<T> *s;first=new Student<T>;first->next=NULL;int i;for(i=0;i<n;i++){s=new Student<T>;s->data=a[i];s->next=first->next;first->next=s;}}template <class T>  LinkList<T> :: ~LinkList( )  {      Student<T> *q;      while (first != NULL)            {          q = first;                       first = first->next;              delete q;          }  }template<class T>void LinkList<T>::Insert(int i,T x){Student<T> *p = first , *s;int count=0;while(p!=NULL&&count<i-1){p=p->next;count++;}if(p==NULL)throw"输入错误";else{s=new Student<T>;s->data=x;s->next=p->next;p->next=s;}}template<class T>T LinkList<T>::Delete(int i){Student<T> *p =first, *q;T 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 T>void LinkList<T>::PrintList(){Student<T> *p = first->next;while(p!=NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;}void main( )  {  int score[5]={10,20,40,50,60};      LinkList<int>ScoreList(score, 5);ScoreList.PrintList();ScoreList.Insert(4,30);ScoreList.PrintList();ScoreList.Delete(4);ScoreList.PrintList();}



在这里,以后我们要注意的问题如下:【不专业不厉害,可是一步一个脚印】

1、定义模板类后要记得在使用的时候加上“<T>”。

2、定义Student的这个“结构体”模板指针的结点后记得在使用时写成“Student<T>”这种字样。

3、对于每个小框架代码里的变量,记得在使用它们前先定义好,否则就是undeclear之类的。

4、调用的时候不需要写成“ScoreList<T>XXXX“,不过要记得创建对象,否则不可以调用模板函数。


                                             
0 0