Form of declaring struct types:
Sturct struct type name
{
Member columns;
}
Three methods for defining struct type variables:
1. type name + variable name;
2. struct name
{
Member List
} Variable table column;
3. sturct
{
Member List
} Variable name list; // rarely used
Usage of typedef in struct
Typedef struct
{Char input;
A * p_input;
} A, * B;
Equivalent
Typedef struct
{Char input;
A * p_input;
} * B;
B is declared as the struct pointer type (A *): typedef A * B;
For example, typedef char * string // declare string as the character pointer type
String P, S [10]; // P is the character pointer variable, and s is the pointer array (including 10 elements)
The procedure is as follows:
# Include <stdio. h>
# Include <malloc. h>
Typedef struct lnode {
Int data;
Struct lnode * next;
} Lnode, * linklist;
Linklist createlist_l (linklist L, int N) // create a single-chain table
{
L = (linklist) malloc (sizeof (lnode ));
L-> next = NULL;
Linklist P;
For (INT I = N; I> 0; I --){
P = (linklist) malloc (sizeof (lnode ));
Scanf ("% d", & (p-> data ));
P-> next = L-> next;
L-> next = P; // insert to the header
}
Return L;
}
Void outputlist_l (linklist L, int N) // output the value of a single-chain table
{
Linklist P;
P = L-> next;
For (INT I = 0; I <n; I ++ ){
Printf ("% d", p-> data );
P = p-> next;
}
Printf ("/N ");
}
Linklist inversion (linklist L) // inverted linked list
{
Linklist newp = NULL, nowp, oldp; // defines three node tags
Oldp = L-> next;
While (oldp ){
Nowp = oldp;
Oldp = oldp-> next;
Nowp-> next = newp;
Newp = nowp;
}
L-> next = newp;
Return L;
}
Void main ()
{
Linklist L;
L = createlist_l (L, 4 );
Outputlist_l (L, 4 );
Int I;
L = inversion (L );
Outputlist_l (L, 4 );
}