求教一个数据结构单链表的题目~~~最好可以解释以下
2个回答
展开全部
#include <ansi_c.h>#include <stdlib.h>
#define Length 10
typedef struct LNode
{
int elem;
struct LNode *pNext;
}LNode, *LinkList;
LNode * CreatLinkList(int n);
int GetEvenNumAndMaxValue(LinkList L, int *Max, int *EvenNum);
void main()
{
LinkList L;
int Max = 0, EvenNum = 0, Result = 0 ;
L = CreatLinkList(Length);
Result = GetEvenNumAndMaxValue(L, &Max, &EvenNum);
printf("最大值为:%d, 偶数数字的个数%d", Max, EvenNum);
}
int GetEvenNumAndMaxValue(LinkList L, int* Max, int* EvenNum)
{
int i = 0;
LinkList p, q;
p = L;
q = p->pNext;
*EvenNum = 0;
if(p->pNext == NULL)
{
return -1;
}
*Max = p->elem;
while(q!=NULL)
{
if(p->elem < q->elem)
{
*Max = q->elem;
}
if(((p->elem)%2) == 0)
{
(*EvenNum) += 1;
}
p = p->pNext;
q = q->pNext;
}
if(((p->elem)%2) == 0) //对最后一个元素作判断
{
(*EvenNum) += 1;
}
return 0;
}
LinkList CreatLinkList(int n)
{
int i = 0;
LinkList p, q;
printf("Please input the Data for LinkList Node:\n");
p = q = (LinkList)malloc(sizeof(LNode)); //已经有一个节点
p->pNext = NULL;
scanf("%d", &p->elem);
do
{
p->pNext= (LinkList)malloc(sizeof(LNode));
p = p->pNext;
scanf("%d", &p->elem);
p->pNext = NULL;
i++;
}while(i<n-1); // 所以这里n-1;
return q;
}
#define Length 10
typedef struct LNode
{
int elem;
struct LNode *pNext;
}LNode, *LinkList;
LNode * CreatLinkList(int n);
int GetEvenNumAndMaxValue(LinkList L, int *Max, int *EvenNum);
void main()
{
LinkList L;
int Max = 0, EvenNum = 0, Result = 0 ;
L = CreatLinkList(Length);
Result = GetEvenNumAndMaxValue(L, &Max, &EvenNum);
printf("最大值为:%d, 偶数数字的个数%d", Max, EvenNum);
}
int GetEvenNumAndMaxValue(LinkList L, int* Max, int* EvenNum)
{
int i = 0;
LinkList p, q;
p = L;
q = p->pNext;
*EvenNum = 0;
if(p->pNext == NULL)
{
return -1;
}
*Max = p->elem;
while(q!=NULL)
{
if(p->elem < q->elem)
{
*Max = q->elem;
}
if(((p->elem)%2) == 0)
{
(*EvenNum) += 1;
}
p = p->pNext;
q = q->pNext;
}
if(((p->elem)%2) == 0) //对最后一个元素作判断
{
(*EvenNum) += 1;
}
return 0;
}
LinkList CreatLinkList(int n)
{
int i = 0;
LinkList p, q;
printf("Please input the Data for LinkList Node:\n");
p = q = (LinkList)malloc(sizeof(LNode)); //已经有一个节点
p->pNext = NULL;
scanf("%d", &p->elem);
do
{
p->pNext= (LinkList)malloc(sizeof(LNode));
p = p->pNext;
scanf("%d", &p->elem);
p->pNext = NULL;
i++;
}while(i<n-1); // 所以这里n-1;
return q;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询