69,382
社区成员
发帖
与我相关
我的任务
分享
//sqstack_store_int.h头文件
#include <stdlib.h>
#include <stdio.h>
#ifndef SQSTACK_H_H
#define SQSTACK_H_H
#ifdef _cplusplus
extern "C" {
#endif
#define SQSTACK_MIN_ALLOCATE_NR 10 //初始分配栈的大小
typedef enum
{
SQSTACK_RET_OK,
SQSTACK_RET_FAIL,
SQSTACK_RET_STOP,
}SqStackRet;
struct _SqStack;
typedef struct _SqStack SqStack;
typedef int BOOL;
typedef int (*SqStackVisitFunc)(int *ctx, int data);
// typedef void (*SqStackDataDestroy)(void *ctx, void *data);
SqStack* sqstack_create(void);
SqStackRet sqstack_clear(SqStack *thiz);//清空栈
BOOL sqstack_empty(SqStack *thiz);//判栈是否为空栈,是返回1,否返回0
SqStackRet sqstack_get_top(SqStack *thiz, int *data);//取栈顶元素
SqStackRet sqstack_push(SqStack *thiz, int data);//入栈
SqStackRet sqstack_pop(SqStack *thiz, int *data);//出栈
SqStackRet sqstack_traverse(SqStack *thiz, SqStackVisitFunc sq_visit, int *ctx);//遍历栈,在本例中没有用它
size_t sqstack_length(SqStack *thiz);
void sqstack_destroy(SqStack *thiz);
void print_stack(SqStack *thiz);//本例没用到
void print_stack_str(SqStack *thiz);//本例用到的输出函数:将栈中元素输出(栈底->栈顶).
#ifdef _cplusplus
}
#endif
#endif
//sqstack_store_int.c文件
#include "sqstack_store_int.h"
struct _SqStack
{
int *base;
int *top;
size_t stack_size;
};
//创建一个空栈
SqStack * sqstack_create(void)
{
SqStack *thiz = (SqStack *)malloc(sizeof(SqStack));
if (thiz != NULL)
{
thiz->base = NULL;
thiz->top = NULL;
thiz->stack_size = 0;
}
return thiz;
}
//判栈是否为空栈,是返回1,否返回0
BOOL sqstack_empty(SqStack *thiz)
{
return ( (thiz == NULL) || ((thiz != NULL) && (thiz->base == thiz->top)) ) ? 1 : 0;
}
SqStackRet sqstack_get_top(SqStack *thiz, int *data)
{
if ((thiz != NULL) && (thiz->base != thiz->top))
{
*data = *(thiz->top - 1);
return SQSTACK_RET_OK;
}
else
{
return SQSTACK_RET_FAIL;
}
}
//判断栈中是否满足need的空间,若空间不够,则分配空间。
static SqStackRet stack_need(SqStack *thiz, size_t need)
{
size_t sqstack_len = sqstack_length(thiz);
if ((sqstack_len + need) > thiz->stack_size)
{
size_t alloc_size = thiz->stack_size + (thiz->stack_size>>2) + SQSTACK_MIN_ALLOCATE_NR; //现有元素的1.5倍,为了避免为0,总是加上一个最小量
int *p = (int *)realloc(thiz->base, sizeof(int) * alloc_size);
if (p != NULL)
{
thiz->base = p;
thiz->top = thiz->base + sqstack_len;
thiz->stack_size = alloc_size;
}
}
return sqstack_len + need <= thiz->stack_size ? SQSTACK_RET_OK : SQSTACK_RET_FAIL;
}
//入栈
SqStackRet sqstack_push(SqStack *thiz, int data)
{
SqStackRet ret = stack_need(thiz, 1);//判断有没有空间,若没有则为其分配空间
if (ret == SQSTACK_RET_OK)
{
*(thiz->top) = data;
thiz->top++;
}
return ret;
}
//当栈中元素个数小于栈容量的一半,并且栈的容量大于最小量SQSTACK_MIN_ALLOCATE_NR时,对栈进行压缩.
static SqStackRet sqstack_shrink(SqStack *thiz)
{
size_t sqstack_len = sqstack_length(thiz);
if (sqstack_len < (thiz->stack_size>>1) && thiz->stack_size > SQSTACK_MIN_ALLOCATE_NR)
{
size_t alloc_size = sqstack_len + (sqstack_len>>1);
int *p = (int *)realloc(thiz->base, alloc_size);
if (p != NULL)
{
thiz->base = p;
thiz->top = thiz->base + sqstack_len;
thiz->stack_size = alloc_size;
}
else
{
return SQSTACK_RET_FAIL;
}
}
return SQSTACK_RET_OK;
}
SqStackRet sqstack_pop(SqStack *thiz, int *data)
{
SqStackRet ret = SQSTACK_RET_FAIL;
if (thiz != NULL)
{
if (!sqstack_empty(thiz))
{
*data = *(--thiz->top);
// ret = sqstack_shrink(thiz);//就是这个地方的问题。用下面的语句替代,整个程序就OK。
ret =SQSTACK_RET_OK;
}
}
return ret;
}
size_t sqstack_length(SqStack *thiz)
{
size_t length = 0;
if (thiz != NULL)
{
length = (thiz->base == thiz->top) ? 0 : (thiz->top - thiz->base);
}
return length;
}
SqStackRet sqstack_clear(SqStack *thiz)
{
if (thiz != NULL)
{
free(thiz->base);
thiz->base = NULL;
thiz->top = NULL;
thiz->stack_size = 0;
}
return SQSTACK_RET_OK;
}
//遍历栈,本例没有用到。
SqStackRet sqstack_traverse(SqStack *thiz, SqStackVisitFunc visit, int *ctx)
{
SqStackRet ret = SQSTACK_RET_OK;
int *iter = thiz->top - 1;
while (iter >= thiz->base && ret != SQSTACK_RET_STOP)
{
ret = visit(ctx, *iter);
iter--;
}
return ret;
}
void sqstack_destroy(SqStack *thiz)
{
sqstack_clear(thiz);
free(thiz);
return;
}
//该函数本例没有用到
void print_stack(SqStack *thiz)
{
if (thiz != NULL)
{
int *iter = thiz->base;
while (iter < thiz->top)
{
printf("%4d\t", *iter);
iter++;
}
}
printf("\n");
return ;
}
//本例中用到的输出函数,以字符形式从栈底到栈顶输出内容
void print_stack_str(SqStack *thiz)
{
if (thiz != NULL)
{
int *iter = thiz->base;
while (iter < thiz->top)
{
printf("%c", *iter);
iter++;
}
}
printf("\n");
return ;
}
//main.c文件
#include "sqstack_store_int.h"
static void line_edit_translate(char *string)
{
SqStack *stk = sqstack_create();
int tmp;
while (*string != '\0')
{
switch (*string)
{
case '$':
if (!sqstack_empty(stk))
{
sqstack_pop(stk, &tmp);
}
break;
case '@':
sqstack_clear(stk);
break;
default:
sqstack_push(stk, *string);
break;
}
string++;
}
print_stack_str(stk);
sqstack_destroy(stk);
return;
}
void test_line_edit(void)
{
//为了方便,我直接将两个例子放在两个字符串中
char *string1 = "whli$$ilr$e(s$*s)";
char *string2 = "outjhgjhgcha@putchar(*s=$++)";
printf("the string1:%s\n", string1);
line_edit_translate(string1);
printf("the string2:%s\n", string2);
line_edit_translate(string2);
}
int main(int argc, char *argv[])
{
test_line_edit();
return 0;
}