summaryrefslogtreecommitdiff
path: root/books/workshops/2004/ruiz-et-al/support/dag-quadratic-C/lists.c
blob: 5e431cc2e21d494a5632de31af9c0f8936bd61e4 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/// Ordered lists
/// Author: ChesKo
///****************************************************************************

#include "lists.h"

///****************************************************************************
/// Ordered lists
///****************************************************************************

Varlist *varlistalloc(void) {
  return (Varlist *) malloc(sizeof(struct varlist));
}

Varlist *varlistadd(Varlist *vl, char newsymbol[5], int newdirection) {
  Varlist *newvl;
  Varlist *res;
  res = varlistalloc();

  if ( vl == NULL || strcmp(newsymbol,vl->symbol) < 0 ) {
    newvl = varlistalloc();
    strcpy(newvl->symbol,newsymbol);
    newvl->direction = newdirection;
    newvl->rest = vl;
    res->direction = newdirection;
    res->rest = newvl;
  } else if ( strcmp(newsymbol,vl->symbol) == 0 ) {
    res->direction = vl->direction;
    res->rest = vl;
  } else {
    res->rest = vl;
    while ( vl->rest != NULL && strcmp(newsymbol,vl->rest->symbol) > 0 ) {
      vl = vl->rest;
    }
    if ( vl->rest == NULL || strcmp(newsymbol,vl->rest->symbol) < 0 ) {
      newvl = varlistalloc();
      strcpy(newvl->symbol,newsymbol);
      newvl->direction = newdirection;
      newvl->rest = vl->rest;
      vl->rest = newvl;
      res->direction = newdirection;
    } else {
      res->direction = vl->rest->direction;
    }
  }
  return res;
} 

///****************************************************************************

/// Print function

void print_varlist ( Varlist *vl ) {
  if ( vl == NULL ) {
    printf(".\n");
  } else {
    if ( vl->rest == NULL ) {
      printf("(%s,%d).\n", vl->symbol, vl->direction);
    } else {
      while ( vl->rest != NULL ) {
	printf("(%s,%d) -> ", vl->symbol, vl->direction);
	vl = vl->rest;
      }
      printf("(%s,%d).\n", vl->symbol, vl->direction);
    }
  }
}

///****************************************************************************
/// Integer lists
///****************************************************************************

Intlist *intlistalloc(void) {
  return (Intlist *) malloc(sizeof(struct intlist));
}

Intlist *intlistadd( Intlist *il , int i ) {
  Intlist *newil;
  newil = intlistalloc();
  newil->val = i;
  newil->rest = NULL;
  if ( il == NULL ) {
    return newil;
  } else {
    Intlist *orig = il;
    while ( il->rest != NULL ) 
      il = il->rest;
    il->rest = newil;
    return orig;
  }
}

int intlistlength( Intlist *il ) {
  int res;
  res = 0;
  while ( il != NULL ) {
    il = il->rest;
    res++;
  }
  return res;
}

///****************************************************************************

/// Print function

void print_intlist ( Intlist *il ) {
  if ( il == NULL ) {
    printf(".\n");
  } else {
    if ( il->rest == NULL ) {
      printf("%d.\n", il->val);
    } else {
      while ( il->rest != NULL ) {
	printf("%d -> ", il->val);
	il = il->rest;
      }
      printf("%d.\n", il->val);
    }
  }
}

///****************************************************************************