Io & List Insertion Sort

July 9th, 2014

io

1
2
3
int putchar(int c)
putchar(c) -- printf("%c", c) -- putc(int c, stdout)
int getchar() -- getc(stdin)

file version

1
2
int putc(int c, FILE *stream)
int getc(FILE *stream)

EOF – end-of-file

1
#define EOF (-1)

List Insertion sort

list_insertion_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#define N 16

struct node {
  int item;
  struct node* next;
};
typedef struct node* link;

link reverse(link x);

int main() {
  struct node heada, headb;
  link t, u, x, a = &heada, b;
  int i;

  for (i = 0, t = a; i < N; i++) {
    t->next = malloc(sizeof(struct node));
    t = t->next;
    t->next = NULL;
    t->item = rand() % 1000;
  }

  b = &headb;
  b->next = NULL;

  for (t = a->next; t != NULL; t = u) {
    u = t->next;

    for (x = b; x->next != NULL; x = x->next) {
      if (x->next->item > t->item) break;
    }
    t->next = x->next;
    x->next = t;
  }

  for (t = b->next, i = 1; t != NULL; t = t-> next, i++) {
    printf("%3d: %3d ", i, t->item);
  }
  printf("\n");

  a = reverse(b);
  for (t = a, i = 1; t->next != NULL; t = t->next, i++) {
    printf("%3d: %3d ", i, t->item);
  }
  printf("\n");
}

link reverse(link x) {
  link y = x, t, r = NULL;
  while (y != NULL) {
    t = y->next;
    y->next = r;
    r = y;
    y = t;
  }
  return r;
}

Comments