View plain
# Include <stdio. h>
# Include <malloc. h>
Typedef struct test
{
Int;
Struct test * next;
} Lianbiao;
Lianbiao * create () // create a linked list
{
Lianbiao * head, * temp;
Int I;
Printf ("Enter some integers and end with 0 :");
Temp = head = (lianbiao *) malloc (sizeof (lianbiao ));
Scanf ("% d", & I );
If (I = 0)
{
Printf ("you input zero! Exit now! ");
Return head;
}
Head-> a = I;
Head-> next = NULL;
While (1)
{
Scanf ("% d", & I );
If (I = 0)
Break;
Else
{
Temp-> next = (lianbiao *) malloc (sizeof (lianbiao ));
Temp = temp-> next;
Temp-> a = I;
Temp-> next = NULL;
}
}
Return head;
}
Lianbiao * reverse (lianbiao * p) // reverse linked list
{
Lianbiao * temp1, * temp2;
If (p-> next = NULL)
{
Printf ("the number of linked list nodes is less than 2 and cannot be reversed! \ N ");
Return p;
}
Int flag = 0;
While (p-> next! = NULL)
{
If (flag = 0)
{
Temp1 = p;
Temp2 = p-> next;
P-> next = NULL;
P = temp2;
Flag = 1;
Continue;
}
Temp2 = p-> next;
P-> next = temp1;
Temp1 = p;
P = temp2;
}
P-> next = temp1; // point the End Node of the original linked list to the second to last node.
Return p;
}
Void print (lianbiao * p)
{
While (p-> next! = NULL)
{
Printf ("% d", p-> );
P = p-> next;
}
Printf ("% d", p-> );
Printf ("\ n ");
}
Int main ()
{
Lianbiao * temp;
Temp = create ();
Print (temp); // print the original linked list
Print (reverse (temp); // print the reverse linked list
Return 0;
}
The author's "simonjay2007 column"