请编写在带头结点的单链表L上删除其值为奇数的所有元素的算法 10
3个回答
展开全部
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Node)
struct Node
{
int num ;
struct Node *next;
};
int main()
{
struct Node *creat();
struct Node *del(struct Node *head);
void print(struct Node *);
struct Node *head;
head=creat();
print(head);
printf("删除相同的结点后:\n");
del(head);
print(head);
return 0;
}
//建立链表的的函数
struct Node *creat()
{
struct Node *head;
struct Node *p1,*p2;
p1=p2=(struct Node *) malloc(LEN);
head=NULL;
int n = 0;
p1->num = n;
while (p1->num < 19)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Node *)malloc(LEN);
p1->num = n;
}
p2->next=NULL;
return (head);
}
//删除相同结点的函数
struct Node *del(struct Node *head)
{
struct Node *p,*q,*f,*r;
p=head;
while(p!=NULL)
{
r=p;
f=r->next;
while(f!=NULL)
{
if(f->num % 2 != 0)
{
q=f;
r->next=f->next;
f=f->next;
free(q);
}
else
{
r=f;
f=f->next;
}
}
p=p->next;
}
return head;
}
//输出链表的函数
void print(struct Node *head)
{
struct Node *p;
p=head;
while (p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}
#include<malloc.h>
#define LEN sizeof(struct Node)
struct Node
{
int num ;
struct Node *next;
};
int main()
{
struct Node *creat();
struct Node *del(struct Node *head);
void print(struct Node *);
struct Node *head;
head=creat();
print(head);
printf("删除相同的结点后:\n");
del(head);
print(head);
return 0;
}
//建立链表的的函数
struct Node *creat()
{
struct Node *head;
struct Node *p1,*p2;
p1=p2=(struct Node *) malloc(LEN);
head=NULL;
int n = 0;
p1->num = n;
while (p1->num < 19)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Node *)malloc(LEN);
p1->num = n;
}
p2->next=NULL;
return (head);
}
//删除相同结点的函数
struct Node *del(struct Node *head)
{
struct Node *p,*q,*f,*r;
p=head;
while(p!=NULL)
{
r=p;
f=r->next;
while(f!=NULL)
{
if(f->num % 2 != 0)
{
q=f;
r->next=f->next;
f=f->next;
free(q);
}
else
{
r=f;
f=f->next;
}
}
p=p->next;
}
return head;
}
//输出链表的函数
void print(struct Node *head)
{
struct Node *p;
p=head;
while (p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}
展开全部
struct Node
{
int data;
struct Node *next;
};
struct Node *del(struct Node *head)
{
struct Node *p,*q,*r;
q = head;
p = head->next;
r = NULL;
while ( p != NULL )
{
if ( p->data % 2 != 0 )
{
r = p;
p = p->next;
q->next = p;
r->next = NULL;
free (r);
r = NULL;
}
else
{
q = p;
p = p->next;
}
}
return head;
}
{
int data;
struct Node *next;
};
struct Node *del(struct Node *head)
{
struct Node *p,*q,*r;
q = head;
p = head->next;
r = NULL;
while ( p != NULL )
{
if ( p->data % 2 != 0 )
{
r = p;
p = p->next;
q->next = p;
r->next = NULL;
free (r);
r = NULL;
}
else
{
q = p;
p = p->next;
}
}
return head;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void delete(PNODE pHead)
{
PNODE pTemp = pHead->pNext;
while (pTemp != NULL)
{
if (pTemp->e % 2 != 0)
{
pHead->pNext = pTemp->pNext;
free(pTemp);
pTemp = pHead->pNext;
}
else
{
pHead = pTemp;
pTemp = pTemp->pNext;
}
}
}
{
PNODE pTemp = pHead->pNext;
while (pTemp != NULL)
{
if (pTemp->e % 2 != 0)
{
pHead->pNext = pTemp->pNext;
free(pTemp);
pTemp = pHead->pNext;
}
else
{
pHead = pTemp;
pTemp = pTemp->pNext;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询