已知一个带头结点的单链表L,其结点数据为整型,请给出用C语言描述的该链表的数据结构定义,并编写算法删除链表L中所有的数据为奇数的结点,并输
出删除后链表的所有数据。
你题目的解答代码如下:
#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;
}
如有帮助,望采纳!谢谢!