C语言链表编写问题

来源:百度知道 编辑:UC知道 时间:2024/06/27 16:21:59
链表的基本操作
编写完整程序实现以下功能:
(1) 建立由15个整数构成的链表,以随机数的方式产生数据;
(2) 按链表中的次序显示各个数据;
(3) 删除链表中的所有偶数;
(4) 再次显示当前链表中的各个数据。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct Node
{
int data;
Node *next;
};

Node *creatLink() //创建链表
{
Node *h,*p,*q;
int count=0;
while(count!=6)
{
if(count==0)
{
h=new Node;
p=h;
}
else{
p->next=q;
p=q;
}
int new_data=rand();
p->data=new_data;
q=new Node;
count++;
}
p->next=NULL;
delete q;
return h;
}
void print(Node *head) //打印链表
{
Node *p=head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}

}
Node * check(Node *head) //做check并删除满足条件的节点
{
Node *p=head,*c=p,*q,*new_head=head;
while(p==new_head && p!=NULL)
{
if(p->data%2==0 &&p->next!=NULL)
{
q=p->next;
delete p;
p=q;

new_head=q;