Advertisement
aaaaaa123456789

Reisyukaku's challenge

Aug 30th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct numberlist {
  5.   long long number;
  6.   struct numberlist * next;
  7. };
  8.  
  9. void add(struct numberlist **, long long);
  10. unsigned dump(struct numberlist *, long long **);
  11. int compareLL(void *, void *);
  12.  
  13. int main (void) {
  14.   int result;
  15.   long long value;
  16.   struct numberlist * numberlist = NULL;
  17.   long long * array;
  18.   unsigned arrayCount;
  19.   do {
  20.     printf("Input value, non-numeric ends input: ");
  21.     result = scanf("%llu", &value);
  22.     if (result) add(&numberlist, value);
  23.   } while (result);
  24.   if (!numberlist) {
  25.     printf("No numbers entered\n");
  26.     return 0;
  27.   }
  28.   arrayCount = dump(numberlist, &array);
  29.   qsort(array, arrayCount, sizeof(long long), &compareLL);
  30.   unsigned current;
  31.   printf("Numbers: %lld", *array); // print the first
  32.   for (current = 1; current < arrayCount; current ++)
  33.     printf(", %lld", array[current]); // print the rest
  34.   putchar('\n'); // end the line
  35.   free(array);
  36.   return 0;
  37. }
  38.  
  39. void add (struct numberlist ** plist, long long number) {
  40.   struct numberlist * newNode = malloc(sizeof(struct numberlist));
  41.   newNode -> number = number;
  42.   newNode -> next = *plist;
  43.   *plist = newNode;
  44. }
  45.  
  46. unsigned dump (struct numberlist * list, long long ** parray) {
  47.   unsigned count = 0;
  48.   struct numberlist * temp = list;
  49.   while (temp) {
  50.     temp = temp -> next;
  51.     count ++;
  52.   }
  53.   *parray = malloc(count * sizeof(long long));
  54.   unsigned current;
  55.   for (current = 0; current < count; current ++) {
  56.     (*parray)[current] = list -> number;
  57.     list = list -> next;
  58.   }
  59.   return count;
  60. }
  61.  
  62. int compareLL (void * number1, void * number2) {
  63.   long long a = *((long long *) number1);
  64.   long long b = *((long long *) number2);
  65.   if (a == b) return 0;
  66.   return (a < b) ? -1 : 1;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement