-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.h
82 lines (80 loc) · 2.49 KB
/
Solution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef __SOLUTION_H__
#define __SOLUTION_H__
typedef struct student_message
{
char *ID = NULL; //学号
double chineseScore = 0; //语文
double mathScore = 0; //数学
double englishScore = 0; //英语
double totalScore = 0; //总分
double aveScore = 0; //加权平均分
student_message *next = NULL;
//计算加权平均分
void calcAveScore()
{
aveScore = chineseScore * 0.3 + mathScore * 0.5 + englishScore * 0.2;
}
//计算总分
void calcTotalScore()
{
totalScore = chineseScore + mathScore + englishScore;
}
void printStuMes()
{
printf(" 学号:%s\n", ID);
printf(" 语文成绩:%5.2lf\n", chineseScore);
printf(" 数学成绩:%5.2lf\n", mathScore);
printf(" 英语成绩:%5.2lf\n", englishScore);
printf(" 总成绩:%5.2lf\n", totalScore);
printf(" 加权平均分:%5.2lf\n", aveScore);
}
} stuNode;
struct listNode
{
int val;
listNode *nextNode = NULL;
};
listNode *reverseList(listNode *headNode)
{
listNode *originNodePointer = headNode; //该指针遍历原链表
listNode *lastNode = NULL;
while (originNodePointer->nextNode)
{
listNode *newNode = (listNode *)malloc(sizeof(listNode)); //创建一个节点
newNode->val = originNodePointer->val; //新建的节点拷贝原节点值
newNode->nextNode = lastNode; //其nextNode指针域指向上次创建的节点,即头插法
lastNode = newNode; //为下次循环做准备,把这次新建的newNode作为下次的behindNode使用
originNodePointer = originNodePointer->nextNode; //原链表接着遍历
}
listNode *newNode = (listNode *)malloc(sizeof(listNode)); //创建一个节点
newNode->val = originNodePointer->val; //新建的节点拷贝原节点值
newNode->nextNode = lastNode; //其nextNode指针域指向上次创建的节点,即头插法
return newNode;
}
void question4()
{
listNode *reverseList(listNode * headNode);
listNode testList[6];
testList[0].val = 2;
testList[0].nextNode = &testList[1];
testList[1].val = 4;
testList[1].nextNode = &testList[2];
testList[2].val = 6;
testList[2].nextNode = &testList[3];
testList[3].val = 1;
testList[3].nextNode = &testList[4];
testList[4].val = 3;
testList[4].nextNode = &testList[5];
testList[5].val = 5;
testList[5].nextNode = NULL;
printf("测试用例为:2 -> 4 -> 6 -> 1 -> 3 -> 5\n");
listNode *pointer = reverseList(testList);
printf("反转结果为:");
while (pointer)
{
printf("%d ", pointer->val);
pointer = pointer->nextNode;
}
free(pointer);
}
#endif