-
Notifications
You must be signed in to change notification settings - Fork 0
/
leQueue.h
167 lines (141 loc) · 5.36 KB
/
leQueue.h
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*---------------------------------------------------------------------------+
| Module : leQueue.h (versao de formacao)
+----------------------------------------------------------------------------+
|
| Author : Luis Rodrigues & Henrique J. Fonseca
|
| Copyright (c) 1990, INESC.
| All Rights Reserved. Licensed Material-Property of INESC
+----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------+
| Presentation
+----------------------------------------------------------------------+
|
| This module defines the following types:
|
| type QueHead
| type QueElem
|
| And the following functions or macros:
|
| - To create
|
| QueHead* leQueNewHead ( )
| QueElem* leQueNewElem ( )
|
| - To delete
|
| void leQueFreeHead ( QueHead* )
| void leQueFreeElem ( QueElem* )
|
| - To initialize
|
| void leQueHeadInit ( QueHead* )
| void leQueElemInit ( QueElem* )
| void leQueSetKey ( QueElem* qe, int k )
| void leQueGetKey ( QueElem* qe )
| void leQueExec ( QueHead* qh_p, int (*func) ()
|
| - To get
|
| QueElem* leQueGetFirst ( QueHead* qh )
| QueElem* leQueGetLast ( QueHead* qh )
| QueElem* leQueGetPrev ( QueHead* qh, QueElem* qe )
| QueElem* leQueGetNext ( QueHead* qh, QueElem* qe )
| QueElem* leQueFindKey ( QueHead* qh, int key )
|
| - To insert
|
| void leQueInsFirst ( QueHead* qh, QueElem* qe )
| void leQueInsLast ( QueHead* qh, QueElem* qe )
| void leQueInsBefore ( QueHead* qh, QueElem* before, QueElem* qe )
| void leQueInsAfter ( QueHead* qh, QueElem* after, QueElem* qe )
| void leQueInsByKey ( QueHead* qh, QueElem* qe )
|
| - To remove
|
| QueElem* leQueRemElem ( QueHead* qh, QueElem* qe )
| QueElem* leQueRemFirst ( QueHead* qh )
| QueElem* leQueRemLast ( QueHead* qh )
|
| - To test
|
| int leQueTestEmpty ( QueHead* qh)
| int leQueSize ( QueHead* qh)
| int leQueTestIsIn ( QueHead* qh, QueElem* qe )
|
+----------------------------------------------------------------------*/
#ifndef LEQUEUE
#define LEQUEUE
/*---------------------------------------------------------------------+
| Global Definitions
+----------------------------------------------------------------------*/
typedef struct QueElem
{
struct QueElem *prev;
struct QueElem *next;
int key;
} QueElem;
#define SzQueElem sizeof (struct QueElem)
typedef struct QueHead
{
QueElem elem;
int nel;
int maxelem;
} QueHead;
#define SzQueHead sizeof (struct QueHead)
/*------------------------------------------------------------------+
| Macros
--------------------------------------------------------------------*/
#define leQueNewHead() ((QueHead*) malloc (SzQueHead))
#define leQueNewElem() ((QueElem*) malloc (SzQueElem))
#define leQueFreeHead(qh_p) free (qh_p)
#define leQueFreeElem(qe_p) free (qe_p)
#define leQueHeadInit(qh_p, max) \
if (1) {\
((QueHead*)qh_p)->elem.next = (QueElem*) &(qh_p->elem); \
((QueHead*)qh_p)->elem.prev = (QueElem*) &(qh_p->elem); \
((QueHead*)qh_p)->nel = 0; \
((QueHead*)qh_p)->maxelem = max; \
} else
#define leQueElemInit(qe_p) \
if (1) {\
((QueElem*)qe_p)->prev = 0; \
((QueElem*)qe_p)->next = 0; \
} else
#define leQueSetKey(qe_p, k) ((QueElem*)qe_p)->key = k
#define leQueGetKey(qe_p) ((QueElem*)qe_p)->key
#define leQueGetFirst(qh_p) \
(((QueHead*)qh_p)->nel ? ((QueHead*)qh_p)->elem.next : 0)
#define leQueGetLast(qh_p) \
(((QueHead*)qh_p)->nel ? ((QueHead*)qh_p)->elem.prev : 0)
#define leQueGetPrev(qh_p, qe_p) \
((((QueElem*)qe_p)->prev != (QueElem*)(&(qh_p->elem))) ? ((QueElem*)qe_p)->prev : 0)
#define leQueGetNext(qh_p, qe_p) \
((((QueElem*)qe_p)->next != (QueElem*)(&(qh_p->elem))) ? ((QueElem*)qe_p)->next : 0)
#define leQueInsFirst(qh_p, qe_p) \
leQueInsAfter (qh_p, &(qh_p->elem), ((QueElem*)qe_p))
#define leQueInsLast(qh_p, qe_p) \
leQueInsAfter (qh_p, ((QueHead*)qh_p)->elem.prev, ((QueElem*)qe_p))
#define leQueInsBefore(qh_p, qb_p, qe_p) \
leQueInsAfter (qh_p, ((QueElem*)qb_p)->prev, ((QueElem*)qe_p))
#define leQueRemFirst(qh_p) leQueRemElem (qh_p, ((QueHead*)qh_p)->elem.next)
#define leQueRemLast(qh_p) leQueRemElem (qh_p, ((QueHead*)qh_p)->elem.prev)
#define leQueTestEmpty(qh_p) (((QueHead*)qh_p)->nel == 0)
#define leQueSize(qh_p) (((QueHead*)qh_p)->nel)
#define leQueNbElem(qh_p) (((QueHead*)qh_p)->nel)
#define leQueMaxElem(qh_p) (((QueHead*)qh_p)->maxelem)
/*------------------------------------------------------------------+
| Functions
--------------------------------------------------------------------*/
QueElem* leQueFindKey (QueHead* h, int k);
void leQueInsAfter (QueHead* h, QueElem*, QueElem* e);
void leQueInsByKey (QueHead* h, QueElem* e);
QueElem* leQueRemElem (QueHead* h, QueElem* e);
QueElem* leQueIsIn (QueHead* h, QueElem* e);
void leQueExec (QueHead* h, int (*func) ());
void leQueRemAllKey (QueHead* h, int k);
void leQuePush (QueHead* h, int k);
void leQueFreeAll (QueHead* h);
void leQueDup (QueHead* to, QueHead* from);
#endif /* LEQUEUE */