C++链表逆转操作

/*
    链表逆转操作
    Wirtten by: nick
    Date: 2012-10-18 19:56
*/

#include <iostream>
#include <iomanip>

using namespace std;

struct node
{
    int item;
    node *next;
    node(int x, node* t)
    {
        item = x;
        next = t;
    }
};

typedef node *link;

link reverse(link x)
{
    link t, y=x, r=0;
    while(y!=0)
    {
        t = y->next;
        y->next = r;
        r = y;
        y = t;
    }
    return r;
}

int main()
{
    int n,m;

    n=9;
    m=5;

    link t = new node(1, 0);
    t->next = t;
    link x = t;
    for(int i=2; i<=n; i++)
        x = (x->next = new node(i, t));
    x->next = 0;

    t = reverse(t);

    while(t != 0)
    {
        cout << setw(5) << t->item;
        t = t->next;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/wouldguan/p/2730140.html