示例代码:
#include <iostream>
using namespace std;
struct Item
{
char c;
Item *next;
};
Item *rev(Item *x) //循环实现
{
Item *prev = NULL, //先前
*curr = x; //当前
while (curr)
{
Item *next = curr->next ;
curr->next = prev;
prev = curr; //先前=当前
curr = next; //当前=next
}
return prev;
}
Item *rev1(Item *x) //递归实现
{
if(x->next==NULL)
return x;
else
{
Item *ph=rev(x->next);
x->next->next=x;
x->next=NULL;
return ph;
}
}
void Routine2(Item *x)
{
Item *curr = x;
while(curr)
{
cout<<curr->c<<" ";
curr = curr->next;
}
}
int main(void)
{
Item *x,
d={'d',NULL},
c={'c',&d},
b={'b',&c},
a={'a',&b};
x=rev(&a);
Routine2(x);
return 0;
}