数据结构--顺序栈实现(c语言)

练习了一下数据结构的栈的实现,感悟就是栈的容量好像没有在数据结构中引用。不过最后测试都通过了。还是老习惯,自己的练习自己看,留下来,提供一下参考,我先把数据结构学习一下,再来研究有应用的技术。基础打扎实点再说别的。稳住!

#include <stdio.h>
#include <alloc.h>

typedef struct array_stack
{
	int	*space;
	int	top;
	int	base;
	int	size;
	int	volume;
}array_stack;

/**
*	visit element method.
*/
void visit(int	data)
{
	printf("%d\t",data);
}

/**
*	init the stack.
*/
void stack_init(array_stack *stack)
{
	(*stack).volume=100;
	(*stack).space=(int *)malloc(sizeof(int)*(*stack).volume);
	(*stack).base=0;
	(*stack).top=-1;
	(*stack).size=0;
}

/**
*	destroy the stack.
*/
void stack_destroy(array_stack *stack)
{
	(*stack).base=0;
	(*stack).top=-1;
	(*stack).size=0;
	(*stack).volume=0;
	free((*stack).space);
}

/**
*	clear the stack.
*/
void stack_clear(array_stack *stack)
{
	(*stack).base=0;
	(*stack).top=-1;
	(*stack).size=0;
}

/**
*	jugde the stack is empty.
*/
int	stack_empty(array_stack *stack)
{
	return (*stack).size==0?1:0;
}

/**
*	get the stack length.
*/
int	stack_length(array_stack *stack)
{
	return (*stack).size;
}

/**
*	get the stack top.
*/
int	stack_get_top(array_stack *stack)
{
	return (*stack).space[(*stack).top];
}

/**
*	push a element to the stack.
*/
void stack_push(array_stack *stack,int	element)
{
	int	move_index=(*stack).base;
	int	*new_space=NULL;
	if((*stack).volume == (*stack).size)
	{
		new_space=(int*)malloc(sizeof(int)*(*stack).volume*2);
		for(;move_index< (*stack).size;move_index++)
		{
			new_space[move_index]=(*stack).space[move_index];
		}
		free((*stack).space);
		(*stack).space=new_space;
		(*stack).volume=(*stack).volume*2;
	}
	(*stack).top++;
	(*stack).space[(*stack).top]=element;
	(*stack).size++;
}

/**
*	pop a element from the stack.
*/
void stack_pop(array_stack *stack,int	*element)
{
	*element=(*stack).space[(*stack).top];
	(*stack).top--;
	(*stack).size--;
}

/**
*	traverse the stack.
*/
void stack_traverse(array_stack *stack,void (*ptr)(int	data))
{
	
	int	move_index=(*stack).base;
	for(;move_index<=(*stack).top;move_index++)
	{
		ptr((*stack).space[move_index]);
	}
}

int	main()
{
	int	move_index=0;
	int	pop_data=0;
	array_stack test_stack;

	stack_init(&test_stack);
	stack_traverse(&test_stack,visit);

	for(;move_index<300;move_index++)
	{
		stack_push(&test_stack,move_index);
	}
	stack_traverse(&test_stack,visit);

	stack_pop(&test_stack,&pop_data);
	printf("\nthe pop number:%d",pop_data);

	printf("\nthe stack is empty ? :%d",stack_empty(&test_stack));
	printf("\nthe stack length:%d",stack_length(&test_stack));
	printf("\nthe stack top:%d",stack_get_top(&test_stack));

	stack_clear(&test_stack);
	printf("\n");
	stack_traverse(&test_stack,visit);

	stack_destroy(&test_stack);
}

数据结构,越写越觉得熟练,理解也更加深刻。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当当小螳螂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值