#include<stdio.h>
#include<conio.h>
#define MAXSTACK 10
int items[MAXSTACK];
int ctr=0;
void push(int item){
items[ctr] = item;
ctr++;
}
int pop(){
return items[--ctr];
}
void modify(){
int i;
for(i=0; i<MAXSTACK; i++){
if(items[i]%2==0){
items[i] = items[i]/2;
}else {
items[i] = items[i]*3+1;
}
}
}
void displayStack(){
int i;
for(i=MAXSTACK; i>=0; i--){
if(items[i]!=0){
printf("%-5d",items[i]);
}
}
}
int main(){
int i,item;
printf("Enter values to stack(10): \n");
for(i=0; i<MAXSTACK; i++){
scanf("%d",&item);
push(item);
}
printf("Original Stack elements:\n");
displayStack();
modify();
printf("\nModified Stack Elements: \n");
displayStack();
printf("\npop():%d",pop());
printf("\npop():%d",pop());
printf("\npop():%d",pop());
getch();
}
Implement this:
S.push(10);
S.push(5);
S.pop();
S.top();
S.push(6);
S.top();