189 8069 5689

C语言中链表的修改函数,C语言链表增删改查

如何用C语言创建一个链表,实现增、删、改、查

#includestdio.h

创新互联主营西乌珠穆沁网站建设的网络公司,主营网站建设方案,重庆APP开发公司,西乌珠穆沁h5微信小程序开发搭建,西乌珠穆沁网站营销推广欢迎西乌珠穆沁等地区企业咨询

#includestring.h

#include malloc.h

//先定义一种student类型,表示一个学生的信息,如下:

typedef struct student

{

int num; //表示学号

char name[30]; //表示姓名

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;

printf("请输入第%d个学生的学号:",i);

scanf("%d",xh);

}

}

else if(direction==0) //当direction为0时,新建立的结点成为第一个结点

{

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=head-next;

head-next=p;

i=i+1;

printf("请输入第%d个学生的学号:",i);

scanf("%d",xh);

}

}

return head;

}

//2、写出输出上述链表各结点数据域值的函数。该函数对应的函数需要一个形参,表示链表的头指针,形式如下:

void print_link(NODE *head)

{

NODE *p;

p=head-next;

printf("%-10s%-20s%-10s\n","学号","姓名","分数");

while(p!=NULL)

{

printf("%-10d%-20s%-10.1f\n",p-st.num,p-st.name,p-st.score);

p=p-next;

}

//该函数能输出head所指的链表的所有结点值,输出形式如下:

/*本函数输出线性表sq中所有数据,形式如下:

学号 姓名 分数

12 张三 234.5

18 李四 987.7

……… ……… …….*/

}

//3、写出在链表中删除结点的函数

int del_link(NODE *head,char name[])

{

NODE *p,*p1;

p=head-next;

p1=head;

while(p!=NULL)

{

if(strcmp(p-st.name,name)!=0)

{

p1=p;

p=p-next;

}

else

{

break;

}

}

if(p!=NULL)

{

p1-next=p-next;

free(p);

return 1;

}

else

{

return 0;

}

//删除head所指的链表中,名字为name的结点,删除成功返回1,不成功返回0

}

//4、写出在链表中插入结点的算法

int insert(NODE *head,student x,int wz)

{

NODE *p=head;

int i=0,jg;

if(wz=0)

{

jg=0;

}

else

{

while(iwz-1p!=NULL)

{

i++;

p=p-next;

}

if(p==NULL)

{

jg=0;

}

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。

}

//5、写出主函数,分别调用上面算法所对应的程序,建立链表,并输出链表的值。

void main()

{

NODE *head; //定义指针变量head

int wz; //表示插入位置

char xm[30];

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);

}

else

{

printf("插入不成功");

}

//调用函数,在链表中删除一个指定结点的值,如果删除成功,输出新链表

printf("\n\n请输入要删除的学生的姓名:");

getchar();

gets(xm);

if(del_link(head,xm)==1)

{

printf("\n删除成功,新表为:\n");

print_link(head);

}

else

{

printf("删除不成功");

}

}

C语言——链表的修改

#include stdio.h

#include stdlib.h

#define LEN sizeof(struct student)

struct student

{

int num;

char name[20];

float score[3];

struct student *next;

};

int n=0;

struct student *creat(void) //建立链表

{

struct student *head,*p1,*p2;

p1=p2=(struct student *)malloc(LEN);

head=NULL;

scanf("%d %s %f %f %f",p1-num,p1-name,p1-score[0],p1-score[1],p1-score[2]);

while(p1-num!=0)

{

n=n+1;

if(n==1)

head=p1;

else p2-next=p1;

p2=p1;

p1=(struct student *)malloc(LEN);

scanf("%d %s %f %f %f",p1-num,p1-name,p1-score[0],p1-score[1],p1-score[2]);

}

p2-next=NULL;

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;

int m,flag=1,flag1=1,flag2=1;

b=p=creat();

printf("now,there are %d crunodes are:\n",n);

for(;p!=NULL;)

{

printf("%d%10s%8.2f%8.2f%8.2f\n",p-num,p-name,p-score[0],p-score[1],p-score[2]);

p=p-next;

}

while(flag2!=0) //控制开关

{

printf("if you input '1' that you will have the funtion to delete the crunode\nif you input '2' that you will have the funtion to insert crunode.\nif you input '0' that you will end.\n");

printf("what you want to enter 1 ,2 or 0:");

scanf("%d" ,flag2);//选择功能,是增加结点还是删减结点

if(flag2==1)

{

while(flag)

{

printf("delete the crunode(结点):");

scanf("%d",m);

p=del(b,m);

if(n==0)

{

b=p;

flag=0;

break;

}

b=p;

printf("now,there are %d crunodes are:\n",n);

for(;p!=NULL;)

{

printf("%d%10s%8.2f%8.2f%8.2f\n",p-num,p-name,p-score[0],p-score[1],p-score[2]);

p=p-next;

}

printf("contiune or not(0/1):");

scanf("%d",flag);

}

}

if(flag2==2)

{

while(flag1)

{

if(flag)//第一次是不执行这句的

{

if(n==0) b=NULL;

p=insert(b,stu);

b=p;

printf("now,there are %d crunodes are:\n",n);

for(;p!=NULL;)

{

printf("%d%10s%8.2f%8.2f%8.2f\n",p-num,p-name,p-score[0],p-score[1],p-score[2]);

p=p-next;

}

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); //申请内存

scanf("%d %s %f %f %f",stu-num,stu-name,stu-score[0],stu-score[1],stu-score[2]);//输入学生的信息

flag=1;

// 起到控制作用,目的是为了第一次不执行上上个if语句

}

}

}

}

struct student *del(struct student *head,int num) //删除结点

{

struct student *p1,*p2;

if(head==NULL)

{

printf("this is list null.\n");

return head;

}

else

p1=head;

while(num!=p1-num p1-next!=NULL)

{

p2=p1;p1=p1-next; //一个一个的下去

}

if(num==p1-num)

{

if(p1==head) //才一个结点

{

head=p1-next;

free(p1);

}

else //不止一个结点

{

p2-next=p1-next;

free(p1);

}

n=n-1;

printf("delete:%d\n",num);

printf("there are %d crunode(结点) now.\n",n);

}

else

printf("%d crunode(结点) has not been found!\n",num);

if(n==0) //如果删除结点后结点个数为0

return NULL;

else

return (head);

}

struct student *insert(struct student *head,struct student *stu) //增加结点

{

struct student *p1,*p2,*p0;

p1=head;

p0=stu;

if(head==NULL)

{

head=p0;

p0-next=NULL;

}

else

{

while(p0-nump1-num p1-next!=NULL)

{

p2=p1;p1=p1-next;

}

if(p0-num=p1-num)

{

if(head==p1) //插到第一个结点前

{

head=p0;

p0-next=p1;

}

else

{

p2-next=p0;

p0-next=p1;

}

}

else

{

p1-next=p0;

p0-next=NULL;

}

}

n=n+1;

return(head);

}

我正好学到链表的,所以就话了一个晚上来做了这个,我哦完善了一下,如果有什么不满意的话,或有什么不懂的话你可以加QQ419842687

c语言建立链表修改的原代码

main函数没写

你自己调用就ok了

#includestdio.h

#includemalloc.h

#define

len

sizeof(struct

student)

#define

NULL

int

n;

struct

student

{

long

number;

float

score;

struct

student

*next;

};

struct

student

*creat(void)

{

struct

student

*p1,*p2,*head;

p1=p2=(struct

student

*)malloc(len);

scanf("%ld,%f",p1-number,p1-score);

head=NULL;

while(p1-number!=0)

{

n=n+1;

if(n==1)

head=p1;

else

p2-next=p1;

p2=p1;

p1=(struct

student

*)malloc(len);

scanf("%ld,%f",p1-number,p1-score);

}

p2-next=NULL;

return(head);

}

struct

student

*del(struct

student

*head,long

number_1)

{

struct

student

*p1,*p2;

if(head==NULL)

{printf("no

list\n");

goto

end;}

p1=head;

while((number_1!=p1-number)(p1-next!=NULL))

{

p2=p1;p1=p1-next;

}

if(number_1==p1-number)

{

if(p1==head)

head=p1-next;

else

p2-next=p1-next;

n=n-1;

}

else

printf("cant

found\n");

end:

return(head);

}

struct

student

*insert(struct

student

*head,struct

student

*stud)

{

struct

student

*p1,*p2,*p0;

p0=stud;p1=head;

if(head==NULL)

{head=p0;p0-next=NULL;}

else

{

while((p0-numberp1-number)(p1-next!=NULL))

{

p2=p1;p1=p1-next;

}

if(p0-number=p1-number)

{

if(p1==head)

head=p0;

else

p2-next=p0;

p0-next=p1;

}

else

{

p1-next=p0;p0-next=NULL;

}

}

n=n+1;

return(head);

}

void

print(struct

student

*head)

{

struct

student

*p;

p=head;

while(p!=NULL)

{

printf("%ld:%f\n",p-number,p-score);

p=p-next;

}

}

C语言中链表的修改

字符串处理有问题,就是char nam[30];

输入字符串,scanf("%s",stu.nam);//不用加取址符

而且,下面赋值nam的时候

最好用strcpy,字符串复制函数

C语言链表修改问题.

1.关于insert如下修改

struct student *insert(void)

{

#define NULL 0

int Dim;

struct student *head,*pf,*pb;

int i;

head=NULL;

for(i=0;i2000;i++)

{

pb=(struct student*) malloc(sizeof (struct student));

if(NULL == pb)

{

printf("malloc fail!\n");//添加空间申请失败检查!

return head;

}

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);

printf("Continue?1 - y 2 - n\n");

scanf("%d",Dim);

if (Dim==2)

{i=3000;}

}

return head;

}

意思是:

A.依次申请空间,建立链表节点,若空间申请失败则推出!

B.每输入一次,询问是否继续建立节点,输入数字2则退出!

2.函数 change1

struct student *change1(char NAME[96],struct student *head)

{

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的值!

我帮你添加了一些调试用的句子!

C语言修改链表里的某个结点

scanf给整型变量输入时,变量前要加取地址符

scanf("%d", num);

scanf("%d%s", num, name);


网站题目:C语言中链表的修改函数,C语言链表增删改查
文章转载:http://cdxtjz.cn/article/hsijod.html

其他资讯