#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define LEN sizeof(struct student)
struct student
{
int
num;
char
name[10];
float
score;
struct
student *next;
};
static unsigned inode=1;
//定义全局变量,表示链表节点数(长度)
void fill(char c[],int n,char
ch);
//专门用于初始化字符数组:用c填充整个数组c[],n是数组长度
void del(struct student *ph,int
n); //删除链表对象,n是用户指定要删除的序号
void insert(struct student *ph,int
n);
//插入链表节点,在ph的第n个节点之后插入,也可以在表头插入
void output(struct student
*ph);
//输出函数
struct student
*input(void);
//输入函数,函数会新申请一个空间,完成数据输入,返回空间首地址
void clearline(int n);
int
main()
//尝试动态链表的创建与删除
{
struct
student *head,*ps,*pa,*pb;
//head是头部,ps用来特别定位到头部,pa用来访问和遍历链表,pb用来备份节点地址
int
u;
//u用来读取用户指定要删除的序号
char
flag='y';
//flag旗标用来记录用户选择
head=ps=pa=(struct student
*)malloc(LEN);
//所有指针初始化到链表头部
fill(pa->name,10,'\0');
//字符数组初始化
printf("请输入学生信息,格式如下,字段之间可以以TAB跳格:\n");
printf("学号\t姓名\t成绩\n");
while(flag!='n') {
scanf("%d %s
%f",&pa->num,pa->name,&pa->score);
printf("Press any key input data continue,otherwise press n
quit:");
flag=getche();
if(flag!='n') {
clearline(60);
pa->next=(struct student *)malloc(LEN);
pa=pa->next;
fill(pa->name,10,'\0');
inode++;
//记住链表的节点数(长度)
}
}
pa->next=NULL;
output(ps);
//删除操作开始
printf("你想删除其中的哪一个学生?\n");
printf("请输入你要删除的信息列表序号:");
scanf("%d",&u);
if(u>inode) {
printf("输入错误!\n");
exit(1);
}
if(u==1)
{
//第一个节点的删除和其他不同
pb=head;
//备份链表的原始头地址
ps=head->next;