死磕代码!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MALLOC_OK 1
#define MALLOC_NO 0
#define CREATE_OK 1
#define CREATE_NO 0
struct node
{
int num;
char name[20];
char sex;
int age;
char add[100];
struct node * next;
};
typedef struct node Node;
typedef Node * Link;
void create_link(Link * head)
{
*head = NULL;
}
int is_malloc_ok(Link new_node)
{
if(NULL == new_node)
{
printf("malloc error!\n");
return MALLOC_NO;
}
return MALLOC_OK;
}
int create_node(Link * new_node)
{
*new_node = (Link)malloc(sizeof(Node));
if(MALLOC_OK == is_malloc_ok(*new_node))
{
return CREATE_OK;
}
return CREATE_NO;
}
void insert_node_head(Link * head,Link new_node)
{
new_node->next = *head;
*head = new_node;
}
void display_link(Link head)
{
Link p = NULL;
p = head;
if( NULL == head)
{
printf("link is empty!\n");
return ;
}
while(p != NULL)
{
printf("%d\n",p->num);
p = p->next;
}
}
void release_link (Link * head)
{
Link p = NULL;
p = *head;
while(*head != NULL)
{
*head = (*head)->next;
free(p);
p = *head;
}
}
void insert_node_tail(Link * head,Link new_node)
{
Link p = NULL;
p = *head;
if(NULL == *head)
{
*head = new_node;
new_node->next = NULL;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
}
void insert_node_mid(Link *head,Link new_node,int loc)
{
Link p = NULL;
Link q = NULL;
p = q = *head;
//while( p->num != loc && p != NULL)
while( p != NULL && p->num != loc)
{
q = p;
p = p->next;
}
if(p == *head)
{
new_node->next = *head;
*head = new_node;
}
else
{
q->next = new_node;
new_node->next = p;
}
}
void insert_node_sort(Link * head,Link new_node)
{
Link p = NULL;
Link q = NULL;
p = q = *head;
while( p != NULL && p->num < new_node->num)
{
q = p;
p = p->next;
}
if(p == *head)
{
new_node->next = *head;
*head = new_node;
}
else
{
q->next = new_node;
new_node->next = p;
}
}
void delete_node(Link * head,int num)
{
Link p = NULL,q = NULL;
p = q = *head;
if(NULL == *head)
{
printf("delete: link is empty!\n");
}
else
{
while(p != NULL && p->num != num)
{
q = p;
p = p->next;
}
if(NULL == p)
{
printf("no such node!\n");
}
else if(p == *head)
{
*head = (*head)->next;
free(p);
}
else
{
q->next = p->next;
free(p);
}
}
}
void reverse_link(Link * head)
{
Link p1 = NULL,p2 = NULL,p3 = NULL;
if(NULL == *head)
{
printf("reverse: Link is empty!\n");
return;
}
p1= *head;
if(NULL == (*head)->next)
{
return;
}
p2 = p1->next;
p3 = p2->next;
while(p3 != NULL)
{
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
(*head)->next = NULL;
*head = p2;
}
int main()
{
Link head = NULL;
Link new_node = NULL;
int i;
int loc;
int num;
srand((unsigned)time(NULL));
create_link(&head);
for(i = 0; i < 4;i++)
{
if(CREATE_OK == create_node(&new_node))
{
new_node->num = rand() % 100;
//new_node->num = i + 1;
insert_node_sort(&head,new_node);
// insert_node_tail(&head,new_node);
// insert_node_head(&head,new_node);
}
}
#if 0
printf("please input location :\n");
scanf("%d",&loc);
if(CREATE_OK == create_node(&new_node))
{
printf("please input num:\n");
scanf("%d",&new_node->num);
insert_node_mid(&head,new_node,loc);
}
printf("link:\n");
display_link(head);
printf("please input delete num:\n");
scanf("%d",&num);
delete_node(&head,num);
printf("delete:\n");
#endif
display_link(head);
reverse_link(&head);
printf("reverse link:\n");
display_link(head);
release_link(&head);
display_link(head);
return 0;
}
Exit过于简单粗暴 但是会造成问题