您的位置:首页 > 理论基础 > 数据结构算法

《数据结构》同学链表程序的调试修改代码示例(有代表性,请看看)

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();    

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐