大家帮我看看哪错了?解决立刻结!!
问题在注释的地方说明
#include <iostream>
#include "d_random.h "// "d_random.h "的代码没贴出...但不影响大家看程序..
using namespace std;
template <typename T>
class Node{
public:
T Nodevalue;
Node <T> *next;
Node():next(null){};
Node(const T&value,Node <T> *nextNode):Nodevalue(value),next(nextNode){};
};
template <typename T>
void writeLikelistValue(Node <T> * front){
Node <T> *cur = front;
while(cur!=NULL){
cout < <cur-> Nodevalue < < " ";
cur = cur-> next;
}
}
template <typename T>
void eraseValue(Node <T> *front,const T& target){
Node <T> *current = front,*prev = NULL;
bool hasfind = false;
while(current != NULL && !hasfind){
if(current-> Nodevalue == target){
if(prev == NULL)
front = front-> next;
else
prev-> next = current-> next;
delete current;
hasfind = true;
}
else{
prev = current;
current = current-> next;
}
}
}
template <typename T>
Node <T> * getMax(Node <T> * front){
Node <T> *current = front,*max = front;
while(current != NULL){
if(current-> Nodevalue > max-> Nodevalue){
max =current;
current = current-> next;
}
}
return max;
}
int main(){
int size;
randomNumber ran;
Node <int> *front = NULL,*p;
cout < < "please input Node size: " < <endl;
cin> > size;
for(int i = 0;i < size;i++)
front = new Node <int> (ran.random(100),front);
cout < < "original Node is: " < <endl;
writeLikelistValue(front);
cout < <endl;
cout < < "sorted Node is: " < <endl;
while(front != NULL){//就是这里开始有问题了....什么也输不出了...
p = getMax(front);
cout < <p-> Nodevalue < < " ";
eraseValue(front,p-> Nodevalue);
}
return 0;
}
[解决办法]
template <typename T>
Node <T> * getMax(Node <T> * front){
Node <T> *current = front,*max = front;
while(current != NULL){
if(current-> Nodevalue > max-> Nodevalue){
max =current;
}
current = current-> next;
}
return max;
}
[解决办法]
说来所去,还是基础问题
修改两个地方:
(1)
template <typename T>
void eraseValue(Node <T> *front,const T& target){
Node <T> *current = front,*prev = NULL;
改为:
template <typename T>
void eraseValue(Node <T> *&front,const T& target){
Node <T> *current = front,*prev = NULL;
(2)
template <typename T>
Node <T> * getMax(Node <T> * front){
Node <T> *current = front,*max = front;
while(current != NULL){
if(current-> Nodevalue > max-> Nodevalue){
max =current;
//current = current-> next; 写在if外面
}
current = current-> next;
}
return max;
[解决办法]
if(current-> Nodevalue > max-> Nodevalue){
max =current;
current = current-> next;
}
这个应该这样改:
if(current-> Nodevalue > max-> Nodevalue){
max =current;
}
current = current-> next; // 注意循环因子的改变!
还有:
void eraseValue(Node <T> *front,const T& target){
这个地方这样改:
void eraseValue(Node <T> *&front,const T& target){ // 注意应该是引用!否则如何改变?
[解决办法]
程序停止不动,一般可考虑:
(1)死循环
(2)死锁(多线程下)
...
你的这个程序,打开任务管理,cpu 肯定100%死循环了,看看含有循环
的代码,一般能够看出问题。
看出那个删除函数问题倒是需要一些经验。
不用急,基础的书本在身边,记不住了没事翻翻,很多东西积累经验就轻车熟路了。
[解决办法]
因为 eraseValue 这个函数的目的是要删除结点,并且需要改变头指针的值!如果是按值传递的话,就无法改变头指针的值了(只能改变它的拷贝而已),因此将它改为引用(也可以改成二级指针,不过这通常是C程序员的做法).
[解决办法]
了解调试,首先要知道 "断点 "这个概念.断点就是程序运行中可能会中断的地方,方便开发者在程序运行的过程中查看程序当前的运行状态,比如变量的值,函数的返回值等等.究竟怎么使用断点呢? 1.F9在当前光标所在的行下断点,如果当前行已经有断点,则取消断点. F5调试状态运行程序,程序执行到有断点的地方会停下来. F10单步执行程序. CTRL+F10运行到光标所在行. F11和F10的区别是,如果当前执行语句是函数调用,则会进入函数里面. SHIFT+F11跳出当前所在函数. 特别说明: a.有的地方不能下断点.比如空行,基本类型定义语句(不初始化),等等非执行语句.比如 int i; // 此行不能下断点 int j=0; // 这里可以下 CString str;