Mastering Algorithms with C(stack)

(1)stack.h

/*****************************************************************************
*                                                                            *
*  ------------------------------- stack.h --------------------------------  *
*                                                                            *
*****************************************************************************/

#ifndef STACK_H
#define STACK_H

#include <stdlib.h>

#include "list.h"
#include "stack.h"

/*****************************************************************************
*                                                                            *
*  Implement stacks as linked lists.                                         *
*                                                                            *
*****************************************************************************/

typedef List Stack;

/*****************************************************************************
*                                                                            *
*  --------------------------- Public Interface ---------------------------  *
*                                                                            *
*****************************************************************************/

#define stack_init list_init

#define stack_destroy list_destroy

int stack_push(Stack *stack, const void *data);

int stack_pop(Stack *stack, void **data);

#define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data)

#define stack_size list_size

/*****************************************************************************
*                                                                            *
*  ------------------------------- stack.c --------------------------------  *
*                                                                            *
*****************************************************************************/

/*****************************************************************************
*                                                                            *
*  ------------------------------ stack_push ------------------------------  *
*                                                                            *
*****************************************************************************/

int stack_push(Stack *stack, const void *data) {

/*****************************************************************************
*                                                                            *
*  Push the data onto the stack.                                             *
*                                                                            *
*****************************************************************************/

return list_ins_next(stack, NULL, data);

}

/*****************************************************************************
*                                                                            *
*  ------------------------------ stack_pop -------------------------------  *
*                                                                            *
*****************************************************************************/

int stack_pop(Stack *stack, void **data) {

/*****************************************************************************
*                                                                            *
*  Pop the data off the stack.                                               *
*                                                                            *
*****************************************************************************/

return list_rem_next(stack, NULL, data);

}


#endif

(2)ex-1.c

/*****************************************************************************
*                                                                            *
*  ex-1.c                                                                    *
*  ======                                                                    *
*                                                                            *
*  Description: Illustrates using a stack (see Chapter 6).                   *
*                                                                            *
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#include "list.h"
#include "stack.h"

/*****************************************************************************
*                                                                            *
*  ------------------------------ print_stack -----------------------------  *
*                                                                            *
*****************************************************************************/

static void print_stack(const Stack *stack) {

ListElmt           *element;

int                *data,
                   size,
                   i;

/*****************************************************************************
*                                                                            *
*  Display the stack.                                                        *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Stack size is %d\n", size = stack_size(stack));

i = 0;
element = list_head(stack);

while (i < size) {

   data = list_data(element);
   fprintf(stdout, "stack[%03d]=%03d\n", i, *data);
   element = list_next(element);
   i++;

}

return;

}

/*****************************************************************************
*                                                                            *
*  --------------------------------- main ---------------------------------  *
*                                                                            *
*****************************************************************************/

int main(int argc, char **argv) {

Stack              stack;

int                *data,
                   i;

/*****************************************************************************
*                                                                            *
*  Initialize the stack.                                                     *
*                                                                            *
*****************************************************************************/

stack_init(&stack, free);

/*****************************************************************************
*                                                                            *
*  Perform some stack operations.                                            *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Pushing 10 elements\n");

for (i = 0; i < 10; i++) {

   if ((data = (int *)malloc(sizeof(int))) == NULL)
      return 1;

   *data = i + 1;

   if (stack_push(&stack, data) != 0)
      return 1;

}

print_stack(&stack);

fprintf(stdout, "Popping 5 elements\n");

for (i = 0; i < 5; i++) {

   if (stack_pop(&stack, (void **)&data) == 0)
      free(data);
   else
      return 1;

}

print_stack(&stack);

fprintf(stdout, "Pushing 100 and 200\n");

if ((data = (int *)malloc(sizeof(int))) == NULL)
   return 1;

*data = 100;

if (stack_push(&stack, data) != 0)
   return 1;

if ((data = (int *)malloc(sizeof(int))) == NULL)
   return 1;

*data = 200;

if (stack_push(&stack, data) != 0)
   return 1;

print_stack(&stack);

if ((data = stack_peek(&stack)) != NULL)
   fprintf(stdout, "Peeking at the top element...Value=%03d\n", *data);
else
   fprintf(stdout, "Peeking at the top element...Value=NULL\n");

print_stack(&stack);

fprintf(stdout, "Popping all elements\n");

while (stack_size(&stack) > 0) {

   if (stack_pop(&stack, (void **)&data) == 0)
      free(data);
   
}

if ((data = stack_peek(&stack)) != NULL)
   fprintf(stdout, "Peeking at an empty stack...Value=%03d\n", *data);
else
   fprintf(stdout, "Peeking at an empty stack...Value=NULL\n");

print_stack(&stack);

/*****************************************************************************
*                                                                            *
*  Destroy the stack.                                                        *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Destroying the stack\n");
stack_destroy(&stack);

return 0;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章