Paste: 链表
Author: | 菜 |
Mode: | c |
Date: | Wed, 4 Mar 2020 16:18:55 |
Plain Text |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _NODE {
int data;
struct _NODE * next;
}
NODE, * PNODE;
PNODE llist_create_head() {
PNODE node = (PNODE) malloc(sizeof(NODE));
memset(node, 0, sizeof(NODE));
}
PNODE llist_append_to_end(PNODE head, int data) {
PNODE current = head;
while (current->next != NULL) {
current = (PNODE)current->next;
}
PNODE new_node = llist_create_head();
new_node->data = data;
current->next = (PNODE)new_node;
return new_node;
}
void llist_print_all(PNODE head) {
PNODE current = head;
while (current->next != NULL) {
current = (PNODE)current->next;
printf("%d, ", current->data);
}
}
PNODE llist_search(PNODE head, int factor) {
PNODE current = (PNODE)head->next;
PNODE min = current;
while (current->next != NULL) {
current = (PNODE)current->next;
if (current->data * factor < min->data * factor) {
min = current;
}
}
return min;
}
PNODE llist_search_max(PNODE head) {
return llist_search(head, -1);
}
PNODE llist_search_min(PNODE head) {
return llist_search(head, 1);
}
int llist_calculate_total(PNODE head) {
PNODE current = head;
int total = 0;
while (current->next != NULL) {
current = (PNODE)current->next;
total += current->data;
}
return total;
}
int main() {
PNODE head = llist_create_head();
int input;
scanf("%d", & input);
while (input != -1) {
llist_append_to_end(head, input);
scanf("%d", & input);
}
int max = llist_search_max(head)->data;
int min = llist_search_min(head)->data;
int sum = llist_calculate_total(head);
printf("The maximum,minmum and the total are:%d %d %d\n", max, min, sum);
return 0;
}
New Annotation