通过c以简单的方式实现堆栈 [英] implementing stack through c in simple way

查看:62
本文介绍了通过c以简单的方式实现堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



i需要学习c中的堆栈实现。

所以请建议我。

hi..

i need to learn stack implementation in c.
so please suggest me.

推荐答案

最简单的方法是使用固定大小的数组和当前top的索引。将项目推送到堆栈时,将它们存储在数组中并递增索引。当你弹出它们时,你会做相反的事情。当然,这有一个缺点,它是一个固定的大小,无论你有多少元素,你也可以达到最大尺寸,然后将无法添加更多的元素。



另一种选择是拥有一个链表,并始终添加和删除。这样你只需要分配你需要的内存。



现在,我不知道简单方法的意思。
The simplest way is to have an array on a fixed size, and an index of the current top. When you push items to the stack you store them in the array and increment the index. When you pop them, you do the other way around. Of course, this has the drawback it''s a fixed size, no matter how many elements you have and you can also reach the maximum size and then would not be able to add more elements.

Another option is to have a linked-list, and always add and remove at the head. This way you allocate only the memory you need.

Now, I don''t know what you mean by "simple way".

在这里:



c-language-programs / implement-stack-and-perform-push-and-pop-operations [ ^ ]



干杯,

Edo
Here you go:

c-language-programs/implement-stack-and-perform-push-and-pop-operations[^]

Cheers,
Edo


这篇关于通过c以简单的方式实现堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆