testnumlist.c 3.05 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
2
3
 * $Id: testnumlist.c,v 0.8 1999/05/24 09:34:14 ceder Exp $
 * Copyright (C) 1991, 1993-1995  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
25
static char *rcsid = "$Id: testnumlist.c,v 0.8 1999/05/24 09:34:14 ceder Exp $";
Per Cederqvist's avatar
.    
Per Cederqvist committed
26
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
27
28
29
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
.    
Per Cederqvist committed
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
124
125
126
127
128
129
130
131
132
133
134
135
136

#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;
}