Reverse Order of a single-chain table:
# Include <stdio. h>
Struct Link
{
Int value;
Struct link * next;
};
Void output_link (struct link * head)
{
Printf ("");
While (Head! = NULL)
{
Printf ("[% d]", head-> value );
Head = head-> next;
}
}
Void rever_output_link (struct link * head)
{
If (head ){
Rever_output_link (Head-> next );
Printf ("[% d]", head-> value );
}
}
Struct link * create_link (int n)
{
Struct link * head = malloc (sizeof (struct link ));
Struct link * TMP, * pre;
Int I;
Pre = head;
Head-> value = rand ();
For (I = 0; I <n-1; I ++)
{
TMP = malloc (sizeof (struct link ));
TMP-> value = rand ();
TMP-> next = NULL;
Pre-> next = TMP;
Pre = pre-> next;
}
Return head;
}
Struct link * revert_link (struct link * head)
{
Struct link * left, * mid, * TMP;
Left = head;
Mid = head-> next;
Left-> next = NULL;
While (Mid! = NULL)
{
TMP = mid-> next;
Mid-> next = left;
Left = mid;
Mid = TMP;
}
Return left;
}
Void del_link (struct link * head)
{
Struct link * TMP;
While (Head! = NULL)
{
TMP = head;
Head = head-> next;
Free (TMP );
}
}
Int main ()
{
Struct link * head = create_link (6 );
Struct link * TMP;
Output_link (head );
TMP = revert_link (head );
Output_link (TMP );
Del_link (head );
Return 0;
}