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

模板类函数的构造函数解决办法

2012-03-03 
模板类函数的构造函数原代码如下://LinkList.h#include iostreamusing namespace stdtemplate class T

模板类函数的构造函数
原代码如下:
//LinkList.h
#include <iostream>
using namespace std;
template <class T>
struct Node{
T data;
Node *next;
};
template <class T>
class LinkList{
public:
LinkList(){first = new Node<T>;first->next = NULL;}
LinkList(Node<T> *first);
LinkList(T a[],int n);
~LinkList(){destroy();}
int length(Node<T> *first);
T Get(int i);
int Locate(T x);
Node<T>* Insert(int i,T x);
Node<T>* Delete(int i);
//T Delete(T x);当T为int时与int Delete(int i)相同,不是重构函数.
Node<T>* Reverse (Node<T> *first);
void PrintList(Node<T> *first);
private:
Node <T> *first;
void destroy();
};
template <class T>
LinkList<T>::LinkList(T a[],int n){
first = new Node<T>;
Node <T> *r = first;//创建尾指针
for (int i = 0;i <n;i++){
p = new Node<T>;
if (p != NULL){
r->next = p;//把结点挂在尾指针后
p->data = a[i];
r = p;
}
else {
cout<<"内存已满"<<endl;
exit(0);
}
}
r->next = NULL;//将尾指针指针域置空
}
template <class T>
LinkList<T>::LinkList(Node<T> *first){
Node<T> *p,*q;
p = new Node<T>;
if (p != NULL){
first->next = p;
while (1){
q = new Node<T>;
if (q != NULL){
cout<<"请输入数据(输入不是整数为结束)"<<endl;
cin>>q->data;
if (cin.fail()){
p->next = NULL;
delete q;
cin.clear();
}
else{
p->next = q;
p = p->next;
}
}
else 
cout<<"内存已满或已被占用!"<<endl;
}
}
else
cout<<"内存已满或已被占用!"<<endl;
}
template <class T>
int LinkList<T>::length(Node<T> *first){
int i = 0;//累加器i
while(first->next != NULL){
++i;
first = first->next;
}
return i;
}
template <class T>
void LinkList<T>::destroy(){
while(first->next != NULL){
Node<T> *p = first;
first = first->next;
delete p;
}
}
template <class T>
T LinkList<T>::Get(int i){
Node <T> *p; p = first->next;int j = 1;
while(p != NULL && j<i){
p = p->next;
++j;
}
if (p == NULL)
throw "位置";
else 
return p->data;
}
template <class T>
int LinkList<T>::Locate(T x){
Node<T> *p ;p = first->next;
int i =1;
while(p != NULL && p->data != x)
++i;
if(p == NULL)
throw"位置";
else
return i;
}
template <class T>
Node<T>* LinkList<T>::Insert(int i,T x){
Node<T> *p,*s;p = first;int j = 0;
while(p && j<i-1){
p = p->next;
++j;
}
if (!p)throw"位置";
else{
s = new Node<T>;
s->data = x;
s->next = p->next;
p->next = s;
return first;
}
}
template <class T>
Node<T>*LinkList<T>::Delete(int i){
Node<T> *p,*q; p = first; int j = 0;
while(p != NULL && j<i-1){
p = p->next;
++j;
}
if(p == NULL || p->next == NULL)throw"位置";//i超出链表长度
//所删结点为表尾结点,即q->next不存在
else if(p->next->next == NULL){
q = p->next;
p->next = NULL;
delete q;
}
//所删结点在表头或表中
else{
q = p->next;
p->next = q->next;
delete q;
}
return first;
}
/*template <class T>
T LinkList<T>::Delete(T x){
Node<T> *q,*p;p = first;q = p->next;//初始化,p从第一个结点开始,q作为p的后继


while(p != NULL && q->data != x ){
p = p->next;//指针p前移
q = p->next;//q始终作为p的后继
}
if(p == NULL)throw"位置";
else if(q->next == NULL){
p->next = NULL;
delete q;
}
else{
p->next = q->next;
delete q;
}
}*/
template <class T>
Node<T>* LinkList<T>::Reverse (Node<T> *first){
Node<T> *p,*q,*r;
p = first;q = p->next;
if (first == NULL)
cout<<"链表为空!"<<endl;
else{
while (q->next->next != NULL){
r = q->next;
q->next = p;
q = q;
q = r;
}
r->next = q;
first = r;
return first;
}
}
template <class T>
void LinkList<T>::PrintList(Node<T> *first){
Node<T> *p = first;
if (p != NULL)
do{
cout<<p->data<<" ";
p = p->next;
}while(p != NULL);
else 
cout<<"此链表为空!";
cout<<endl;
}
//LinkList.cpp
//LinkList.cpp : 定义控制台应用程序的入口点。
//
#include "LinkList.h"
const int MAX = 20;
int main(){
Node<int> *head = NULL;
LinkList<int> linklist;
int k ;
UP:while(1){
cout<<endl;
cout<<"1 ——创建链表"<<endl;
cout<<"2 ——计算链表长度"<<endl;
cout<<"3 ——链表第i个结点的数据域"<<endl;
cout<<"4 ——求数据x是链表中第几个结点"<<endl;
cout<<"5 ——在第i个结点前插入数据x"<<endl;
cout<<"6 ——删除链表中第i个结点"<<endl;
cout<<"7 ——逆转链表"<<endl;
cout<<"8 ——"<<endl;
cout<<"9 ——"<<endl;
cout<<"10——输出链表"<<endl;
cout<<"0 ——退出"<<endl;
cout<<"请选择:"<<endl;
cin>>k;
switch(k){
case 0: exit(0);
case 1:
while(1){
int k_01;
cout<<endl;
cout<<"1——手动输入数据创建链表"<<endl;
cout<<"2——根据数组与长度创建链表"<<endl;
cout<<"3——返回上一层"<<endl;
cin>>k_01;
switch(k_01){
case 1:{
LinkList<int> linklist = LinkList<int>(head);
continue;
}
case 2:{
int b[MAX],len;
cout<<"请输入数组的长度len"<<endl;
cin>>len;
if (len > MAX)
cout<<"输入长度过长,请输入小于20的数字"<<endl;
else{
cout<<"请输入数组数据;"<<endl;
for (int i = 0; i< len;i++)
cin>>b[i];
}
//LinkList<int> linklist = LinkList<int> (b[MAX],len);这里出错!
continue;
}
case 3:
goto UP;
}
}
continue;
case 2:
cout<<"链表的长度为;"<<linklist.length(head);
continue;
case 3:
int i_03;
//int Get(int i);
cout<<"请输入想求的第i项"<<endl;
cin>>i_03;
cout<<"第"<<i_03<<"项的数据域为:"<<linklist.Get(i_03)<<endl;
continue;
case 4:
int x_04;
//int Locate(int x);
cout<<"请输入数据x"<<endl;
cin>>x_04;
cout<<x_04<<"在链表第"<<linklist.Locate(x_04)<<endl;
continue;
case 5:
int i_05,x_05;
cout<<"请输入在第几个数前插入"<<endl;
cin>>i_05;
cout<<"请输入插入的数据域为:"<<endl;
cin>>x_05;
//int Insert(int i,int x);
linklist.Insert(i_05,x_05);
continue;
case 6:
cout<<"请输入要删除的结点i"<<endl;


int i_06;
cin>>i_06;
//int Delete(int i);
linklist.Delete(i_06);
continue;
case 7:
linklist.Reverse(head);
continue;
case 8:
case 9:
case 10: linklist.PrintList(head);
continue;
default:
cout<<"输入错误!请重新输入."<<endl;
continue;
}
}
return 0;
}
请告诉我错误的原因及如何改正,谢谢。

[解决办法]
1、构造函数LinkList(T a[],int n)定义时,参数1为数组,而你的调用的时候LinkList<int> (b[MAX],len),b[MAX]是一个数组的一个元素,所以参数不匹配,可以改为LinkList<int> (b,len)
2、first = new Node<T>;Node <T> *r = first;有问题,因为r为指针,first不是指针,改为Node<T> *first, *r;r = first; //创建尾指针
同理p = new Node<T>改为Node<T> *p;

热点排行
Bad Request.