/* 写一个函数del,用来删除动态链表中指定的节点 */ #include<stdio.h> #include<stdlib.h> typedef struct LNode{ int num; struct LNode *next; }LNode; LNode* creat(int n) { LNode *head,*p; head = (LNode *)malloc(sizeof(LNode));//建立头节点 p = head; head->num = 0; head->next = NULL; for(int i = 1;i<=n;i++)//使用尾插法对链表赋值 { LNode *newNode = (LNode *)malloc(sizeof(LNode)); newNode->num = i; newNode->next = NULL; p->next = newNode; p = p->next; } return head; } void del(int n,LNode *head) { LNode *pre,*current; pre = head;//将头节点赋给pre current = head->next;//将current的值变为头节点后面的那个值,即链表的第一个有效值 //从第一个有效的节点开始查询需要删除的节点; printf("delete node %d\n",n); while(current!=NULL) { //找到需要删除的节点,重新链接,释放待删除的节点 if(current->num == n) { pre->next = current->next;//将current给间隔过去直接让pre的值指向current后面那个值 free(current);//释放current break;//跳出循环 } pre = current; current = current->next; } } int main() { LNode *head, *p; int n; head = creat(10); printf("请输入需要删除的节点:1-10\n"); scanf("%d", &n); del(n, head); int i = 1; p = head->next; while (p != NULL) { printf("p %d.num -> %d\n", i, p->num); p = p->next; i++; } return 0; }
0.0分
2 人评分
点我有惊喜!你懂得!浏览:1233 |
C二级辅导-求偶数和 (C语言代码)浏览:603 |
C二级辅导-统计字符 (C语言代码)浏览:729 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码) 如果a<0,那就不是这个代码了,哈哈哈哈浏览:1227 |
C语言训练-求具有abcd=(ab+cd)2性质的四位数 (C语言代码)浏览:1367 |
点我有惊喜!你懂得!浏览:1393 |
【回文数(二)】 (C语言代码)浏览:851 |
Hello, world! (C语言代码)浏览:1195 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:601 |
用筛法求之N内的素数。 (C语言代码)浏览:1228 |
kai 2022-11-23 19:16:58 |
那必须呀