本程序在LINUX下测序通过。编译命令: gcc -Wall -g link.c -o link
文件名为 link.c
#include
#include
typedef struct node{
int data;
struct node *next;
}Node;
void reverse(Node **head);
void free_link(Node **head);
int main()
{
int i = 0;
Node *head = NULL,*p = NULL,*q = NULL;
for (i =0; i < 10; i++)
{
p = (Node *)malloc(sizeof(Node));
p->data = i + 1;
p->next = NULL;
if (NULL == head)
{
head = p;
q = head;
}
else
{
q->next = p;
q = p;
}
}
q = head;
while(q)
{
printf("%5d",q->data);
q = q->next;
}
printf("\n\n");
reverse(&head);
q = head;
while(q)
{
printf("%5d",q->data);
q = q->next;
}
puts("\n\n");
free_link(&head);
return 0;
}
void reverse(Node **head)
{
Node *p = NULL,*q = NULL;
p = *head;
while(p)
{
if (NULL == q)
{
q = p;
*head = q;
p = p->next;
(*head)->next = NULL;
}
else
{
q = p;
p = p->next;
q->next = *head;
*head = q;
}
}
return;
}
void free_link(Node **head)
{
Node *p;
int i = 0;
while(*head)
{
p = *head;
*head = (*head)->next;
free(p);
i++;
}
printf("%5d\n\n Link free successful\n",i);
return;
}
阅读(995) | 评论(2) | 转发(0) |