基于数组的扩缩容栈实现

参照算法第四版写的基于数组的可扩容与缩容的栈实现。

非 -字符入栈,-字符出栈。

接口申明文件:stack_array.h

#ifndef __STACK_ARRAY_H__
#define __STACK_ARRAY_H__
extern int stack_array_size;
void arraystackpush(char** parray, char value);
char arraystackpop(char** parray);
char* resizestack(char* poldstack, int capacity);
int arraystacksize();
int arraystackcapacity();
char* arraystackcreate(int capacity);
int arraystackisempty();// 1 empty, 0 not empty
#endif

接口实现文件:stack_array.c

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

int stack_array_size = 0;
int stack_array_capacity = 0;

char* arraystackcreate(int capacity)
{
	char* pstack = NULL;

    if(0 >= capacity)
    {
        return NULL;
    }

    pstack = (char*)malloc(sizeof(char) * capacity);
    if(NULL == pstack)
    {
        return NULL;
    }

    stack_array_capacity = capacity;
    return pstack;

}

int arraystackcapacity()
{
	return stack_array_capacity;
}

int arraystacksize()
{
	return stack_array_size;
}

int arraystackisempty()
{
	if(0 >= stack_array_size)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

char arraystackpop(char** pparray)
{
	if(0 >= stack_array_size)
	{
		return -1;
	}
	if(stack_array_size <= stack_array_capacity/4)
	{
		*pparray = resizestack((char*)*pparray, stack_array_capacity/2);
		printf("after shrink, the capacity is %d\n", stack_array_capacity);
	}
	return (*pparray)[--stack_array_size];
}

void arraystackpush(char** pparray, char value)
{
	(*pparray)[stack_array_size++] = value;
	if(stack_array_size >= stack_array_capacity)
	{
		*pparray = resizestack(*pparray, 2 * stack_array_capacity);
		printf("after extend, the capacity is %d\n", stack_array_capacity);
	}
}

char* resizestack(char* poldstack, int capacity)
{
	int index = 0;
	char* pnewstack = (char*)malloc(sizeof(char) * capacity);

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

    stack_array_capacity = capacity;
    for(index = 0; index < stack_array_size; index++)
    {
        pnewstack[index] = poldstack[index];
    }

    free(poldstack);
    return pnewstack;

}

测试的的main.c文件:

#include "stack_array.h"
#include <stdio.h>
#include <string.h>
int main ()
{
	char c;

    char* pstack = arraystackcreate(10); 
    int i = 0;
    for(i = 0; i < 100; i++)
    {
        //scanf("%c", &c);
        c = getchar();

    	if ('-' != c && '\n' != c)
    	{
    		arraystackpush(&pstack, c);
    		printf("after push, stack size is:%d, capacity is:%d, input char is:%c\n", arraystacksize(), arraystackcapacity(), c);
    	}
    	else if('-' == c)
    	{
    		printf("after pop, stack size is:%d, capacity is:%d, output char is:%c\n", arraystacksize(), arraystackcapacity(), arraystackpop(&pstack));
    	}
    	else if('\n' == c)
    	{
    		continue;
    	}
	//printf("count i = %d\n", i);
	}

    printf("test successfully\n");
    return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值