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

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

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

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

祝大家下次调试成功!

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...