1 #if !defined( CSTACK_H ) 2 #define CSTACK_H 3 4 /* 5 * An implementation of a char stack in heap memory. 6 * Copyright (C) 2018 Michael Goehler 7 * 8 * This file is part of mdp. 9 * 10 * This program is free software: you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation, either version 3 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program. If not, see <http://www.gnu.org/licenses/>. 22 * 23 * 24 * struct: cstack_t which defines char stack type in heap memory 25 * 26 * function: cstack_init to intialize struct of type cstack_t 27 * function: cstack_t->push to add one char on top if the stack 28 * function: cstack_t->pop to remove the top char from the stack 29 * function: cstack_t->top to test if the top char is a given char 30 * function: cstack_t->empty to test if the stack is empty 31 * function: cstack_t->delete to free the allocated memory 32 * 33 * Example: 34 * cstack_t *p = cstack_init(); 35 * (p->push)(p, 'X'); 36 * printf("%c\n", (p->pop)(p)); 37 * (p->delete)(p); 38 * 39 */ 40 41 #include "common.h" 42 43 typedef struct _cstack_t { 44 wchar_t *content; 45 size_t alloc; 46 size_t size; 47 int head; 48 void (*push)(struct _cstack_t *self, wchar_t c); 49 wchar_t (*pop)(struct _cstack_t *self); 50 bool (*top)(struct _cstack_t *self, wchar_t c); 51 bool (*empty)(struct _cstack_t *self); 52 void (*delete)(struct _cstack_t *self); 53 } cstack_t; 54 55 cstack_t *cstack_init(); 56 void cstack_push(cstack_t *self, wchar_t c); 57 wchar_t cstack_pop(cstack_t *self); 58 bool cstack_top(cstack_t *self, wchar_t c); 59 bool cstack_empty(cstack_t *self); 60 void cstack_delete(cstack_t *self); 61 62 #endif // !defined( CSTACK_H ) 63