/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 10
#define TRUE 1
#define FALSE 0
struct stack {
int num[10];
int top;
};
void push(struct stack *ps , int cp){
if(ps->top==STACKSIZE-1){
printf("%s","Stack is Overflow!");
}
else{
ps->top=ps->top+1;
cp=ps->num[ps->top];
}
}
int pop(struct stack *ps){
int a;
if(ps->top==-1){
printf("%s","Stack is Empty");
exit(1);
}
else{
a=ps->num[ps->top];
ps->top=ps->top-1;
}
return a;
}
int empty(struct stack *ps){
if(ps->top==-1){
return(TRUE);
}
else{
return(FALSE);
}
}
int main(){
struct stack first,second;
int i=0,number;
first.top=-1;
second.top=-1;
printf("Enter a number: ");
scanf("%d",&number);
push(&first,number);
while(!empty(&first) && first.top!=STACKSIZE-1){
scanf("%d",&number);
while(first.top!=-1 && number>first.num[first.top]){
if(number>first.num[first.top]){
push(&second,pop(&first));
}
}
push(&first,number);
while(!empty(&second) && second.top!=-1){
push(&first,pop(&second));
}
}
printf("\nAfter Sorting\n");
for(i=0;i<STACKSIZE;i++){
printf("%d\t",first.num[i]);
}
return 0;
}