1.无头节点
struct stud_node *createlist()
{
struct stud_node *tail,*p,*head;
head = (struct stud_node *)malloc(sizeof(struct stud_node));
tail = head = NULL;
int num;
scanf("%d",&num);
p = (struct stud_node *)malloc(sizeof(struct stud_node));
while(num!=0)
{
p->num = num;
scanf("%s%d",p->name,&p->score);
if(head==NULL)
{
head = p;
head->next = NULL;
}
if(tail!=NULL)
{
tail->next = p;
}
tail = p;
tail->next = NULL;
p = (struct stud_node *)malloc(sizeof(struct stud_node));
scanf("%d",&num);
}
return head;
}
2.含有头节点
struct stud_node *createlist()
{
struct stud_node *tail,*p,*head;
head = (struct stud_node *)malloc(sizeof(struct stud_node));
tail = head = NULL;
int num;
scanf("%d",&num);
p = (struct stud_node *)malloc(sizeof(struct stud_node));
while(num!=0)
{
p->num = num;
scanf("%s%d",p->name,&p->score);
tail->next = p;
tail = p;
tail->next = NULL;
p = (struct stud_node *)malloc(sizeof(struct stud_node));
scanf("%d",&num);
}
return head->next;
}
3.头插法
struct ListNode *createlist()
{
struct ListNode *head,*p;
head = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next = NULL;
int data;
scanf("%d",&data);
while(~data)
{
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = data;
p->next = head->next;
head->next = p;
scanf("%d",&data);
}
return head->next;
}