Last active
October 6, 2020 01:42
-
-
Save quinnzipse/e3f7250d3b1b57c63cefbd08ae1f3316 to your computer and use it in GitHub Desktop.
Final Project 2 CS270
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "list.h" | |
int main(int argc, char * argv[]) { | |
int n1[] = {2,4,6,8,10,12,14,16}; | |
int n2[] = {256, 128, 64, 32, 16, 8, 4, 2, 1}; | |
int n3[] = {40, 30, 2, 7, 55, 7, 99}; | |
List *x = createList(n1, 8); | |
List *y = createList(n2, 9); | |
List *z = createList(n3, 7); | |
print(x); | |
print(y); | |
print(z); | |
printf("List x: Sum = %d, Max = %d\n", sum(x), max(x)); | |
printf("List y: Sum = %d, Max = %d\n", sum(y), max(y)); | |
printf("List z: Sum = %d, Max = %d\n", sum(z), max(z)); | |
} | |
List * createList(int nums[], int size){ | |
List *list = (List *) malloc(sizeof(List)); | |
if(size == 0) return list; | |
list->head = (Node *) malloc(sizeof(Node)); | |
list->head->data = nums[0]; | |
Node *temp = list->head; | |
for(int i=1; i<size; i++){ | |
temp->next = (Node *) malloc(sizeof(Node)); | |
temp->data = nums[i]; | |
temp = temp->next; | |
} | |
return list; | |
} | |
int sum(List *aList){ | |
Node *temp = aList->head; | |
int sum = 0; | |
while(temp != NULL){ | |
sum += temp->data; | |
temp = temp->next; | |
} | |
return sum; | |
} | |
int max(List *aList){ | |
Node *temp = aList->head; | |
int max = 0; | |
while(temp != NULL){ | |
if(max < temp->data) | |
max = temp->data; | |
temp = temp->next; | |
} | |
return max; | |
} | |
void print(List *aList){ | |
Node *temp = aList->head; | |
while(temp != NULL){ | |
printf("%d ", temp->data); | |
temp = temp->next; | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment