大家帮小弟我看看哪错了?解决立刻结!
大家帮我看看哪错了?解决立刻结!!
问题在注释的地方说明
#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)
问题在注释的地方说明
#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)