The abstract data type Stack


The ADT Stack is specified by The data type stack_object is provided by the ADT user; the data type stack is chosen by the designer of the data structure that implements the ADT. In C, the data type boolean is not provided explicitly, but may be created by the definitions
#define TRUE  1
#define FALSE 0
and the declaration
typedef char boolean;


Implementing Stack as a linked list

typedef struct stack_item
{
  stack_object content;
  struct stack_item *next;
} stack_item;

typedef stack_item *stack;

stack *create_stack(void)
{
  stack *s;
  s = (stack *) malloc(sizeof(stack));
  if(s==NULL)
    {
      printf("OUT OF MEMORY!\n");
      exit(1);
    }

  *s = NULL;
  return s;
}

boolean stack_is_empty(stack *s)
{
  return( (*s==NULL) ? TRUE : FALSE );
}

stack_object *top_of_stack(stack *s)
{
  if(stack_is_empty(s)==TRUE)
    {
      printf("TRIED TO FIND THE TOP OF AN EMPTY STACK!\n");
      exit(1);
    }

  return &((*s)->content);
}

void push_on_stack(stack *s, stack_object *object)
{
  stack_item *new_item;
  new_item = (stack_item *) malloc(sizeof(stack_item));
  if(new_item==NULL)
    {
      printf("OUT OF MEMORY!\n");
      exit(1);
    }

  new_item->content=*object;
  new_item->next=*s;
  *s=new_item;
}

void pop_stack(stack *s)
{
  stack_item *memo;
  
  if(stack_is_empty(s)==TRUE)
    {
      printf("TRIED TO POP AN EMPTY STACK!\n");
      exit(1);
    }

  memo=*s;
  *s=memo->next;
  free(memo);
}
Functions malloc and free are discussed in


Implementing Stack as an array

typedef struct stack
{
  stack_object *array;
  int capacity;
  int count;
} stack;

stack *create_stack(void)
{
  stack *s;
  stack_object *object;

  s = (stack *) malloc(sizeof(stack));
  if(s==NULL)
    {
      printf("OUT OF MEMORY!\n");
      exit(1);
    }      

  object = (stack_object *) malloc(sizeof(stack_object));
  if(object==NULL)
    {
      printf("OUT OF MEMORY!\n");
      exit(1);
    }

  s->array = object;
  s->count = 0;
  s->capacity = 1;
  return s;
}

boolean stack_is_empty(stack *s)
{
  return( (s->count==0) ? TRUE : FALSE );
}

stack_object *top_of_stack(stack *s)
{
  if(stack_is_empty(s)==TRUE)
    {
      printf("TRIED TO FIND THE TOP OF AN EMPTY STACK!\n");
      exit(1);
    }

  return (s->array)+((s->count)-1);
}

void push_on_stack(stack *s, stack_object *object)
{
  stack_object *newarray;

  if(s->count == s->capacity)
    /* DOUBLE THE LENGTH OF THE ARRAY */
    {
      (s->capacity)*=2;
      newarray = (stack_object *)
        realloc(s->array, (s->capacity)*sizeof(stack_object));
      if(newarray==NULL)
        {
          printf("OUT OF MEMORY!\n");
          exit(1);
        }
      s->array = newarray;
    }

  (s->array)[s->count]=*object;
  (s->count)++;
}

void pop_stack(stack *s)
{
  if(stack_is_empty(s)==TRUE)
    {
      printf("TRIED TO POP AN EMPTY STACK!\n");
      exit(1);
    }

  (s->count)--;
}
Function realloc is discussed in
Statement
return (s->array)+((s->count)-1);
in top_of_stack is equivalent to statement
return &((s->array)[(s->count)-1]);
pointer arithmetic is the subject of

Back to the table of contents of Vašek Chvátal's course notes