链表

// 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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值