/bncsutil/src/bncsutil/stack.c

http://ghostcb.googlecode.com/ · C · 113 lines · 61 code · 21 blank · 31 comment · 11 complexity · 7788092ac4f60ca4e10ee3b98f063ea1 MD5 · raw file

  1. /**
  2. * BNCSutil
  3. * Battle.Net Utility Library
  4. *
  5. * Copyright (C) 2004-2006 Eric Naeseth
  6. *
  7. * Stack
  8. * August 16, 2005
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library 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 GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * A copy of the GNU Lesser General Public License is included in the BNCSutil
  21. * distribution in the file COPYING. If you did not receive this copy,
  22. * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  23. * Boston, MA 02111-1307 USA
  24. */
  25. /*
  26. * Mule Server
  27. * Copyright (c) 2004-2006 Eric Naeseth.
  28. *
  29. * Stack
  30. * May 13, 2005
  31. */
  32. #include <bncsutil/stack.h>
  33. #include <stdlib.h>
  34. cm_stack_t cm_stack_create()
  35. {
  36. cm_stack_t stack = (cm_stack_t) calloc(1, sizeof(struct cm_stack));
  37. if (!stack)
  38. return (cm_stack_t) 0;
  39. return stack;
  40. }
  41. void cm_stack_destroy(cm_stack_t stack)
  42. {
  43. cm_stack_node_t* node;
  44. cm_stack_node_t* next;
  45. if (!stack)
  46. return;
  47. node = stack->top;
  48. while (node) {
  49. next = node->next;
  50. free(node);
  51. node = next;
  52. }
  53. free(stack);
  54. }
  55. void cm_stack_push(cm_stack_t stack, void* item)
  56. {
  57. cm_stack_node_t* new_node;
  58. if (!stack || !item)
  59. return;
  60. new_node = (cm_stack_node_t*) malloc(sizeof(cm_stack_node_t));
  61. if (!new_node)
  62. return;
  63. new_node->next = stack->top;
  64. new_node->value = item;
  65. stack->size++;
  66. stack->top = new_node;
  67. }
  68. void* cm_stack_pop(cm_stack_t stack)
  69. {
  70. cm_stack_node_t* next;
  71. void* value;
  72. if (!stack || !stack->top)
  73. return (void*) 0;
  74. next = stack->top->next;
  75. value = stack->top->value;
  76. free(stack->top);
  77. stack->top = next;
  78. stack->size--;
  79. return value;
  80. }
  81. void* cm_stack_peek(cm_stack_t stack)
  82. {
  83. if (!stack || !stack->top)
  84. return (void*) 0;
  85. return stack->top->value;
  86. }
  87. unsigned int cm_stack_size(cm_stack_t stack)
  88. {
  89. if (!stack)
  90. return 0;
  91. return stack->size;
  92. }