#includestdio.h
#includestring.h
#include malloc.h
//先定义一种student类型,表示一个学生的信息,如下:
typedef struct student
{
int num; //表示学号
float score; //表示分数
}student;
//定义一种NODE类型,表示一个结点信息,如下:
typedef struct node
student st; //表示一个学生的信息
struct node *next; //表示一个NODE类型的指针
}NODE;
//1、写出建立一个带头结点的线性链表的函数,其中每个结点包括学号、姓名、分数三个数据域.函数形式如下:
NODE *creat_link(int direction)
NODE *head,*p,*tail;
int xh,i=1;
if(direction==1) //当direction的值为1时,新建立的结点连到尾部
tail=head=(NODE *)malloc(sizeof(NODE));
head-next=NULL;
printf("请输入第%d个学生的学号:",i);
scanf("%d",xh);
while(xh0) //从键盘临时输入学生情况,当输入的学号非正,则链表建立完毕
p=(NODE *)malloc(sizeof(NODE));
p-st.num=xh;
printf("请输入第%d个学生的姓名:",i);
scanf("%s",p-st.name);
printf("请输入第%d个学生的成绩:",i);
scanf("%f",p-st.score);
p-next=NULL;
tail-next=p;
tail=p;
i=i+1;
}
else if(direction==0) //当direction为0时,新建立的结点成为第一个结点
head=(NODE *)malloc(sizeof(NODE));
p-next=head-next;
head-next=p;
return head;
void print_link(NODE *head)
NODE *p;
p=head-next;
while(p!=NULL)
p=p-next;
//该函数能输出head所指的链表的所有结点值,输出形式如下:
/*本函数输出线性表sq中所有数据,形式如下:
学号 姓名 分数
......... ......... .......*/
int del_link(NODE *head,char name[])
NODE *p,*p1;
p1=head;
if(strcmp(p-st.name,name)!=0)
p1=p;
else
break;
if(p!=NULL)
p1-next=p-next;
free(p);
return 1;
return 0;
//删除head所指的链表中,名字为name的结点,删除成功返回1,不成功返回0
int insert(NODE *head,student x,int wz)
NODE *p=head;
int i=0,jg;
if(wz=0)
jg=0;
while(iwz-1p!=NULL)
i++;
if(p==NULL)
if(i=wz-1)
//找到wz前面的节点,p指向它
NODE *q;
q=(NODE *)malloc(sizeof(NODE));
q-st.num=x.num;
strcpy(q-st.name,x.name);
q-st.score=x.score;
q-next=p-next;
p-next=q;
jg=1;
return jg;
//该函数能够在wz这个结点之前,插入一个新结点,新结点的数据域为x.插入成功返回1,不成功返回0.
void main()
NODE *head; //定义指针变量head
int wz; //表示插入位置
student st; //定义一个变量st,用来表示一个学生的信息
head=creat_link(1);
print_link(head); //调用函数建立链表,并把返回值送给head;
//调用函数,输出链表中各个结点的值
//输入一个学生的有关信息,送给变量st的有关成员
printf("\n\n请输入要插入的位置:");
scanf("%d",wz); //输入wz的值
printf("请输入要插入的学生的学号:");
scanf("%d",st.num);
printf("请输入要插入的学生的姓名:");
scanf("%s",st.name);
printf("请输入要插入的学生的成绩:");
scanf("%f",st.score);
//调用函数,在链表中把学生st的值作为一个结点插入,如果插入成功,输出新链表
if(insert(head,st,wz)==1)
printf("\n插入成功,新表为:\n");
print_link(head);
printf("插入不成功");
//调用函数,在链表中删除一个指定结点的值,如果删除成功,输出新链表
printf("\n\n请输入要删除的学生的姓名:");
getchar();
gets(xm);
if(del_link(head,xm)==1)
printf("\n删除成功,新表为:\n");
printf("删除不成功");
#include stdio.h
#include stdlib.h
#define LEN sizeof(struct student)
struct student
int num;
struct student *next;
};
int n=0;
struct student *creat(void) //建立链表
head=NULL;
while(p1-num!=0)
n=n+1;
if(n==1)
head=p1;
p1=(struct student *)malloc(LEN);
return (head);
void main() //main函数
struct student *insert(struct student *head,struct student *stu);
struct student *del(struct student *head,int num);
struct student *p, *b,*stu;
b=p=creat();
printf("now,there are %d crunodes are:\n",n);
for(;p!=NULL;)
while(flag)
printf("delete the crunode(结点):");
scanf("%d",m);
p=del(b,m);
if(n==0)
b=p;
flag=0;
printf("contiune or not(0/1):");
scanf("%d",flag);
while(flag1)
if(flag)//第一次是不执行这句的
if(n==0) b=NULL;
p=insert(b,stu);
printf("continue or not :(0/1):");
scanf("%d",flag1);
if(flag1==0) break;
//起到控制整个while循环的作用
printf("please input the imformation of the student who you want to add:");
stu=(struct student *)malloc(LEN); //申请内存
flag=1;
// 起到控制作用,目的是为了第一次不执行上上个if语句
struct student *del(struct student *head,int num) //删除结点
if(head==NULL)
printf("this is list null.\n");
while(num!=p1-num p1-next!=NULL)
if(num==p1-num)
if(p1==head) //才一个结点
head=p1-next;
free(p1);
else //不止一个结点
n=n-1;
printf("delete:%d\n",num);
printf("there are %d crunode(结点) now.\n",n);
printf("%d crunode(结点) has not been found!\n",num);
if(n==0) //如果删除结点后结点个数为0
return NULL;
struct student *insert(struct student *head,struct student *stu) //增加结点
p0=stu;
head=p0;
p0-next=NULL;
while(p0-nump1-num p1-next!=NULL)
if(p0-num=p1-num)
if(head==p1) //插到第一个结点前
p0-next=p1;
p1-next=p0;
return(head);
main函数没写
你自己调用就ok了
#includemalloc.h
#define
len
sizeof(struct
student)
NULL
int
n;
struct
student
long
number;
float
score;
*next;
*creat(void)
*)malloc(len);
scanf("%ld,%f",p1-number,p1-score);
while(p1-number!=0)
p1=(struct
*del(struct
*head,long
number_1)
{printf("no
list\n");
goto
end;}
while((number_1!=p1-number)(p1-next!=NULL))
if(number_1==p1-number)
if(p1==head)
printf("cant
found\n");
end:
*insert(struct
*head,struct
*stud)
p0=stud;p1=head;
{head=p0;p0-next=NULL;}
while((p0-numberp1-number)(p1-next!=NULL))
if(p0-number=p1-number)
p1-next=p0;p0-next=NULL;
void
print(struct
*head)
*p;
p=head;
printf("%ld:%f\n",p-number,p-score);
输入字符串,scanf("%s",stu.nam);//不用加取址符
而且,下面赋值nam的时候
最好用strcpy,字符串复制函数
①关于insert如下修改
struct student *insert(void)
#define NULL 0
int Dim;
struct student *head,*pf,*pb;
int i;
pb=(struct student*) malloc(sizeof (struct student));
if(NULL == pb)
printf("malloc fail!\n");//添加空间申请失败检查!
printf("Please insert Name, Gender, Age,Education,Address,Tel\n");
scanf("%s,%s,%d,%s,%s,%ld\n",pb-Name,pb-Gender,pb-Age,pb-Education,pb-Address,pb-Tel);//对应的输入方式修改
if(i==0)
{pf=head=pb;}
else { pf-next=pb;
pb-next=NULL;
pf=pb;}
printf("%d",head-next-Age);
scanf("%d",Dim);
意思是:
A.依次申请空间,建立链表节点,若空间申请失败则推出!
char flag = 1;
struct student *pf,*pb;
for(pb=head;pb!=NULL;pb=pb-next)
if(strcmp(NAME,pb-Name)==0)
printf("找到相应名字的元素,请修改相应的Education\n");
printf("Please insert New education grade;");
scanf("%s",pb-Education);
flag = 0;
if(1 == flag)
printf("没有相应名字的元素\n");
输入元素的名字,及链表头,查找相应名字的元素,修改Education的值!
我帮你添加了一些调试用的句子!
scanf给整型变量输入时,变量前要加取地址符
scanf("%d", num);
scanf("%d%s", num, name);
以上就是土嘎嘎小编为大家整理的C语言中链表的修改函数相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!