https://www.icourse163.org/learn/IDXY-1206905801?tid=1450771731#/learn/content?type=detail&id=1217619479
https://www.icourse163.org/learn/IDXY-1206905801?tid=1450771731#/learn/content?type=detail&id=1217619480&sm=1
一、静态链表
#include<stdio.h>
struct Node{
int data;//数据域
struct Node* next;//指针域
};
int main(){
struct Node1= {
1,NULL};
struct Node2= {
2,NULL};
struct Node3= {
3,NULL};
Node1.next = &Node2;
Node2.next = &Node3;
return 0;
}
二、动态链表
1、分配内存空间函数malloc()
调用形式:(类型说明符*) malloc (size)
功能:在内存的动态存储区中分配一块长度为“size”字节的连续区域。
例如:pc=(char *) malloc (100);
2、分配内存空间函数calloc()
调用形式:(类型说明符*)calloc(n,size)
功能:在内存动态存储区中分配n块长度为“size”字节的连续区域。
例如:ps=( struct stu*) calloc(2,sizeof (struct stu));
3、释放内存空间函数free()
调用形式:free(void *ptr);
功能:释放ptr所指向的一块内存空间,ptr是一个任意类型的指针变量,它指向被释放区域的首地址。
4、动态内存分配和释放例题
分配一块区域,输入一个学生数据并输出
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
int num;
char *name;
char sex;
float score;
} *ps