source: Deliverables/D3.1/C-semantics/test/insertsort.c @ 479

Last change on this file since 479 was 479, checked in by campbell, 8 years ago

Test of linked list insertion sort.

File size: 1.4 KB
Line 
1struct list {
2  unsigned char i;
3  __pdata struct list *next;
4};
5
6/* Init_addrof not yet supported
7__pdata struct list l6 = {69, 0};
8__pdata struct list l5 = {36, &l6};
9__pdata struct list l4 = {136, &l5};
10__pdata struct list l3 = {105, &l4};
11__pdata struct list l2 = {234, &l3};
12__pdata struct list l1 = {240, &l2};
13__pdata struct list l0 = {102, &l1};
14*/
15__pdata struct list l6 = {69, 0};
16__pdata struct list l5 = {36, 0};
17__pdata struct list l4 = {136, 0};
18__pdata struct list l3 = {105, 0};
19__pdata struct list l2 = {234, 0};
20__pdata struct list l1 = {240, 0};
21__pdata struct list l0 = {102, 0};
22
23void insert(__pdata struct list *element, __pdata struct list **dest) {
24  if (*dest == 0 || (*dest)->i >= element->i) {
25    element -> next = *dest;
26    *dest = element;
27  } else
28    insert(element, &(*dest)->next);
29}
30
31void sort(__pdata struct list **list) {
32  __pdata struct list *current;
33  __pdata struct list *next = *list;
34  __pdata struct list *result = 0;
35  while (next) {
36    current = next;
37    next = next->next;
38    insert (current, &result);
39  }
40  *list = result;
41}
42
43extern void out(unsigned char c);
44
45int main(void) {
46  __pdata struct list *l = &l0;
47
48  /* Init_addrof not yet supported */
49  l0.next = &l1;
50  l1.next = &l2;
51  l2.next = &l3;
52  l3.next = &l4;
53  l4.next = &l5;
54  l5.next = &l6;
55
56  sort(&l);
57  while (l) {
58    out(l->i);
59    l = l->next;
60  }
61
62  return 0;
63}
Note: See TracBrowser for help on using the repository browser.