节点定义如下
struct Num
{
int num;
struct Num *next;
};
写一个函数找出链表中的最大数和次大数
答案:2 悬赏:0 手机版
解决时间 2021-02-01 21:31
- 提问者网友:雾里闻花香
- 2021-01-31 22:08
最佳答案
- 五星知识达人网友:平生事
- 2021-01-31 23:00
void linkedListMax2(strut Num *list, int *pmax, int *pmax2)
{
assert(pmax != NULL && pmax2 != NULL); // assert() in <assert.h>
int max = INT_MIN; // INT_MIN in <limits.h>
int max2 = INT_MIN;
struct Num *cur = list;
while(cur) {
if( cur->num > max ) {
max2 = max;
max = cur->num;
}
else if( cur->num > max2 ) { // max2 < cur->num <= max
max2 = cur->num;
}
cur = cur->next;
}
if(pmax != NULL) *pmax = max;
if(pmax2 != NULL) *pmax2 = max2;
}完整测试程序:
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
struct Num
{
int num;
struct Num *next;
};
void linkedListMax2(struct Num *list, int *pmax, int *pmax2)
{
assert(pmax != NULL && pmax2 != NULL); // assert() in <assert.h>
int max = INT_MIN; // INT_MIN in <limits.h>
int max2 = INT_MIN;
struct Num *cur = list;
while(cur) {
if( cur->num > max ) {
max2 = max;
max = cur->num;
}
else if( cur->num > max2 ) { // max2 < cur->num <= max
max2 = cur->num;
}
cur = cur->next;
}
if(pmax != NULL) *pmax = max;
if(pmax2 != NULL) *pmax2 = max2;
}
//
// int iarray[] = {5, 4, 3, 2, 1};
int iarray[] = {6, 7, 8, 9, 10};
#define ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
struct Num *list = NULL;
void printList(struct Num *list)
{
while(list) {
printf("%d ", list->num);
list = list->next;
}
}
int main(int argc, char *argv[])
{
int i, max, max2;
for(i = ARRAYSIZE(iarray)-1; i >= 0 ; i--) {
struct Num *pnew = (struct Num*)malloc(sizeof(struct Num));
pnew->num = iarray[i];
pnew->next = list;
list = pnew;
}
printList(list);
linkedListMax2(list, &max, &max2);
printf("\nmax: %d, max2: %d\n", max, max2);
return 0;
}
{
assert(pmax != NULL && pmax2 != NULL); // assert() in <assert.h>
int max = INT_MIN; // INT_MIN in <limits.h>
int max2 = INT_MIN;
struct Num *cur = list;
while(cur) {
if( cur->num > max ) {
max2 = max;
max = cur->num;
}
else if( cur->num > max2 ) { // max2 < cur->num <= max
max2 = cur->num;
}
cur = cur->next;
}
if(pmax != NULL) *pmax = max;
if(pmax2 != NULL) *pmax2 = max2;
}完整测试程序:
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
struct Num
{
int num;
struct Num *next;
};
void linkedListMax2(struct Num *list, int *pmax, int *pmax2)
{
assert(pmax != NULL && pmax2 != NULL); // assert() in <assert.h>
int max = INT_MIN; // INT_MIN in <limits.h>
int max2 = INT_MIN;
struct Num *cur = list;
while(cur) {
if( cur->num > max ) {
max2 = max;
max = cur->num;
}
else if( cur->num > max2 ) { // max2 < cur->num <= max
max2 = cur->num;
}
cur = cur->next;
}
if(pmax != NULL) *pmax = max;
if(pmax2 != NULL) *pmax2 = max2;
}
//
// int iarray[] = {5, 4, 3, 2, 1};
int iarray[] = {6, 7, 8, 9, 10};
#define ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
struct Num *list = NULL;
void printList(struct Num *list)
{
while(list) {
printf("%d ", list->num);
list = list->next;
}
}
int main(int argc, char *argv[])
{
int i, max, max2;
for(i = ARRAYSIZE(iarray)-1; i >= 0 ; i--) {
struct Num *pnew = (struct Num*)malloc(sizeof(struct Num));
pnew->num = iarray[i];
pnew->next = list;
list = pnew;
}
printList(list);
linkedListMax2(list, &max, &max2);
printf("\nmax: %d, max2: %d\n", max, max2);
return 0;
}
全部回答
- 1楼网友:大漠
- 2021-02-01 00:01
额
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯