第一范文网 - 专业文章范例文档资料分享平台

C语言链表的建立和基本运算

来源:用户分享 时间:2025/7/27 10:44:17 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

{

y=p->name;

if(strcmp(y,x)==0) /*把数据域里的姓名与所要查找的姓名比较,若相同则返回0,即条件成立*/

return(p); /*返回与所要查找结点的地址*/ else p=p->link; }

if(p==NULL)

printf(\没有查找到该数据!\

}

main() {

int number;

char fullname[20];

stud *head,*searchpoint; /*head是表头指针,searchpoint是保存符合条件的结点地址的指针*/

number=N;

head=creat(number);

printf(\请输入你要查找的人的姓名:\

scanf(\

searchpoint=search(head,fullname); /*调用查找函数,并把结果赋给searchpoint指针*/

}

2、 插入(后插) 假设在一个单链表中存在2个连续结点p、q(其中p为q的直接前驱),若我们需要在p、q之间插入一个新结点s,那么我们必须先为s分配空间并赋值,然后 使p的链域存储s的地址,s的链域存储q的地址即可。(p->link=s;s->link=q),这样就完成了插入操作。

下例是应用插入算法的一个例子 #include #include #include #define N 10

typedef struct node {

char name[20]; struct node *link; }stud;

stud * creat(int n) /*建立单链表的函数*/ {

stud *p,*h,*s;

int i;

if((h=(stud *)malloc(sizeof(stud)))==NULL) {

printf(\不能分配内存空间!\

exit(0); }

h->name[0]='\\0'; h->link=NULL; p=h;

for(i=0;i

if((s= (stud *) malloc(sizeof(stud)))==NULL) {

printf(\不能分配内存空间!\

exit(0); }

p->link=s;

printf(\请输入第%d个人的姓名:\

scanf(\s->link=NULL; p=s; }

return(h); }

stud * search(stud *h,char *x) /*查找函数*/ {

stud *p; char *y; p=h->link;

while(p!=NULL) {

y=p->name;

if(strcmp(y,x)==0) return(p);

else p=p->link; }

if(p==NULL)

printf(\没有查找到该数据!\

}

void insert(stud *p) /*插入函数,在指针p后插入*/ {

char stuname[20];

stud *s; /*指针s是保存新结点地址的*/

if((s= (stud *) malloc(sizeof(stud)))==NULL) {

printf(\不能分配内存空间!\

exit(0); }

printf(\请输入你要插入的人的姓名:\

scanf(\

strcpy(s->name,stuname); /*把指针stuname所指向的数组元素拷贝给新结

点的数据域*/

s->link=p->link; /*把新结点的链域指向原来p结点的后继结点*/ p->link=s; /*p结点的链域指向新结点*/

}

main() {

int number;

char fullname[20]; /*保存输入的要查找的人的姓名*/ stud *head,*searchpoint; number=N;

head=creat(number); /*建立新链表并返回表头指针*/ printf(\请输入你要查找的人的姓名:\

scanf(\

searchpoint=search(head,fullname); /*查找并返回查找到的结点指针*/ insert(searchpoint); /*调用插入函数*/

}

3.删除 假如我们已经知道了要删除的结点p的位置,那么要删除p结点时只要令p结点的前驱结点的链域由存储p结点的地址该为存储p的后继结点的地址,并回收p结点即可。 以下便是应用删除算法的实例: #include #include #include #define N 10

typedef struct node {

char name[20]; struct node *link; }stud;

stud * creat(int n) /*建立新的链表的函数*/ {

stud *p,*h,*s; int i;

if((h=(stud *)malloc(sizeof(stud)))==NULL) {

printf(\不能分配内存空间!\

exit(0); }

h->name[0]='\\0'; h->link=NULL; p=h;

for(i=0;i

if((s= (stud *) malloc(sizeof(stud)))==NULL) {

printf(\不能分配内存空间!\

exit(0); }

p->link=s;

printf(\请输入第%d个人的姓名\

scanf(\s->link=NULL; p=s; }

return(h); }

stud * search(stud *h,char *x) /*查找函数*/ {

stud *p; char *y; p=h->link;

while(p!=NULL) {

y=p->name;

if(strcmp(y,x)==0) return(p);

else p=p->link; }

if(p==NULL)

printf(\没有查找到该数据!\

}

stud * search2(stud *h,char *x) /*另一个查找函数,返回的是上一个查找函数的直接前驱结点的指针,*/

搜索更多关于: C语言链表的建立和基本运算 的文档
C语言链表的建立和基本运算.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2tjpm98eq568ub00wplr_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top