I wrote a single-chain table reversal program. The record is as follows. please correct me.
# Include <iostream>
Using namespace std;
Struct Node
{
Int data;
Node * next;
};
Node * reverse (Node * head)
{
If (head-> next = NULL)
Return head;
Node * t1 = head-> next;
If (t1 = NULL)
Return head;
Node * t2 = t1-> next;
T1-> next = NULL;
While (t2-> next! = NULL)
{
Node * t3 = t2-> next;
T2-> next = t1;
T1 = t2;
T2 = t3;
}
T2-> next = t1;
Head-> next = t2;
Return head;
}
Void main ()
{
Node * list = new Node [5];
Node * head = new Node;
Head-> next = & list [0];
List [0]. data = 1;
List [0]. next = & list [1];
List [1]. data = 2;
List [1]. next = & list [2];
List [2]. data = 3;
List [2]. next = & list [3];
List [3]. data = 4;
List [3]. next = & list [4];
List [4]. data = 5;
List [4]. next = NULL;
Node * p;
P = head;
While (p-> next! = NULL)
{
Printf ("% d \ n", p-> next-> data );
P = p-> next;
}
Reverse (head );
Printf ("reverse \ n ");
P = head;
While (p-> next! = NULL)
{
Printf ("% d \ n", p-> next-> data );
P = p-> next;
}
Delete head;
Delete [] list;
}
Running result:
Author: "cainiao changes"