已知一个带头结点的单链表L,其结点数据为整型,请给出用C语言描述的该链表的数据结构定义,并编写算法删除链表L中所有的数据为奇数的结点,并输
出删除后链表的所有数据。

考研数据结构算法设计题 链表算法设计题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- CSDN专家-天际的海浪 2021-11-06 03:26关注
你题目的解答代码如下:
#include <stdio.h> #include <stdlib.h> struct Data { int numb; struct Data *next; }; void creat(struct Data *pHead) { int count = 1; struct Data *p, *t; pHead->next = NULL; p = pHead; while (count<10) { t = (struct Data *)malloc(sizeof(struct Data)); t->numb = count; t->next = NULL; p->next = t; p = t; count++; } } void del(struct Data *pHead) { struct Data *q, *p = pHead; while (p->next != NULL) { q = p; p = p->next; if (p->numb%2==1) { printf("删除 %d\n", p->numb); q->next = p->next; p = q; } } } void print(struct Data *pHead) { struct Data *p = pHead; while (p->next != NULL) { p = p->next; printf("%d ", p->numb); } printf("\n"); } int main() { struct Data *pHead = (struct Data *)malloc(sizeof(struct Data)); creat(pHead); print(pHead); del(pHead); print(pHead); return 0; }
如有帮助,望采纳!谢谢!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用