creat.c

来自「本程序是一个动态建立链表的程序 分为链表的建立」· C语言 代码 · 共 36 行

C
36
字号
#include<stdio.h>
#include<stdlib.h>
#include"def.h"
#define LEN sizeof(struct student)


struct student * creat(int MAX)
{
	struct student *head;
	struct student *p1;
	struct student *p2;
	n=1;
	p1=p2=(struct student *)malloc(LEN);
	scanf("%ld",&p1->num);
	head=p1;
		while((p1->num!=0)&&(n<(MAX+1)))
			{

					if(n==1)
							head=p1;
					p2->next=p1;
					p2 = p1;//链接上以后P2就立刻指向了最后的节点
					n=n+1;

							if(n>MAX)
							continue; //continue放在这个位置可以减少不必要的节点申请,减少内存浪费
							p1=(struct student * )malloc( LEN ); //再新申请节点
							scanf("%ld",&p1->num);


			}
	p2->next=NULL;
	n=n-1;
	return head;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?