单链表的建立 插入问题
#include<stdio.h>#include<stdlib.h>
#include<string.h>
struct stud_node
{
char name[20];
int score,num;
struct stud_node *next ;
};
struct stud_node *Create_Stu_Doc();
struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stud);
int main (void)
{
char name[20];
int score,num,choice;
struct stud_node *head,*p;
int size=sizeof(struct
stud_node);
do
{
printf("1: Creat 2:Insert 3:Delete 4:Print 0:Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=Create_Stu_Doc();
break;
case 0: break;
}
}while(choice!=0);
return 0;
}
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 num,name and score:\n");
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);
scanf("%d%s%d",&num,name,&score);
}
return head;
}
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;
}
else
{
ptr2->next=ptr;
ptr->next=NULL;
}
}
return head;
}