// stu_list.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stu
{
int num;
int age;
float score;
char name[20+1];
struct stu *next;
}student;
student *stu_1 = NULL;
//创建链表
void initList()
{
//student *head; //头结点
stu_1 = (student *)malloc(sizeof(student));
stu_1->next = NULL;
//return stu_1;
}
int insertList(student* stu,int num,char *name,int age,float score,int n)
{
student *temp;
temp = stu_1;
student *newlist = NULL; //新结点
newlist = ( student *)malloc(sizeof(student));
newlist->num = num;
newlist->age = age;
newlist->score = score;
strcpy(newlist->name ,name);
if (n==1)
{
stu_1=newlist;
stu_1->next = NULL;
}
else
{
while(temp->next != NULL)
temp=temp->next;
newlist->next = temp->next;
newlist->next = NULL;
temp->next = newlist;
}
//free(newlist);
return 0;
}
void PrintList(student *stu_2)
{
while(stu_1 != NULL)
{
printf("学号:%d,姓名:%s,年龄:%d,分数:%f\n",stu_1->num,stu_1->name,stu_1->age,stu_1->score);
stu_1 = stu_1->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
student *temp;
initList();
insertList(stu_1,1,"A",10,74.5,1);
insertList(stu_1,2,"B",11,74,2);
insertList(stu_1,3,"C",12,73,3);
PrintList(stu_1);
while (stu_1 != NULL)
{
temp = stu_1;
stu_1= stu_1->next;
free(stu_1);
}
return 0;
}
10-14
10-14