《数据结构》同学链表程序的调试修改代码示例(有代表性,请看看)
2014-11-01 18:49
585 查看
下面是一个同学的程序,自己不能调试通过。现将调试修改代码示众,让大家学习。并讨论。
请大家注意红色字体注释。大多是粗心错误;不知道C++是选样学的。
原程序链接:http://blog.csdn.net/z1094219402/article/details/40559877
#include<iostream>
using namespace std;
//下面原来是单独存于头文件的 内容,现在放在一起
#ifndef LinkList_H
#define LinkList_H
template<class DataType>
struct Node
{ DataType data;
Node<DataType> * next;
};
template<class DataType>
class LinkList
{
public:
LinkList();
LinkList(DataType a[],int n);
~LinkList();
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
Node<DataType> *first;
};
#endif
//原来是类的函数定义文件内容
//#include<iostream> //因放到一个文件这些上面有
//using namespace std;
//#include"LinkList.h" //因代码在一个文件里,去掉这行
template<class DataType>
LinkList<DataType>::LinkList()
{ first=new Node<DataType>;
first->next=NULL;
}
template<class DataType>
LinkList<DataType>::LinkList(DataType a[],int n)
{ Node<DataType> * r,* s;
first=new Node<DataType>;
r=first;
for(int i=0;i<n;i++)
{
s=new Node<DataType>;
s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class DataType>
LinkList<DataType>::~LinkList()
{ Node<DataType> * q=NULL;
while(first!=NULL)
{ q=first;
first=first->next;
delete q;
}
}
template<class DataType>
void LinkList<DataType>::Insert (int i,DataType x)
{ Node<DataType> * p=first,* s=NULL;
int count=0;
while(p!=NULL && count<i-1)
{ p=p->next;
count++;
}
if(p==NULL)
throw"location";
else
{ s=new Node<DataType>;
//这里改了原来是NodeV
s->data=x; //这里改了原来是s
s->next=p->next;p->next=s;
}
}
template<class DataType>
DataType LinkList<DataType>::Delete (int i)
{ Node<DataType> * p=first, * q=NULL;
DataType x;
int count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)
throw"location" ;//这里加了一个;
else{
q=p->next;x=q->data;
p->next=q->next;
delete q;
return x; //这里改了原来是q
}
}
template<class DataType>
int LinkList<DataType>::Locate (DataType x)
{ Node<DataType> * p=first->next;
int count=1; //这里改了,原来是小写L,是数字1
while(p!=NULL)
{
if(p->data==x) return count;
p=p->next;
}
return 0;
}
template<class DataType>
void LinkList<DataType>::PrintList ()
{
Node<DataType> * p=first->next;
while(p!=NULL)
{ cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
//主程序内容
//#include<iostream> //因放到一个文件这些上面有
//using namespace std;
//#include"LinkList.cpp" //因代码在一个文件里,去掉这行
void main()
{ int r[5]={1,2,3,4,5};
LinkList<int>L(r,5); //这里改了,原来是linklist
cout<<"show the data that are before inserting: "<<endl;
L.PrintList();
try
{
L.Insert(2,3);
}
catch (char * s)
{
cout<<s<<endl;
}
cout<<"apply the order that insert the data and show them: "<<endl;
L.PrintList();
cout<<"the location of the element whose number is 5 directs: ";
cout<<L.Locate(5)<<endl;
cout<<"show the data that are infron the operation of delection: "<<endl;
L.PrintList();
try
{
L.Delete(1); //这里原来是delect(1;)
}
catch(char * s)
{
cout<<s<<endl; //这里改了 原来是end
}
L.PrintList();
}
请大家注意红色字体注释。大多是粗心错误;不知道C++是选样学的。
原程序链接:http://blog.csdn.net/z1094219402/article/details/40559877
#include<iostream>
using namespace std;
//下面原来是单独存于头文件的 内容,现在放在一起
#ifndef LinkList_H
#define LinkList_H
template<class DataType>
struct Node
{ DataType data;
Node<DataType> * next;
};
template<class DataType>
class LinkList
{
public:
LinkList();
LinkList(DataType a[],int n);
~LinkList();
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
Node<DataType> *first;
};
#endif
//原来是类的函数定义文件内容
//#include<iostream> //因放到一个文件这些上面有
//using namespace std;
//#include"LinkList.h" //因代码在一个文件里,去掉这行
template<class DataType>
LinkList<DataType>::LinkList()
{ first=new Node<DataType>;
first->next=NULL;
}
template<class DataType>
LinkList<DataType>::LinkList(DataType a[],int n)
{ Node<DataType> * r,* s;
first=new Node<DataType>;
r=first;
for(int i=0;i<n;i++)
{
s=new Node<DataType>;
s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class DataType>
LinkList<DataType>::~LinkList()
{ Node<DataType> * q=NULL;
while(first!=NULL)
{ q=first;
first=first->next;
delete q;
}
}
template<class DataType>
void LinkList<DataType>::Insert (int i,DataType x)
{ Node<DataType> * p=first,* s=NULL;
int count=0;
while(p!=NULL && count<i-1)
{ p=p->next;
count++;
}
if(p==NULL)
throw"location";
else
{ s=new Node<DataType>;
//这里改了原来是NodeV
s->data=x; //这里改了原来是s
s->next=p->next;p->next=s;
}
}
template<class DataType>
DataType LinkList<DataType>::Delete (int i)
{ Node<DataType> * p=first, * q=NULL;
DataType x;
int count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)
throw"location" ;//这里加了一个;
else{
q=p->next;x=q->data;
p->next=q->next;
delete q;
return x; //这里改了原来是q
}
}
template<class DataType>
int LinkList<DataType>::Locate (DataType x)
{ Node<DataType> * p=first->next;
int count=1; //这里改了,原来是小写L,是数字1
while(p!=NULL)
{
if(p->data==x) return count;
p=p->next;
}
return 0;
}
template<class DataType>
void LinkList<DataType>::PrintList ()
{
Node<DataType> * p=first->next;
while(p!=NULL)
{ cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
//主程序内容
//#include<iostream> //因放到一个文件这些上面有
//using namespace std;
//#include"LinkList.cpp" //因代码在一个文件里,去掉这行
void main()
{ int r[5]={1,2,3,4,5};
LinkList<int>L(r,5); //这里改了,原来是linklist
cout<<"show the data that are before inserting: "<<endl;
L.PrintList();
try
{
L.Insert(2,3);
}
catch (char * s)
{
cout<<s<<endl;
}
cout<<"apply the order that insert the data and show them: "<<endl;
L.PrintList();
cout<<"the location of the element whose number is 5 directs: ";
cout<<L.Locate(5)<<endl;
cout<<"show the data that are infron the operation of delection: "<<endl;
L.PrintList();
try
{
L.Delete(1); //这里原来是delect(1;)
}
catch(char * s)
{
cout<<s<<endl; //这里改了 原来是end
}
L.PrintList();
}
相关文章推荐
- 《数据结构》链表程序调试示例--有一定代表性请看看
- 《数据结构》链队列同学程序调试修改
- 《数据结构》一位同学链表程序调试--供大家参考
- 《数据结构》KMP算法程序调试示例-大家看看一定会有收获
- 解决调试asp.net程序时无法修改代码的问题
- 解决调试asp.net程序时无法修改代码的问题(提示: 当调试器已经附加到已经运行的进程...)
- 关于调试----在单步执行某个程序的中间修改源代码,当修改代码之后移动到下一步时,调试器自动在执行下一语句之前重新编译
- vs2010 修改程序代码,调试程序时,“当前不会命中断点,源代码与原始版本不同。” 的解决方案, 及vs调试时,不能修改源代码的解决方案。
- 解决在VS(winform)程序中无法在调试时修改代码的问题(也就是“编辑并继续”功能无效)
- Windows Forms 实现安全的多线程详解(附带程序代码示例) (摘自网络)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- ASP.NET程序中动态修改web.config中的设置项目(前台页面代码)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- 用asp实现的代码批量修改程序,fso相关
- “增强js程序代码的健壮性”之我见大量示例代码
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- Windows Forms 实现安全的多线程详解(附带程序代码示例)