微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

《数据结构》链表程序调试示例--有一定代表性请看看

下面的一个同学程序,编译和链接均没问题,只是运行出面错误。现将程序示众,大家细看我改过的地方面,用红色标记

还是一个粗心,请细分每个功能函块,要画图来推推。要说明的是,我没有从功能上完善同学的程序。程序有好多改进的地方。

同学程序链接是: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;  
}  

     祝大家下次调试成功!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐