栈意味着后进先出,它可以通过简单的迭代程序来实现,我们可以以相反的顺序打印给定的输入数字,然后我们可以将其称为栈实现或不实现? [英] stack means last in first out it can be implement by simple iterative program that we can print the given input number in reverse order then we can call it as stack implementation or not?

查看:86
本文介绍了栈意味着后进先出,它可以通过简单的迭代程序来实现,我们可以以相反的顺序打印给定的输入数字,然后我们可以将其称为栈实现或不实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#inlcude<stdio.h>
main(){
int a,i=0,j,k=0,stack[5];
	for(i=0;i<4;i++){
		scanf("%d\n",&stack[i]);
	}
	for(i=3;i>=0;i--){
		printf("%d\t",stack[i]);
	}
}

(or)
void push();
void display();
int stack[MAX], top=-1, item;
main()
{
int ch;
do
{
printf("\n\n\n\n1.\tPush\n3.\tDisplay\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
display();
break;
default:
printf("\n\nInvalid entry. Please try again...\n");
}
} while(ch!=2);
{scanf("%d",&ch);}
}

void push()
{
if(top == MAX-1)
{printf("\n\nStack is full.");}
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
top++;
stack[top] = item;
printf("\n\nITEM inserted = %d", item);
}
}
void display()
{
int i;
if(top == -1)
{printf("\n\nStack is empty.");}
else
{
for(i=top; i>=0; i--)
{printf("\n%d", stack[i]);}
} 
}</stdio.h>

推荐答案

堆栈的实现应该是透明的-用户应该只看到PUSH和POP例程.
但是,内部通常会像您所做的那样存储很多-数组只是一种存储方法,因此调试工具可以从下向上遍历堆栈并打印内容.

第二个示例实现了一个堆栈,第一个没有实现.

我强烈建议您养成缩进代码的习惯-它使代码更具可读性!
The implementation of the stack should be transparent - users should only see the PUSH and POP routines.
However, internally it is often stored pretty much as you have done - the array is just a storage method, so a debug facility to traverse the stack from the bottom up and print the content is not a problem.

The second example implements a stack, the first doesn''t.

I would STRONGLY suggest that you get into the habit of indenting your code - it makes it a whole lot more readable!


这篇关于栈意味着后进先出,它可以通过简单的迭代程序来实现,我们可以以相反的顺序打印给定的输入数字,然后我们可以将其称为栈实现或不实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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