given the following types and variables have been defined and are available for use:
struct nodetype
{
char *Airport;
struct nodetype *Link;
};
typedef nodetype NodeType;
And given that L =={DUS, ORD, DAN} before hand.
I'm trying to write a function Reverse(&L) that reverses the order of the list. I've copied the specification for the problem into the comments just before the function.
What I have thus far does remove a node, but when I start building the new node, I get lost. I can't declare any more variables than are listed in the function spec though it seems to me that I need one more temp variable to pull this off.
/*Write a function, Reverse(&L), which reverses the order of the nodes
on list L. Must write two subroutines to remove the first node N of a
list L1 and to insert a node N as the new first node on a list L2. Then,
starting with an empty list L2=NULL, successively remove nodes from L1
and insert them on L2 until L1 is empty.*/
void ReverseList(NodeType **L)
{
NodeType *M, //temp node
*N; //new list
N=NULL;
*L=(*L)->Link; //move in from the head node.
while(*L!=NULL){
//remove the first node from L.
M=*L; //M equals first node
//N=(*L)->Link; // N equals second node.
*L=(*L)->Link; //remove first node.
//add the removed first node to the head of node.
if(N==NULL){N=(NodeType*)malloc(sizeof(NodeType)); }
N->Link=M; //add node to head
}
}