首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求教,这个单链表为什么getData函数出有关问题

2012-10-20 
求教,这个单链表为什么getData函数出问题?C/C++ code#include Student.h#include iostreamusing names

求教,这个单链表为什么getData函数出问题?

C/C++ code
#include "Student.h"#include <iostream>using namespace std;template<class T>class linkNode{public:    linkNode(linkNode<T>* ptr=NULL){        link=ptr;    }    linkNode(T& x,linkNode<T>* ptr=NULL){        data=x;        link=ptr;    }    T* data;    linkNode* link;};template<class T>class List:public linkNode<T>{protected:    linkNode<T>* first;public:    List();    List(T& x);    List(List<T>& L);    ~List();    void makeEmpty();    int length();    linkNode<T>* getHead();    linkNode<T>* search(T& x);    linkNode<T>* locate(int i);    bool getData(int i,T& x);    void setData(int i,T& x);    bool insert(int i,T& x);    bool remove(int i,T& x);    bool isEmpty();    void sort();    List<T>&operator==(List<T>& L);    List<T>&reverse(List<T>& L);};template<class T>List<T>::List(){    first=new linkNode<T>();}template<class T>List<T>::List(T& x){    first=new linkNode<T>(x);}template<class T>List<T>::List(List<T>& L){    List<T>* newList;    linkNode<T>* newNode;    linkNode<T>* current;    current=first->link;    newList->first=first;    while (current->link!=NULL)    {        newNode->data=current->data;        newNode->link=current->link;        current=current->link;        newNode=newNode->link;    }}template<class T>List<T>::~List(){    makeEmpty();}template<class T>void List<T>::makeEmpty(){    linkNode<T>* del;    while(first->link!=NULL){        del=first->link;        first->link=del->link;        delete del;    }}template<class T>int List<T>::length(){    linkNode<T>* q;    int count=0;    while(q->link!=NULL){        count++;        q=q->link;    }    return count;}template<class T>linkNode<T>* List<T>::getHead(){    return first->link;}template<class T>linkNode<T>* List<T>::search(T& x){    linkNode<T>* current;    current=first->link;    while(current->link!=NULL){        if(current->data==x)            break;        current=current->link;    }    return current;}template<class T>linkNode<T>* List<T>::locate(int i){    linkNode<T>* current;    current=first->link;    for (int j=1;j<i;j++)    {        current=current->link;    }    return current;}template<class T>bool List<T>::getData(int i,T& x){    linkNode<T>* a;    a=locate(i);    if (a!=NULL)    {        x=a->data;        return true;    }    return false;}template<class T>void List<T>::setData(int i,T& x){    locate(i)->data=x;}template<class T>bool List<T>::insert(int i,T& x){    linkNode<T>* newNode=new linkNode<T>(x);    if(newNode==NULL)        cerr<<"error"<<endl;    linkNode<T>* current;    current=locate(i);    if(current==NULL)        return false;    else{        newNode->link=current->link;        current->link=newNode;    }    return true;}template<class T>bool List<T>::remove(int i,T& x){    linkNode<T>* current;    linkNode<T>* del;    current=locate(i-1);    if(current==NULL)        return false;    del=current->link;    x=del->data;    current=del->link;    delete del;    return true;}template<class T>bool List<T>::isEmpty(){    if(first->link==NULL)        return true;    else        return false;}void main(){    List<int> ls;    int t,k;    ls.insert(1,t);    ls.getData(1,k);    cout<<k<<endl;    }


------解决方案--------------------


探讨

引用:

第一处:
T data;
linkNode* link;
第二处:
linkNode<T>* List<T>::locate(int i){
linkNode<T>* current;
current=first;
for (int j=0;j<i;j++)
{
if(current!=NULL &amp;amp;&amp;amp; curren……

热点排行