00001 /* Copyright (c) 2006-2007, Philip Busch <broesel@studcs.uni-sb.de> 00002 * All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * - Redistributions of source code must retain the above copyright notice, 00008 * this list of conditions and the following disclaimer. 00009 * - Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * 00013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00014 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00015 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00016 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00017 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00018 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00019 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00020 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00021 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00022 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00023 * POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00026 #include <stdio.h> 00027 #include <stdlib.h> 00028 #include <assert.h> 00029 #include <string.h> 00030 #include "list.h" 00031 #include "stack.h" 00032 00033 00050 void stack_init(stack *_s) 00051 { 00052 assert(_s != NULL); 00053 00054 _s->data = NULL; 00055 _s->size = 0; 00056 } 00057 00064 stack *stack_create() 00065 { 00066 stack *s; 00067 00068 if((s = (stack *)malloc(sizeof(stack))) != NULL) { 00069 stack_init(s); 00070 } 00071 00072 return(s); 00073 } 00074 00084 int stack_push(stack *_s, void *_elem) 00085 { 00086 int e = 0; 00087 00088 if((e = list_push(&_s->data, _elem)) >= 0) 00089 _s->size++; 00090 00091 return(e); 00092 } 00093 00106 void *stack_pop(stack *_s) 00107 { 00108 if(_s->size == 0) { 00109 return(NULL); 00110 } else { 00111 _s->size--; 00112 return(list_pop(&_s->data)); 00113 } 00114 } 00115 00125 size_t stack_size(stack *_s) 00126 { 00127 return(_s->size); 00128 } 00129 00137 int stack_is_empty(stack *_s) 00138 { 00139 return(_s->size == 0); 00140 } 00141 00152 void stack_delete(stack *_s) 00153 { 00154 if(_s != NULL) { 00155 list_delete(&_s->data); 00156 free(_s); 00157 } 00158 } 00159 00171 void stack_reset(stack *_s) 00172 { 00173 if(_s != NULL) { 00174 list_delete(&_s->data); 00175 stack_init(_s); 00176 } 00177 } 00178 00179 00180 00191 void stack_destroy(stack *_s) 00192 { 00193 if(_s != NULL) { 00194 list_destroy(&_s->data); 00195 free(_s); 00196 } 00197 } 00198 00208 node_l *stack_data(stack *_s) 00209 { 00210 return(_s->data); 00211 } 00212