#include"stdio.h"
#include"stdlib.h"
struct jiedian
{
char ming[20];
int xuehao;
struct jiedian* next;
};
int i;
struct jiedian* coo()
{
i = 0;
struct jiedain* head = NULL;
struct jiedain* pnew, * pend;
pnew = pend = (struct jiedian*)malloc(sizeof(struct jiedian));
printf("请输入学生信息姓名和学号;\n");
scanf("%s", &pnew->ming);
scanf("%d", &pnew->xuehao);
while (pnew->xuehao != 0)
{
i++;
if (i == 1)
{
pnew->next = head;
pend = pnew;
head = pnew;
}
else
{
pnew->next = NULL;
pend->next = pnew;
pend = pnew;
}
pnew = (struct jiedian*)malloc(sizeof(struct jiedian));
scanf("%s", &pnew->ming);
scanf("%d", &pnew->xuehao);
}
free(pnew);
return head;
}
void print(struct jiedian* head)
{
int p;
struct jiedian* l;
l = head;
if (i = 0)
{
printf("没有一个成员");
}
else
{
for (p = 0;p < i;p++)
{
printf("第%d个成员是:\n", i);
if (i = 1)
{
printf("%s", l->ming);
printf("%d", l->xuehao);
}
else
{
l = l->next;
printf("%s", l->ming);
printf("%d", l->xuehao);
}
}
}
}
int main()
{
struct jiedian* head;
head = coo();
print(head);
return 0;
}
这样写链表不是最合理的方法,改动处见注释,供参考:
#include"stdio.h"
#include"stdlib.h"
struct jiedian{
char ming[20];
int xuehao;
struct jiedian* next;
};
int i;
struct jiedian* coo()
{
i = 0;
struct jiedian* head = NULL;
//struct jiedain* head = NULL; 修改
struct jiedian* pnew, * pend;
//struct jiedain* pnew, * pend;修改
pnew = pend = (struct jiedian*)malloc(sizeof(struct jiedian));
printf("请输入学生信息姓名和学号;\n");
scanf("%s", pnew->ming);
//scanf("%s", &pnew->ming); 修改
scanf("%d", &pnew->xuehao);
while (pnew->xuehao != 0)
{
i++;
if (i == 1)
{
pnew->next = head;
pend = pnew;
head = pnew;
}
else
{
pnew->next = NULL;
pend->next = pnew;
pend = pnew;
}
pnew = (struct jiedian*)malloc(sizeof(struct jiedian));
scanf("%s", pnew->ming);
//scanf("%s", &pnew->ming); 修改
scanf("%d", &pnew->xuehao);
}
free(pnew);
return head;
}
void print(struct jiedian* head)
{
int p;
struct jiedian* l;
l = head;
if (i == 0)//if (i = 0) 修改
{
printf("没有一个成员");
}
else
{
for (p = 0;p < i;p++)
{
printf("第%d个成员是:\n", p);
//printf("第%d个成员是:\n", i); 修改
//if (i == 1) //if (i = 1) 修改
//{
// printf("%s", l->ming);
// printf("%d", l->xuehao);
//}
//else 修改
{
printf("%s", l->ming);
printf(" %d\n", l->xuehao);
//printf("%d", l->xuehao); 修改
}
l = l->next; //修改
}
}
}
int main()
{
struct jiedian* head;
head = coo();
print(head);
return 0;
}
额,你这个命名。看我的博客吧,我有写一篇c语言的单链表。