Stacks
Stacks are a type of Non Primitive Data Structures in C/C++ where NON Primitive means derived from basic data types ie. int, char, float.
It is an ordered collection of elements that works on the principle of LIFO which is an abbreviation for LAST IN FIRST OUT.
In layman's language this means the element that was input at the last is to be popped out first.
Now when I say element popped it means only one element can be deleted each time also the position is fixed ie the last position.
Summing it up by saying both the operations can be done from only one end.
So if I talk about Static Stacks the word static here means the size will be fixed hence it follows the concept of arrays. And so I started the index as 0.
Relating the above diagram with a real life example. If you have a stack of CD's which CD can you pull out first? The CD at the Top? Which is the last CD you had put in the stack. And So goes here. The first element that can be popped out is 'C at index 2.
Now there are two Operations on Stacks
1. Push which means insertion.
2. Pop which means deletion.
Now Here is the Syntax :
C++
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
const int size=20;
int top=-1, a[size];
void push()
{ if(top==size-1)
{ cout<<"Overflow";
exit(0);
}
top++;
int num; cin>>num;
a[top]=num;
}
void pop()
{ if(top==-1)
{ cout<<"Underflow";
exit(0);
}
int num;
num=a[top];
cout<<"Deleted "<<num;
top--;
}
void display()
{ for(int i=0;i<=top;i++)
{cout<<a[i];
cout<<'\n';
}
}
void main()
{ char ch;
do {clrscr();
cout<<"\nPush an Element 1";
cout<<"\nPop an Element 2";
cout<<"\nDisplay Stack 3";
cout<<"\nExit 4 ";
int n;
cin>>n;
switch(n)
{ case 1:{ clrscr();
push();}break;
case 2:{ clrscr();
pop();}break;
case 3:{ clrscr();
display();}break;
case 4:exit(0);
}cout<<"Want to Continue? ";cin>>ch;
}while(ch=='Y' || ch=='y');
getch();
}
---------------------------------------------------
C
#include<conio.h>
#include<stdio.h>
#include<process.h>
int top=-1, a[10];
void push()
{ int num;
if(top == 10)
{ printf("\nOverflow");
getch();
exit(0);
}
top++;
scanf("%d",&num);
a[top]=num;
}
void pop()
{ int num;
if(top==-1)
{ printf("\nUnderflow");
getch();
exit(0);
}
num = a[top];
printf("Deleted %d",num);
top--;
}
void display()
{ int i;
for( i = top; i>=0; i--)
printf("%d\n",a[i]);
}
void main()
{ char ch;int n;
do {clrscr();
printf("\nPush an Element 1");
printf("\nPop an Element 2");
printf("\nDisplay Stack 3");
printf("\nExit 4");
scanf("%d",&n);
switch(n)
{ case 1:{ clrscr();
push();}break;
case 2:{ clrscr();
pop();}break;
case 3:{ clrscr();
display();}break;
case 4:exit(0);
}printf("\nWant to Continue? ");scanf("%d",&ch);
}while(ch=='Y' || ch=='y');
getch();
}
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeletegoogle-cloud-platform-training-in-chennai
This comment has been removed by the author.
ReplyDeleteI am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog.
ReplyDeletejava training in chennai
java training in omr
aws training in chennai
aws training in omr
python training in chennai
python training in omr
selenium training in chennai
selenium training in omr