- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct all_sch_list
{
char name[10];
char scale[10];
struct all_sch_list *next;
}all_sch_list_t, *sch_list_t;
static sch_list_t sch_list = NULL;
int n = 0;
sch_list_t list_add(char *name, char *scale)
{
sch_list_t new_node,prev,current;
new_node = (sch_list_t)malloc(sizeof(all_sch_list_t));
strcpy(new_node->name, name);
strcpy(new_node->scale, scale);
new_node->next = NULL;
n++;
printf("%s n:%d\n", __FUNCTION__, n);
if(sch_list == NULL)
{
printf("000000\n");
sch_list = new_node;
sch_list->next = NULL;
}
else
{
printf("1111111\n");
current = sch_list;
while(current)
{
prev = current;
current = current->next;
}
prev->next = new_node;
}
while(sch_list != NULL)
{
printf("sch list name:%s scale:%s\n", sch_list->name, sch_list->scale);
sch_list = sch_list->next;
}
return sch_list;
}
static void print_list()
{
while(sch_list != NULL)
{
printf("name:%s scale:%s\n", sch_list->name, sch_list->scale);
sch_list = sch_list->next;
}
}
void main()
{
char name[10] = "name1";
char scale[10] = "Good";
list_add(name, scale);
char name2[10] = "name2";
char scale2[10] = "Very well";
list_add(name2, scale2);
print_list();
}
运行后结果:
list_add n:1
000000
sch list name:name1 scale:Good
list_add n:2
000000
sch list name:name1 scale:Very well
为什么每次list_add()后sch_list内容仍是空啊? |
|