in_chainz

Untitled

Dec 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct listNode{
  6.     int val;
  7.     struct listNode* next;
  8. };
  9.  
  10. int main() {
  11.  
  12.     struct listNode* root;
  13.     size_t size = 0;
  14.     int x;
  15.     scanf("%d", &x);
  16.     struct listNode* now = root;
  17.     while (x != 0) {
  18.         now->val = x;
  19.         struct listNode* nd = (struct listNode*)malloc(size + 1);
  20.         now->next = nd;
  21.         now = nd;
  22.         size++;
  23.         scanf("%d", &x);
  24.     }
  25.  
  26.  
  27.     for (int i = 0; i < size - 1; ++i) {
  28.         now = root;
  29.         for (int j = 0; j < size - i - 1; ++j) {
  30.             if (now->val > now->next->val) {
  31.                 int t = now->val;
  32.                 now->val = now->next->val;
  33.                 now->next->val = t;
  34.             }
  35.             now = now->next;
  36.         }
  37.     }
  38.  
  39.     now = root;
  40.     for (int i = 0; i < size; ++i) {
  41.         printf("%d ", now->val);
  42.         now = now->next;
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment