这个程序主要对动态链表的建立、删除、插入的完整程序,注释很详尽对刚学
习 c 语言的人来说应该有所帮助吧,程序是我在.cpp 格式下运行好的。
/******************************************
******
*
* 文件名 :C practice
* 创建时间 : 2013-1-18
* 文件描 : c 的基础练习 链表的
* 建立及使用
* 错误记录及修改:
* 在动态链表输出时出错
* p->num!=0 改为 p->!=NULL;原因在于最后输入
* 0 0 时没有将最后的链表建立进去,当访问
* p->num 时没有值,最后输错出错
*******************************************
******/
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define Len sizeof(struct student) //定义一个结构体等长的常量 len,用于给新的
结构体分配空间
/*********************************
结构体名:student
**********************************/
struct student
{
int num;
int score;
struct student *next;
};
struct student *head,*p1,*p2;
int n=0; //用于记录插入链表的个数
/*********************************
函数名 :*creat()
参数 :无
功能 :用于插入新的链表
返回值 :返回链表的首地址 head
*********************************/
struct student *creat()
{
head=NULL;
p1=(struct student*)malloc(Len); //给新的节点分配空间
p2=p1;