写一个函数建立一个有3名学生数据的单向动态链表
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct ListNode {
int id;
int score;
ListNode *next;
};
int n;
struct ListNode* create()
{
ListNode *head;
ListNode *p1, *p2;
p1 = p2 = (struct ListNode *)malloc(sizeof(struct ListNode));
n = 0;
scanf("%ld,%ld", &p1->id, &p1->score);
head = NULL;
while (p1->id != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct ListNode *)malloc(sizeof(struct ListNode));
scanf("%ld,%ld", &p1->id, &p1->score);
}
p2->next = NULL;
return head;
}
int main()
{
ListNode* ptr;
ptr = create();
while (ptr != NULL)
{
printf("%ld,%ld\n", ptr->id, ptr->score);
ptr = ptr->next;
}
return 0;
}