testnumlist.c 2.02 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
static char *rcsid = "$Id: testnumlist.c,v 0.2 1991/08/30 10:17:43 ceder Exp $";
Per Cederqvist's avatar
.    
Per Cederqvist committed
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
#include <stdio.h>
#include <stdlib.h>

#include <misc-types.h>

#include "numlist.h"


/* ================================================================ */
/*                        Some useful macros                        */


#define IS_INTERVAL_START(x)  (((x) & INTERVAL_MASK) == INTERVAL_START)
#define IS_INTERVAL_END(x)    (((x) & INTERVAL_MASK) == INTERVAL_END)

#define NUMBER_OF(x)          ((x) & NUMBER_MASK)

#define MAKE_INTERVAL_START(x)  ((x) | INTERVAL_START)
#define MAKE_INTERVAL_END(x)    ((x) | INTERVAL_END)


/* ================================================================ */


void
numlist_node_print(Numlist_node *nln)
{
    int      i;
    u_long   num;

    putchar('[');
    for (i = 0; i < NUM_LONGS_IN_NUMLIST; ++i) {
	if (i < nln->num_numbers) {
	    num = nln->numbers[i];
	    printf("%c%5U%c,", IS_INTERVAL_START(num) ? '<' : ' ',
		   NUMBER_OF(num), IS_INTERVAL_END(num) ? '>' : ' ');
	} else {
	    printf("       ,");
	}
    }
    putchar(']');
    putchar('\n');
}



void
numlist_print(Numlist *nl)
{
    Numlist_node  * nln;

    printf("last_num   = %U\n", nl->last_num);
    printf("last_index = %U\n", nl->last_index);
    nln = nl->first;
    while (nln != NULL) {
	numlist_node_print(nln);
	nln = nln->next;
    }
    putchar('\n');
}



int
main()
{
    Numlist  * nl;
    Bool       finished;

    nl = numlist_create();
    puts("Empty numlist:");
    numlist_print(nl);

    finished = FALSE;
    while (!finished) {
	char     command;
	u_long   num;

	scanf("\n%c%U", &command, &num);
	switch (command) {
	  case 'i':
	    numlist_insert(nl, num);
	    printf("%U inserted\n", num);
	    numlist_print(nl);
	    break;

	  case 'd':
	    numlist_delete(nl, num);
	    printf("%U deleted\n", num);
	    numlist_print(nl);
	    break;

	  case 'q':
	    finished = TRUE;
	    break;

	  case ' ':
	  case '\n': 
	    break;

	  default:
	    printf("Unrecognized command: %c\n", command);
	    break;
	}
    }
    
    return 0;
}