设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 数据 创业者 手机
当前位置: 首页 > 服务器 > 安全 > 正文

《数据结构》链队列同学程序调试修改

发布时间:2021-05-16 04:44 所属栏目:53 来源:网络整理
导读:下面是一个同学的程序。调试没问题,但执行时错了。同学程序链接:http://blog.csdn.net/miumor/article/details/40891645 错误信息是: 上面内容拷自学生原来博文,一看错误是析构函数有问题。但程序广编译没问题,是什么原因呢?细看程序:析构函数在类的

下面是一个同学的程序。调试没问题,但执行时错了。同学程序链接:http://blog.csdn.net/miumor/article/details/40891645

错误信息是:

《数据结构》链队列同学程序调试修改

上面内容拷自学生原来博文,一看错误是析构函数有问题。但程序广编译没问题,是什么原因呢?细看程序:析构函数在类的声明中有声明,但在后面没写代码定义。大家还记得吗,你们一定学过,析构函数的特点:系统会给我们定义一个的,但是如果我们自己程序中声明了,系统就不会定义了。但当你自己只声明,不定义代码,而系统的又不给定义,析构函数就空了。所在改的方法就是:删除析构函数的声明,或者写出其代码,在这里显然是要写代码更合理些(你明白是为什么)。老师,有个错误我不知道怎么改。。


#include<iostream>
using namespace std;
const int QSize=100;
template<class Datatype>
struct Node
{Datatype data;
Node<Datatype> *next;
};
template<class Datatype>
class linkqueue
{public:
linkqueue();
~linkqueue();
void en(Datatype x);
Datatype chu();
Datatype get();
int empty();
private:
Node<int>*front,*rear;
};
template<class Datatype>
linkqueue<Datatype>::linkqueue()
{Node<Datatype>*s=NULL;
s=new Node<Datatype>;
s->next=NULL;
front=rear=s;
}


template<class Datatype> ? ? ? ? ? ? ? ? ? ? ?
linkqueue<Datatype>::~linkqueue() ? ?//调试修正方案:这个函数代码是要加上的。
{? Node<Datatype>*p=NULL;
? while(front!=NULL)
?p=front->next;
? delete front;
? front=p;
}


template<class Datatype>
void linkqueue<Datatype>::en(Datatype x)
{Node<Datatype>*s=NULL;
s=new Node<Datatype>;
s->data=x;
s->next=NULL;
rear->next=s;rear=s;
}
template<class Datatype>
Datatype linkqueue<Datatype>::chu()
{Node<Datatype>*p=NULL;
int x;
if(rear=front)throw"下溢";
p=front->next;
x=p->data;
front->next=p->next;
if(p->next=NULL)rear=front;
delete p;
return x;
}

template<class Datatype>
Datatype linkqueue<Datatype>::get()
{if(front!=rear)
? ? return front->next->data;
}
template<class Datatype>
int linkqueue<Datatype>::empty()
{if(front==rear)
return 1;
else
return 0;
}
void main()
{linkqueue<int>Q;
if(Q.empty())
cout<<"队列为空"<<endl;
else
cout<<"对15和10执行入队操作"<<endl;
try
{Q.en(15);
Q.en(10);
}
catch(char*wrong)
{cout<<wrong<<endl;
}
cout<<"查看队头元素"<<endl;
cout<<Q.get()<<endl;
cout<<"执行出队操作"<<endl;
try
{Q.chu();
}
? ? catch(char*wrong)
{cout<<wrong<<endl;
}
cout<<"查看队头元素"<<endl;
cout<<Q.get()<<endl;
}

《数据结构》链队列同学程序调试修改

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读