aaaaaa123456789 icon

Reisyukaku's challenge

aaaaaa123456789 | PRO | 08/31/13 12:54:03 AM UTC | 0 ⭐ | 681 👁️ | Never ⏰ | []
C |

1.75 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
 
struct numberlist {
  long long number;
  struct numberlist * next;
};
 
void add(struct numberlist **, long long);
unsigned dump(struct numberlist *, long long **);
int compareLL(void *, void *);
 
int main (void) {
  int result;
  long long value;
  struct numberlist * numberlist = NULL;
  long long * array;
  unsigned arrayCount;
  do {
    printf("Input value, non-numeric ends input: ");
    result = scanf("%llu", &value);
    if (result) add(&numberlist, value);
  } while (result);
  if (!numberlist) {
    printf("No numbers entered\n");
    return 0;
  }
  arrayCount = dump(numberlist, &array);
  qsort(array, arrayCount, sizeof(long long), &compareLL);
  unsigned current;
  printf("Numbers: %lld", *array); // print the first
  for (current = 1; current < arrayCount; current ++)
    printf(", %lld", array[current]); // print the rest
  putchar('\n'); // end the line
  free(array);
  return 0;
}
 
void add (struct numberlist ** plist, long long number) {
  struct numberlist * newNode = malloc(sizeof(struct numberlist));
  newNode -> number = number;
  newNode -> next = *plist;
  *plist = newNode;
}
 
unsigned dump (struct numberlist * list, long long ** parray) {
  unsigned count = 0;
  struct numberlist * temp = list;
  while (temp) {
    temp = temp -> next;
    count ++;
  }
  *parray = malloc(count * sizeof(long long));
  unsigned current;
  for (current = 0; current < count; current ++) {
    (*parray)[current] = list -> number;
    list = list -> next;
  }
  return count;
}
 
int compareLL (void * number1, void * number2) {
  long long a = *((long long *) number1);
  long long b = *((long long *) number2);
  if (a == b) return 0;
  return (a < b) ? -1 : 1;
}

Comments