So i'm attempting to sort a linked list of integers but cant seem to figure out the right way to do it, my idea was to take the unordered list, find the largest value from it, and put it into another list. Since I don't believe I could delete the node from the original list without it being doubly linked I had planned to give the node from list 1 a zero value, thereby removing its status as the largest value. Because of this I intended to run it a set number of times, each time finding the next largest value until list 1 is all 0's and list 2 is an ordered version of what list 1 once was. I have created a function to do this but it doesn't seem to be working, although I cannot find a problem.
Functions
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
struct num_node *create(struct num_node *list, int x){
struct num_node *current;
if (list == NULL){
list = (struct num_node*)malloc(sizeof(struct num_node));
list->num = x;
list->next = NULL;
return(list);
}
else{
current = (struct num_node *)malloc(sizeof(struct num_node));
current->num = x;
current->next = list;
return(current);
}
}
void print_nums(struct num_node *list) {
struct num_node *current;
for (current = list; current != NULL; current = current->next)
printf("%d\t", current->num);
}
struct num_node *sort_nums(struct num_node *list1, struct num_node *list2){
struct num_node *current;
struct num_node *large = list1;
for (int i = 0; i < 25; i++){
for (current = list1; current != NULL; current = current->next){
if (current->num > large->num){
large = current;
}
}
create(list2, large->num);
large->num = 0;
return(list2);
}
}
int sum(struct num_node *list){
int total = 0;
struct num_node *current;
for (current = list; current != NULL; current = current->next){
total = total + current->num;
}
return total;
}
float average(struct num_node *list){
float total = 0;
float count = 0;
struct num_node *current;
for (current = list; current != NULL; current = current->next){
total = total + current->num;
count++;
}
return total / count;
}
MAIN
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "functions.h"
int main(){
struct num_node *head = NULL;
struct num_node *new_head = NULL;
srand(time(NULL));
for (int i = 1; i <= 25; i++){
int x = rand() % 100;
head = create(head, x);
}
print_nums(head);
sort_nums(head, new_head);
printf("\n");
printf("\n");
print_nums(new_head);
printf("\n");
printf("\n");
printf("The total of all numbers is: ");
printf("\t%d\n", sum(new_head));
printf("The average of the numbers is: ");
printf("\t%.3f\n", average(new_head));
}