struct link
{
struct link *next;
int data;
};
struct link *g_list = 0;
int link_init(struct link ** head)
{
int i = 0;
struct link *p = *head;
struct link *tmp = 0;
p = malloc(sizeof(struct link));
p->data = 0;
p->next = 0;
for(i = 0; i < 10; i ++)
{
tmp = malloc(sizeof(struct link));
memset(tmp, 0, sizeof(struct link));
tmp->data = 2*i + 1;
tmp->next = p;
p=tmp;
}
*head = p;
return 0;
}
void link_print(struct link *head)
{
int count = 0;
struct link *p = head;
printf("the link data is :/r/n");
while(p != 0)
{
printf("%2u ", p->data);
p = p->next;
count++;
}
printf("/r/ntotol count = %u/r/n", count);
}
void link_reverse(struct link **head)
{
struct link * p = *head;
struct link *q = p->next;
struct link *r = 0;
while(q != 0)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
(*head)->next = 0;
*head = p;
}
void link_free(struct link *head)
{
struct link *p = head;
struct link *tmp = 0;
while(p != 0)
{
tmp = p->next;
free(p);
p=tmp;
}
}
int main()
{
link_init(&g_list);
link_print(g_list);
link_reverse(&g_list);
link_print(g_list);
getchar();
link_reverse(&g_list);
link_print(g_list);
getchar();
link_free(g_list);
}