Added models and dicts
[svmtool++.git] / lib / stack.cc
blob8ca7653c0a69880fc149c65917030ae6e986a50d
1 /*
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.
8 *
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
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "stack.h"
23 /****************************************************************************/
26 * empty -- Indica si la pila est  vac¡a o no
27 * Parametros:
28 * *ps: puntero a la pila
29 * Devuelve:
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
41 * Parametros:
42 * *ps: puntero a la pila
44 void init_stack(struct stack_t *ps)
46 ps->top = -1;
49 /****************************************************************************/
52 * pop -- Extrae el elemento del top de la pila si no est  vac¡a
53 * Parametros:
54 * *ps: puntero a la pila
55 * Devuelve:
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
68 * Parametros:
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);
76 exit(1);
78 else
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
87 * Parametros:
88 * *ps: puntero a la pila
89 * Devuelve:
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 /****************************************************************************/
102 stack_t::stack_t()
105 stack_t::~stack_t()