struct stud_node{
int num;
char name[20];
int score;
struct stud_node *next;
};
////结构体,结构体指针,内存分配
//struct stud_node *create_stu_doc();
//struct stud_node *insertdoc(struct stud_node *head,struct stud_node *stud);
//struct stud_node *deletedoc(struct stud_node *head,int num);
//void print_stud_doc(struct stud_node *head);
int main(){
struct stud_node *head,*p;
int choice,num,score;
char name[20];
int size = sizeof(struct stud_node);//
choice = 8;
while(choice!=0){
printf("choice");
scanf("%d",&choice);
switch(choice){
case 1:
head = create_stu_doc();
break;
case 2:
printf("input");
scanf("%d%s%d",&num,name,&score);
p = (struct stud_node*)malloc(size);
p->num = num;
strcpy(p->name,name);
p->score = score;
head=insertdoc(head,p);
break;
case 3:
scanf("%d",&num);
head=deletedoc(head,num);
break;
case 4:
printf("print");
print_stud_doc(head);
break;
case 0:
break;
}
}
}
struct stud_node *insertdoc(struct stud_node *head,struct stud_node *stud){
struct stud_node *ptr,*ptr1,*ptr2;
ptr2 = head;
ptr = stud;
if(head == NULL){
head = ptr;
head->next=NULL;
}
else{
while((ptr->num>ptr2->num)&&(ptr2->next!=NULL)){
ptr1 = ptr2;
ptr2 = ptr2->next;
}
if(ptr->num<=ptr2->num){
if(head==ptr2){
head = ptr;
}
else{
ptr1->next = ptr;
}
ptr->next = ptr2;
}
return head;
}
}
//返回结构体类型的指针的函数
struct stud_node *create_stu_doc(){
struct stud_node *head,*p;
int num,score;
char name[20];
int size = sizeof(struct stud_node);//动态分配内存
head = NULL;
printf("input");
scanf("%d%s%d",&num,name,&score);
while(num!=0){
p = (struct stud_node *)malloc(size);//指针动态分配内存
p->num=num;
strcpy(p->name,name);
p->score=score;//
head = insertdoc(head,p);
printf("input");
scanf("%d%s%d",&num,name,&score);//调用插入函数,,,
}
return head;//返回的是结构体的指针
}
struct stud_node *deletedoc(struct stud_node *head,int num){
struct stud_node *ptr1,*ptr2;
while(head!=NULL&&head->num==num){
ptr2 = head;
head = head->next;
free(ptr2);
}
if(head==NULL){
ptr1 = head;
ptr2 = head->next;
while(ptr2!=NULL){
if(ptr2->num == num){
ptr1->next = ptr2->next;
free(ptr2);
}
else{
ptr1 = ptr2;
ptr2=ptr1->next;
}
}
return head;
}
}
//
void print_stud_doc(struct stud_node *head){
struct stud_node *ptr;
if(head==NULL){
return;
}
for(ptr=head;ptr!=NULL;ptr=ptr->next){//遍历结构体的方法
printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);
}
}
int prime(int n){
for(int i=0;i<100;i++){
n--;
}
}
int main(){
int n = 2,count = 0;
FILE *fp;//定义文件指针
if((fp=fopen("prime.txt","w"))==NULL){//文件指针打开文件
exit(0);
}
while(count<500){
if(prime(n)!=0){
count++;
fprintf(fp,"%d",n);//用fprint把n录入文件
}
n++;
}
if(fclose(fp)){//关闭
exit(0);
}
}