
C语言编程题目 在线等待答案 做好了 追加90分 10
编写自定义函数:建立一个带有头结点head的有20个结点的链表,20个结点所需数值由随机数产生。编写自定义函数:建立两个链表,把存有数据的链表中的偶数存入一个链表,奇数存...
编写自定义函数:建立一个带有头结点head的有20个结点的链表,20个结点所需数值由随机数产生。
编写自定义函数:建立两个链表,把存有数据的链表中的偶数存入一个链表,奇数存入另一个链表中。
编写主函数调用上述两个函数并可输出三个链表中的数据。 展开
编写自定义函数:建立两个链表,把存有数据的链表中的偶数存入一个链表,奇数存入另一个链表中。
编写主函数调用上述两个函数并可输出三个链表中的数据。 展开
3个回答
展开全部
// Link.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "math.h"
#include "stdlib.h"
typedef struct LinkList{
int data;
struct LinkList *next;
}LinkList;
void CreateLink(LinkList *&head,int n)
{
int i;
LinkList *p,*q;
head=(LinkList *)malloc(sizeof(LinkList));
if(head==NULL)
return;
q=head;
for(i=0;i<20;i++)
{
p=(LinkList*)malloc(sizeof(LinkList));
p->data=rand();
if(p==NULL)
{
return;
}
q->next=p;
q=p;
}
q->next=NULL;
}
void SplitLink(LinkList *L,LinkList * &L1,LinkList *&L2)
{
LinkList *head1=NULL,*head2=NULL,*p=NULL,*s=NULL,*t=NULL;
LinkList *q,*r;
if(L==NULL || L->next==NULL)
{
printf("linklist L is empty");
return;
}
p=L->next;
if((head1=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
if((head2=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
L1=q=head1;
L2=r=head2;
while(p!=NULL)
{
if(p->data%2==0)
{
if((s=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
s->data=p->data;
q->next=s;
q=s;
}
else
{
if((s=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
s->data=p->data;
r->next=s;
r=s;
}
p=p->next;
}
q->next=NULL;
r->next=NULL;
}
int main(int argc, char* argv[])
{
LinkList *head,*L1,*L2,*p;
CreateLink(head,20);
SplitLink(head,L1,L2);
printf("head:");
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
printf("L1:");
p=L1->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
printf("L2:");
p=L2->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
//
#include "stdafx.h"
#include "math.h"
#include "stdlib.h"
typedef struct LinkList{
int data;
struct LinkList *next;
}LinkList;
void CreateLink(LinkList *&head,int n)
{
int i;
LinkList *p,*q;
head=(LinkList *)malloc(sizeof(LinkList));
if(head==NULL)
return;
q=head;
for(i=0;i<20;i++)
{
p=(LinkList*)malloc(sizeof(LinkList));
p->data=rand();
if(p==NULL)
{
return;
}
q->next=p;
q=p;
}
q->next=NULL;
}
void SplitLink(LinkList *L,LinkList * &L1,LinkList *&L2)
{
LinkList *head1=NULL,*head2=NULL,*p=NULL,*s=NULL,*t=NULL;
LinkList *q,*r;
if(L==NULL || L->next==NULL)
{
printf("linklist L is empty");
return;
}
p=L->next;
if((head1=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
if((head2=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
L1=q=head1;
L2=r=head2;
while(p!=NULL)
{
if(p->data%2==0)
{
if((s=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
s->data=p->data;
q->next=s;
q=s;
}
else
{
if((s=(LinkList *)malloc(sizeof(LinkList)))==NULL)
return;
s->data=p->data;
r->next=s;
r=s;
}
p=p->next;
}
q->next=NULL;
r->next=NULL;
}
int main(int argc, char* argv[])
{
LinkList *head,*L1,*L2,*p;
CreateLink(head,20);
SplitLink(head,L1,L2);
printf("head:");
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
printf("L1:");
p=L1->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
printf("L2:");
p=L2->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上明显不是C
#include <stdio.h>
#include <stdlib.h>
#define N 20
#define SIZE 100
#define DEBUG 0
struct node{
int num;
struct node *next;
};
struct node *next_node(struct node* last,int num)
{
struct node *next;
next=(struct node*)malloc(sizeof(struct node));
next->num=num;
last->next=next;
#if DEBUG
printf("The next node !\n");
#endif
return next;
}
struct node *create_list()
{
int i;
struct node *head;
struct node *temp;
head=(struct node*)malloc(sizeof(struct node));
temp=head;
srand(time(NULL));
for(i=0;i<N;i++){
#if DEBUG
printf("The i is:%d\n",i);
sleep(1);
#endif
temp=next_node(temp,1+rand()%SIZE);
}
temp->next=NULL;
#if DEBUG
printf("create_list() Done!\n");
sleep(5);
#endif
return head;
}
struct node **create_two_list(struct node* main_list)
{
struct node **list;
struct node *head1,*head2,*temp,*temp1,*temp2;
list=(struct node**)malloc(sizeof(struct node*[3]));
head1=(struct node*)malloc(sizeof(struct node));
head2=(struct node*)malloc(sizeof(struct node));
temp1=head1;
temp2=head2;
temp=main_list->next;
while(temp!=NULL){
if((temp->num)%2==1)
temp1=next_node(temp1,temp->num);
else
temp2=next_node(temp2,temp->num);
temp=temp->next;
}
temp1->next=NULL;
temp2->next=NULL;
list[0]=head1;
list[1]=head2;
list[2]=NULL;
#if DEBUG
printf("create_two_list() Done!\n");
sleep(2);
#endif
return list;
}
void print_list(struct node* head)
{
struct node *temp;
int i=0;
temp=head->next;
while(temp!=NULL){
i++;
printf("%2d:\t%d\n",i,temp->num);
temp=temp->next;
}
}
int main()
{
struct node *main_list;
struct node **list;
main_list=create_list();
list=create_two_list(main_list);
printf("The main list:\n");
print_list(main_list);
printf("The first list:\n");
print_list(list[0]);
printf("The second list:\n");
print_list(list[1]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define N 20
#define SIZE 100
#define DEBUG 0
struct node{
int num;
struct node *next;
};
struct node *next_node(struct node* last,int num)
{
struct node *next;
next=(struct node*)malloc(sizeof(struct node));
next->num=num;
last->next=next;
#if DEBUG
printf("The next node !\n");
#endif
return next;
}
struct node *create_list()
{
int i;
struct node *head;
struct node *temp;
head=(struct node*)malloc(sizeof(struct node));
temp=head;
srand(time(NULL));
for(i=0;i<N;i++){
#if DEBUG
printf("The i is:%d\n",i);
sleep(1);
#endif
temp=next_node(temp,1+rand()%SIZE);
}
temp->next=NULL;
#if DEBUG
printf("create_list() Done!\n");
sleep(5);
#endif
return head;
}
struct node **create_two_list(struct node* main_list)
{
struct node **list;
struct node *head1,*head2,*temp,*temp1,*temp2;
list=(struct node**)malloc(sizeof(struct node*[3]));
head1=(struct node*)malloc(sizeof(struct node));
head2=(struct node*)malloc(sizeof(struct node));
temp1=head1;
temp2=head2;
temp=main_list->next;
while(temp!=NULL){
if((temp->num)%2==1)
temp1=next_node(temp1,temp->num);
else
temp2=next_node(temp2,temp->num);
temp=temp->next;
}
temp1->next=NULL;
temp2->next=NULL;
list[0]=head1;
list[1]=head2;
list[2]=NULL;
#if DEBUG
printf("create_two_list() Done!\n");
sleep(2);
#endif
return list;
}
void print_list(struct node* head)
{
struct node *temp;
int i=0;
temp=head->next;
while(temp!=NULL){
i++;
printf("%2d:\t%d\n",i,temp->num);
temp=temp->next;
}
}
int main()
{
struct node *main_list;
struct node **list;
main_list=create_list();
list=create_two_list(main_list);
printf("The main list:\n");
print_list(main_list);
printf("The first list:\n");
print_list(list[0]);
printf("The second list:\n");
print_list(list[1]);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
again 楼上说的 嘿嘿。的确太复杂,是作业的话就等老师讲解,或者参考同学, 我不HUI c曾经用过VB比较简单,相对你现在大二 那你初中的时候一定学过VB了,用VB先糊弄一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询