-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_string.c
79 lines (70 loc) · 1.7 KB
/
mat_string.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* mat_string.c : general matrix <> chaining string library */
#include <stdio.h>
#include <stdlib.h>
#include "mat_string.h"
static struct Elt *Anchor;
void init_string(void)
{
Anchor = END;
}
void list_string(void)
{
struct Elt * T;
int i=1;
T = Anchor;
printf("List of the elements :\n");
while (T != END) { /* there is a following element */
/* object address is in T->obj */
printf("Address of object no %d : 0x%lx\n", i++, (long)T->obj);
T = T->fol; /* next */
}
printf("****** END of list ********\n");
return;
}
struct Elt * add_string(void * objet)
{
void * A;
struct Elt *E, **W; /* W is a pointer of pointer of element */
if ((A = malloc(sizeof(struct Elt))) == NULL) {
fprintf(stderr,"Malloc impossible ! Stopping !\n");
exit(1);
}
/* ok, initialize new element */
E = (struct Elt *)A;
E->obj = objet;
E->fol=END;
W = &Anchor;
while (*W != END) W = &((*W)->fol);
*W = E;
return E;
}
int del_string(void * objet)
{
struct Elt *E, **W; /* W is a pointer of pointer of element */
W = &Anchor;
while (*W != END) {
if ((*W)->obj == objet) { /* found it ! */
E = (*W); /* address of the element to delete */
/* we skip the element we are about to delete */
*W = E->fol;
/* freeing memory taken by element */
free((void*)E);
return 1;
}
W = &((*W)->fol);
}
return 0; /* object not found */
}
void * last_addr_string(void)
{
struct Elt * T;
void * A;
int i=1;
T = Anchor;
if (T == END) return NULL;
while (T != END) { /* there is a following element */
A = T->obj;
T = T->fol; /* next */
}
return A;
}