What is Stack ?
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).
How swap two two element :
function:
void swap()
{
int t;
t=stack[top];
stack[top]=stack[top-1];
stack[top-1]=t;
}
#include< stdio.h>
#include< conio.h>
#define max 50
int stack[max],top=-1;
void create()
{
char ch;
do
{
top++;
printf("Enter Number\n");
scanf("%d",&stack[top]);
printf("Continue(y/n)\n");
ch=getch();
}while(ch=='y');
}
void traverse()
{
int i;
printf("Stack is :\n");
for(i=top;i>=0;i--){
printf("%d ",stack[i]);
}
}
void swap()
{
int t;
t=stack[top];
stack[top]=stack[top-1];
stack[top-1]=t;
}
void main()
{
clrscr();
create();
traverse();
swap();
printf("After swap top two element\n");
traverse();
getch();
}
Enter Number
2
Continue(y/n)
Enter Number
7
Continue(y/n)
Enter Number
9
Continue(y/n)
Enter Number
1
Continue(y/n)
Stack is :
1 9 7 3
After Swap top two element of stack
Stack is :
9 1 7 3