Actual source code: stack.c

  1: /*$Id: stack.c,v 1.2 2001/04/10 19:37:38 bsmith Exp $*/
  2: /******************************************************************************
  3: File: stack.c
  4: -------------
  5:   This file implements the stack abstraction via a linked list ...
  6: *****************************************************************************/
  7: #include <stdio.h>
 8:  #include stack.h
 9:  #include error.h
 10: #include "bss_malloc.h"


 13: /******************************************************************************
 14: Type: stack_CDT
 15: ---------------
 16:   Basic linked list implememtation w/header node and chain
 17: ******************************************************************************/
 18: struct node{
 19:   void  *obj;
 20:   struct node *next;
 21: };


 24: struct stack_CDT{
 25:   int len;
 26:   struct node *top;
 27: };



 31: /******************************************************************************
 32: Function: new_stack()

 34: Input : na
 35: Output: na
 36: Return: pointer to ADT.
 37: Description: This function allocates and returns an empty stack.  
 38: ******************************************************************************/
 39: stack_ADT new_stack(void)
 40: {
 41:   stack_ADT s;


 44:   s = (stack_ADT) bss_malloc(sizeof(struct stack_CDT));
 45:   s->len = 0;
 46:   s->top = NULL;
 47:   return(s);
 48: }



 52: /******************************************************************************
 53: Function: free_stack()

 55: Input : pointer to ADT.
 56: Output: na
 57: Return: na
 58: Description: This function frees the storage associated with stack but not any
 59: pointer contained w/in.
 60: ******************************************************************************/
 61: void free_stack(stack_ADT s)
 62: {
 63:   struct node *hold, *rremove;

 65: #ifdef DEBUG
 66:   if (!s->len)
 67:     {
 68:       if (s->top)
 69:         {error_msg_fatal("free_stack() :: len=0 but top NULL?");}
 70:     }

 72:   if (s->len)
 73:     {
 74:       if (!s->top)
 75:         {error_msg_fatal("free_stack() :: len!=0 but top is NULL?");}
 76:     }
 77: #endif

 79: #ifdef INFO
 80:   /* could be destroying the only pointer to prev malloced space!!! */
 81:   if (s->len)
 82:     {error_msg_warning("free_stack() :: prev malloced space?");}
 83: #endif

 85:   /* should use while (s->len--) {pop();} but why waste the fct calls */
 86:   hold = s->top;
 87:   while ((rremove = hold))
 88:     {
 89:       hold = hold->next;
 90:       bss_free(rremove);
 91:     }

 93:   bss_free(s);
 94: }



 98: /******************************************************************************
 99: Function: push()

101: Input : pointer to ADT and pointer to object
102: Output: na
103: Return: na
104: Description: This function adds obj to the top of the stack.
105: ******************************************************************************/
106: void push(stack_ADT s, void *obj)
107: {
108:   struct node *new_node;


111:   new_node = (struct node *) bss_malloc(sizeof(struct node));
112:   new_node->next = s->top;
113:   new_node->obj  = obj;

115:   s->top = new_node;
116:   s->len++;
117: }



121: /******************************************************************************
122: Function:pop()  

124: Input : pointer to ADT
125: Output: na 
126: Return: void * to element
127: Description: This function removes the data value at the top of the stack
128: and returns it to the client.  popping an empty stack is an error.
129: ******************************************************************************/
130: void *pop(stack_ADT s)
131: {
132:   struct node *hold;
133:   void *obj;


136:   if (!s->len--)
137:     {error_msg_fatal("pop :: trying to remove from an empty stack!");}

139:   hold = s->top;
140:   s->top = s->top->next;

142: #ifdef DEBUG
143:   if (!hold)
144:     {error_msg_fatal("pop :: len > 0 but head NULL?");}
145: #endif

147:   obj = hold->obj;
148:   bss_free(hold);
149:   return(obj);
150: }



154: /******************************************************************************
155: Function: len_stack()

157: Input : pointer to ADT
158: Output: na
159: Return: integer number of elements
160: Description: This function returns the number of elements in the stack.
161: ******************************************************************************/
162: int len_stack(stack_ADT s)
163: {
164:   return(s->len);
165: }