|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
struct school
{
int date;
struct school *next;
};
void scan(struct school *student)
{
printf("请输入数据");
scanf("%d",student->date);
}
void addstudent(struct school **head)
{
struct school *student=NULL,*temp=NULL;
student=(struct school *)malloc(sizeof(struct school));
if(student==NULL)
{
printf("内存分配失败");
exit(1);
}
scan(student);
printf("666");
if(*head!=NULL)
{
temp=*head;
*head=student;
student->next=temp;
}
else
{
*head=student;
student->next=NULL;
}
}
void printstudent(struct school *head)
{
struct school *student;
student=head;
while(student!=-NULL)
{
printf("%d",student->date);
student=student->next;
}
}
int main()
{
struct school *head=NULL;
char ch;
while(1)
{ printf("是否输入y/n?");
scanf("%c",&ch);
if(ch=='y')
{
addstudent(&head);
}
if(ch=='n')
{
break;
}
}
printf("是否打印");
while(ch!='y'||ch!='n')
{
scanf("%c",&ch);
}
if(ch=='y')
{
printstudent(head);
}
return 0;
}
- #include<stdio.h>
- #include<stdlib.h>
- struct school
- {
- int date;
- struct school *next;
- };
- void scan(struct school *student)
- {
- printf("请输入数据");
- scanf("%d",&student->date); // 关键错误 &
- student->next = NULL;
- }
- void addstudent(struct school **head)
- {
- struct school *student=NULL,*temp=NULL;
- student=(struct school *)malloc(sizeof(struct school));
- if(student==NULL)
- {
- printf("内存分配失败");
- exit(1);
- }
- scan(student);
- if(*head!=NULL)
- {
- temp=*head;
- *head=student;
- (*head)->next=temp;
- }
- else
- {
- *head=student;
- }
- }
- void printstudent(struct school *head)
- {
- struct school *student;
- student=head;
- while(student!=NULL)
- {
- printf("%d\n",student->date);
- student=student->next;
- }
- }
- int main()
- {
- struct school *head=NULL;
- char ch;
- while(1)
- { printf("是否输入y/n?");
- scanf("%c",&ch);
- if(ch=='y')
- {
- addstudent(&head);
- }
- if(ch=='n')
- {
- break;
- }
- while(getchar() != '\n') continue;
- }
-
- while(getchar() != '\n') continue;
- printf("是否打印y/n?");
- scanf("%c",&ch);
- while(ch!='y'||ch!='n')
- {
-
- if(ch=='y')
- {
- printstudent(head);
- break;
- }
- while(getchar() != '\n') continue;
- printf("是否打印y/n?");
- scanf("%c",&ch);
- }
- return 0;
- }
复制代码
|
|