栈基础

//stacks.h

#ifndef STACK_H
#define STACK_H 1

#ifdef IN_STACK_LIB
#define StkExtern
#else
#define StkExtern   extern
#endif

struct StkElement
{
  int line_no;
  char opener;
};

struct stack_struct
{
  struct StkElement *base;
  int                stack_size;
  int                max_stack;
  int                min_stack;
  int                top;
};

typedef struct stack_struct Stack;

StkExtern void   ClearStack    ( Stack * );
StkExtern Stack* CreateStack   ( int );
StkExtern int    PopElement    ( Stack *, struct StkElement * );
StkExtern int    PushElement   ( Stack *, struct StkElement * );
StkExtern struct StkElement *
                 ViewElement   ( Stack *, int );
#endif

//stacks.c
#define IN_STACK_LIB 1

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "stacks.h"

void ClearStack(Stack* this_stack)
{
  this_stack->top = -1;
}

Stack* CreateStack( int how_many)
{
  Stack *pstk;

  assert(how_many > 0);

  pstk = (Stack*) malloc(sizeof(Stack));

  if(pstk == NULL)
  {
    return NULL;
  }

  pstk->base = (struct StkElement *)malloc(how_many * sizeof(struct StkElement));

  if(pstk->base == NULL)
  {
    return NULL;
  }

  pstk->min_stack = 0;
  pstk->max_stack = how_many - 1;
  ClearStack(pstk);

  return pstk;
}

int PopElement( Stack *this_stack, struct StkElement *destination)
{
  assert(this_stack != NULL);

  if(this_stack->top = -1)
  {
    return (0);
  }

  memmove(destination, &(this_stack->base[this_stack->top]), sizeof(struct StkElement));

  this_stack->top -= 1;

  return (1);
}

int PushElement( Stack *this_stack, struct StkElement *to_push)
{
  assert(this_stack != NULL);

  if(this_stack->top == this_stack->max_stack)
  {
    return 0;
  }

  this_stack->top++;

  memmove(&(this_stack->base[this_stack->top]), to_push, sizeof(struct StkElement));

  return (1);
}

struct StkElement *ViewElement(Stack *this_stack, int which_element)
{
  if(this_stack->top == -1)
  {
    return NULL;
  }

  if(this_stack->top - which_element < 0)
  {
    return NULL;
  }

  return(&(this_stack->base[this_stack->top - which_element]));
}

int main ( int argc, char *argv[] )
{
    FILE *fin;                  /* file we'll be reading from */
    char buffer[128];           /* read file into this buffer */

    int line_count;             /* current line count */
    struct StkElement *stk_el;  /* scratch stack element */
    Stack *stk;                 /* the stack we will use */
    char ch;                    /* character we're examining */
    int i;                      /* for loop count */

    if ( argc != 2 )
    {
        fprintf ( stderr, "Usage: braces filename.ext\n" );
        exit ( EXIT_FAILURE );
    }

    fin = fopen ( argv[1], "rt" );
    if ( fin == NULL )
    {
        fprintf ( stderr, "Cannot open/find %s\n", argv[1] );
        exit ( EXIT_FAILURE );
    }

    /* Create and initialize the stack */

    stk = CreateStack ( 40 );   /* create a stack of 40 items */
    if ( stk == NULL )
    {
        fprintf ( stderr, "Insufficient Memory\n" );
        exit ( EXIT_FAILURE );
    }

    /* Create the scratch stack element */

    stk_el = (struct StkElement *)
                malloc ( sizeof ( struct StkElement ));
    if ( stk_el == NULL )
    {
        fprintf ( stderr, "Insufficient memory\n" );
        exit ( EXIT_FAILURE );
    }

    line_count = 0;

    while ( ! feof ( fin ))
    {
        /* read a line of a C program */
        if ( fgets ( buffer, 127, fin ) == NULL )
            break;

        line_count += 1;

        /* get rid of the trailing carriage return */
        buffer [ strlen ( buffer ) - 1 ] = '\0';

        /* scan and process braces, brackets, and parentheses */
        for ( i = 0; buffer[i] != '\0'; i++ )
        {
            switch ( ch = buffer[i] )
            {
            case '(':
            case '[':
            case '{':
                stk_el->opener  = ch;
                stk_el->line_no = line_count;
                if ( ! PushElement ( stk, stk_el ))
                {
                    fprintf ( stderr, "Out of stack space\n" );
                    exit ( EXIT_FAILURE );
                }
                break;
            case ')':
            case ']':
            case '}':
                if ( ! PopElement ( stk, stk_el ))
                    fprintf ( stderr, "Stray %c at line %d\n",
                                ch,  line_count );
                else
                if (( ch == ')'&& stk_el->opener != '(' ) ||
                    ( ch == ']'&& stk_el->opener != '[' ) ||
                    ( ch == '}'&& stk_el->opener != '{' ))
                    fprintf ( stderr,
                      "%c at line %d not matched by %c at line %d\n",
                       ch, line_count,
                       stk_el->opener, stk_el->line_no );
                break;
            default:
                continue;
            }
        }
    }

    /*  We are at the end of file. Are there unmatched items? */

    if ( ViewElement ( stk, 0 ) != NULL )
        while ( PopElement ( stk, stk_el ) != 0 )
            fprintf ( stderr, "%c from line %d unmatched\n",
                        stk_el->opener, stk_el->line_no );

    fprintf ( stderr, "Error checking complete\n" );

    fclose ( fin );
    return ( EXIT_SUCCESS );
}

 其他参考:

以空间换时间--Google一道关于堆栈操作的面试题

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值