No idea what is wrong??
I cant find any error with this~ I give me an infinite output~....
1 2 3 4 5
|
template <typename T>
struct Node{
T info;
Node<T>* next;
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
template <typename T>
bool Xstack<T>::swap(){
int count = 0;
Node<T>* ptr1 = top;
while(ptr1 != NULL){
ptr1 = ptr1->next;
count += 1;
}
if(count <2){
return false;
}
else{
Node<T>* ptr2 = top->next;
top->next = top->next->next;
ptr2->next = top;
top = ptr2;
return true;
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
template <typename T>
bool Xstack<T>::roll(int num){
if(isEmpty())
{return false;}
else if(num == 1)
{return true;}
else if(num == 2)
{
return swap();
}
else
{
int count = 0;
Node<T>* ptr1 = top;
Node<T>* ptr2 = top;
while(ptr1 != NULL)
{
ptr1 = ptr1->next;
count += 1;
}
ptr1 = top;
for(int i =0; i< num; i++)
{
ptr1 = ptr1->next;
}
ptr1->next = ptr2->next;
ptr2->next = ptr1;
}
}
|
Last edited on
> give me an infinite output
¿output? ¿where are you printing anything?
you've got a leak in line 29
Topic archived. No new replies allowed.