2 * Copyright (C) 2004 Jesus Gimenez, Lluis Marquez and Senen Moya
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /****************************************************************************/
26 * empty -- Indica si la pila est vac¡a o no
28 * *ps: puntero a la pila
30 * TRUE si est vac¡a FALSE si no lo est
32 boolean
empty(struct stack_t
*ps
)
34 return((boolean
)(ps
->top
== -1));
37 /****************************************************************************/
40 * init_stack -- Inicializa la pila
42 * *ps: puntero a la pila
44 void init_stack(struct stack_t
*ps
)
49 /****************************************************************************/
52 * pop -- Extrae el elemento del top de la pila si no est vac¡a
54 * *ps: puntero a la pila
56 * El elemento del top de la pila si no est vac¡a
58 element_type
pop(struct stack_t
*ps
)
60 if (empty(ps
)) return NULL
;
61 return(ps
->items
[ps
->top
--]);
64 /****************************************************************************/
67 * push -- Coloca un valor en la pila
69 * *ps: puntero a la pila
70 * x: valor a colocar en la pila
72 void push(struct stack_t
*ps
, element_type x
)
74 if (ps
->top
== STACKSIZE
-1) {
75 fprintf(stderr
,"Error: Stack Overflow. %d %d\n",ps
->top
,STACKSIZE
-1);
79 ps
->items
[++(ps
->top
)] = x
;
82 /****************************************************************************/
85 * stack_top -- Devuelve sin quitarlo de la pila el elemento que esta en el
86 * top de la misma, si no est vacia
88 * *ps: puntero a la pila
90 * El elemento del top de la pila si no esta vacia
91 * si esta vacia devuelve NULL
93 element_type
stack_top(struct stack_t
*ps
)
95 if (empty(ps
)) return NULL
;
96 return(ps
->items
[ps
->top
]);
99 /****************************************************************************/