198 字
1 分钟
数据结构2-链表的增减删
链表的创建
函数:malloc(动态内存分配) void *malloc(size_t size)
stu* creat(int id,char str[]){//创建链表节点 stu *new; if((new=(stu *)malloc(sizeof(stu)))==NULL){ printf("不能分配内存空间!"); exit(0); } new->num=id; strcpy(new->name,str); new->next=NULL; return new;}链表查找
- 按照值查找
int query_value(stu *p,char name[]){//按照值查找节点,并返回节点序号 int i=0; while((p!=NULL)){ if (strcmp(p->name,name)==0){ return i; } p=p->next; i=i+1; } return -1;}- 按照节点位置查找
stu* query_id(stu *p,int id){//传入指针节点序号,并返回节点指针 if (id<0) return NULL; for (int i = 0; i < id; ++i){ if (p==NULL) return NULL; p=p->next; } return p;}链表的删除
int del_list(stu *p,int id){//传入节点序号,删除节点 stu *p1,*s; if (id<1) return 0; p1=query_id(p,id-1); if (p1==NULL){//前一个节点不存在 return -1; } else{ if (p1->next==NULL){//被删除节点不存在 return 0; } else{ s=p1->next; p1->next=s->next; free(s); return 1; } }}数据结构2-链表的增减删
https://www.klzy.me/posts/7/分享文章
生成精美分享图或复制链接,与更多人分享本文。
继续阅读
换条路线
从其他文章中稳定抽取
最后更新于 ,距今已过 3313 天
部分内容可能已过时