Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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>
Posted
Updated 20-Oct-11 0:22am
v2

1 solution

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900