| 
                         
        
            下面的一个同学程序,编译和链接均没问题,只是运行出面错误。现将程序示众,大家细看我改过的地方面,用红色标记。  
还是一个粗心,请细分每个功能函块,要画图来推推。要说明的是,我没有从功能上完善同学的程序。程序有好多改进的地方。  
同学程序链接是:http://blog.csdn.net/u014577636/article/details/40653223  
#include<iostream> ?  
using namespace std; ?  ??  const int Max=70; ?  template<class TT> ?  struct Node ?  { ?? TT score; ?  ? ? Node<TT> *next; ?  };?  ?  template<class TT> ?  class Score ?  {? ? Node<TT> *first; ?  ? public: ?  ? ? ? Score(); ? ? ? ? ? ? ? ? ? //建立一个空的单链表 ?  ? ? ? Score(TT a[],int n); ? ? ?//建立一个有N个元素的单链表(析构函数省略) ?  ? ? ? ? //~Score(); ?  ? ? ? void insert(int i,TT x); ? //插入函数 ?  ? ? ? int locate( TT x); ? ? ? ? //按分数查找 ?  ? ? ? TT get(int i); ? ? ? ? ? ? //按学号查找 ?  ? ? ? TT Delete(int i); ? ? ? ? ?//删除函数 ?  }; ?  ??  template<class TT> ?  Score<TT>::Score() ?  { ?? ?first=new Node<TT>; ?  ? ? first->next= NULL; ?  } ?  ??  template<class TT> ?  Score<TT>::Score(TT a[],int n) ?  { ?? int i; ?  ? ? Node<TT> *s; ?  ? ? first=new Node<TT>; ?  ? ? first->next=NULL; ?  ? ? for(i=0;i<n;i++){ ?  ? ? ? ? s=new Node<TT>; ?  ? ? ? ? s->score=a[i]; ?  ? ? ? ? s->next=first->next; ?  ? ? ? ? first->next=s; ?  ? ? } ?  } ?  ??  /*?  template<class TT>?  Score<TT>::Score(TT a[],int n) ? ? ? ? ? ? ? ? ?//这个是尾插法?  {?? ?Node<TT> *s,*r;?  ? ? first=new Node;?  ? ? r=first;?  ? ? for(i=0;i<n;i++)?  ? ? {?? ?s=new Node; ?  ? ? ? ? s->score=a[i];?  ? ? ? ? r->next=s;?  ? ? ? ? r=s;?  ? ? }?  ? ? r-next=NULL;?  }?  */ ?
 
   template<class TT> ?  void Score<TT>::insert(int i,TT x) ?  { ??Node<TT> *p,*s=NULL; ? ? ? ? ?  ? ? p=first; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//从头结点开始 ? ? ? ??  ? ? int count=0; ?  ? ? for (count=0;p!=NULL && count<i-1;count++) ? ? ?//查找第i-1个节点 ?  ? ? ? ? p=p->next; ?  ? ? if(p==NULL)throw "位置"; ? ? ? ? ? ? ? ? ? ? ?//没找到 ?  ? ? else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //找到了,插入新节点 ?  ? ? ? ? s=new Node<TT>; ?  ? ? ? ? s->score=x; ?  ? ? ? ? s->next=p->next; ?  ? ? ? ? p->next=s; ?  ? ? } ?  } ?  ??  template<class TT> ?  int Score<TT>::locate(TT x) ?  { ?  ? ? Node<TT> *p; ?  ? ? p=first->next; ?  ? ? int count; ?  ? ? for(count=1;p!=NULL;count++) ? { ? ? ? ? ? ? ? ? ? ? ? ? //加一个括号 if(p->score==x) ? ? ? ? ? ? return count; //返回第i+1位同学 ? p=p->next; ? ? ? //加了这个语句 } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //加一个括号  ? ? return 0;//找不到,退出循环 ?  } ?  ??  template<class TT> ? ? ? ? //i的用处是什么,函数中没作到。  TT Score<TT>::get(int i) ?  { ?  ? ? Node<TT> *p; ?  ? ? p=first->next; ?  ? ? int count; ?  ? ? for(count=1;p!=NULL;count++) ? { ? ? ? ? ? ? ? ? ? //加一个括号 p=p->next; ?  ? ? ? ? if(p==NULL)throw "位置"; ?  ? ? ? ? else return p->score; ? ? ? ? //找到了,返回这位同学 ? } ? ? ? ? ? ? ?//加一个括号  ? ? }?  ??  template<class TT> ?  TT Score<TT>::Delete(int i) ?  { ??Node<TT> *p,*q; ?  ? ? TT x; ?  ? ? p=first; ?  ? ? int count=0; ?  ? ? for(count=0;p!=NULL && count<i-1;count++) ?  ? ? ? ? p=p->next; ?  ? ? if(p==NULL||p->next==NULL) //节点p不存在,或者p的后继节点不存在 ?  ? ? ? ? throw "位置"; ?  ? ? else{ ?  ? ? ? ? q=p->next;x=q->score; ?  ? ? ? ? p->next=q->next; ?  ? ? ? ? delete q; ?  ? ? ? ? return x; ?  } ?  } ?  ??  int main() ? ? ? ? ? ? ?//主程序设计不是佷好,一定在不同处理后要输出来看看。  { ???int a[Max]={0,2}; ?  ? ? Score<int> s(a,4); ?  ? ? ?s.insert(1,98); ?  ? ? ?cout<<s.get(1); ?  ? ? ?s.locate(98); ?  ? ? ?s.Delete(1); ?  ? ? ? ? return 0; ?  } ?
   
? ? ?祝大家下次调试成功! 
        
            
        	
                        (编辑:泰州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |