STACK
what is stack ?
Stack is an ordered list in which, insertion and deletion operations can be performed only at one end that is called top.
Stack is a recursive data structures having pointer to its top element.
Stack is also called linear data structure.
Stacks are sometimes called as Last-In-First-Out(LIFO) lists. i.e. element which inserted first in the stack will deleted last from the stack.
Application of Stack
PUSH : Adding elements into stack
POP : Removing elements from stack
/*stack using array
Last-In-First-Out (FIFO)
*/
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct stack{
int item[max], top;
}s;
int index;
//displaying stack elements
void display(){
if(s.top == -1){
printf("\nStack is empty");
}else{
printf("\nThe stack elements are: ");
for(index=0;index<=s.top;index++){
printf("%d\t",s.item[index]);
}
}
}
//inserting elements into stack
void push(){
int element;
if(s.top == (max-1)){
printf("\nStack is full");
}else{
printf("\nEnter new element: ");
scanf("%d",&element);
s.item[++s.top] = element;
printf("\nPUSH operation done successfully");
display();
}
}
//deleting element from stack
void pop(){
if(s.top == -1){
printf("\nStack is empty");
}else{
printf("\nPOP element is: %d",s.item[s.top--]);
printf("\nPOP operation done successfully");
display();
}
}
//main function
int main(){
int ch;
while(1){
printf("\n1.PUSH();\n2.POP();\n3.display();\nexit();\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("\nWrong choice, try again......");
break;
}
}
}
what is stack ?
Stack is an ordered list in which, insertion and deletion operations can be performed only at one end that is called top.
Stack is a recursive data structures having pointer to its top element.
Stack is also called linear data structure.
Stacks are sometimes called as Last-In-First-Out(LIFO) lists. i.e. element which inserted first in the stack will deleted last from the stack.
Application of Stack
- Recursion
- Expression evaluation and conversions
- Parsing
- Browsers
- Editors
- Tree traversals
PUSH : Adding elements into stack
POP : Removing elements from stack
Stack program using Array
Last-In-First-Out (FIFO)
*/
#include<stdio.h>
#include<stdlib.h>
#define max 5
struct stack{
int item[max], top;
}s;
int index;
//displaying stack elements
void display(){
if(s.top == -1){
printf("\nStack is empty");
}else{
printf("\nThe stack elements are: ");
for(index=0;index<=s.top;index++){
printf("%d\t",s.item[index]);
}
}
}
//inserting elements into stack
void push(){
int element;
if(s.top == (max-1)){
printf("\nStack is full");
}else{
printf("\nEnter new element: ");
scanf("%d",&element);
s.item[++s.top] = element;
printf("\nPUSH operation done successfully");
display();
}
}
//deleting element from stack
void pop(){
if(s.top == -1){
printf("\nStack is empty");
}else{
printf("\nPOP element is: %d",s.item[s.top--]);
printf("\nPOP operation done successfully");
display();
}
}
//main function
int main(){
int ch;
while(1){
printf("\n1.PUSH();\n2.POP();\n3.display();\nexit();\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("\nWrong choice, try again......");
break;
}
}
}
output :
Comments
Post a Comment