What is Stack|Example of Stack

Stack is a data structure or we can say that its a linear data structure which is used to store data element and retrieved data element in ...

Stack is a data structure or we can say that its a linear data structure which is used to store data element and retrieved data element in LIFO Manner.LIFO stands for Last In First Out.In Stack data element are added,removed from the single end i.e. top of the stack but in queue data element are added,removed from both the ends means insertion from rear end and deletion from front end.In stack we used two terms push and pop.Push means adding element to stack and pop means removing element from stack. 

Let us take an non-programming example:-




In above figure We tried to explain how the stack works in our daily life.Balls are added to the top of the stack.The last ball put in the stack is the first ball removed by the user.




top=4

insert-->push-->top++
delete-->pop-->top--

Functions which are used in Stack:-

1)Push:-Adding element to stack.
2)Pop:-Removing element from stack. 
3)Clear:-Used to clear all the node in the stack.
4)IsEmpty:-Check "Is Stack is Empty".
5)IsFull:-Check "Is Stack is Full".

Programming Example:-Write a Class Stack program which perform this operations:-


1)Push()

2)Pop()
3)Display()

Program:-



#include<iostream.h>

#include<conio.h>
int top=-1;
class stack
{
int st[30];
public:
stack()
{
}
void push(int x)
{
if(top==49)
cout<<"Stack is full";
else 
st[++top]=x;
}
int pop()
{
if(top==-1)
return -999;
else
return st[top--];
}
void display()
{
if(top==-1)
cout<<"Stack is empty";
else
for(int i=0;i<=top;i++)
{
cout<<"\n"<<st[i];
}
}
};

void main()

{
stack object;
int n,ch;
while(1)
{
clrscr();

cout<<"\n Menu\n\1.Push 2.Pop\n 3.Display\n 4. Exit\n Enter your choice";


cin>>ch;

switch(ch)
{
case 1:top==-1;
cout<<"enter a value";
cin>>n;
object.push(x);
break;

case 2:cout<<object.pop();

break;

case 3:object.display();

break;

case 4:Exit(0);

}
}
getch()
}



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

COMMENTS

Name

.Net Framework,8,.Net Tutorials,25,1's,1,2's,1,Addressing mode,1,ASP.NET,2,Associative Memory,1,AVL Tree,1,B-Tree,1,BFS,1,Binary Arithmetic,1,Binary Tree,2,Boolean Algebra,4,Booth's Algo,1,Bubble Sorting,1,C guide,29,C language,2,c plus plus,53,C Plus Plus guide,38,C Programs,16,c#,1,C# Tutorials,15,C++ guide,13,C++ tutorials,38,CASE Tools,1,Circular Linked List,1,Circular Queue,1,Computer Architecture Tutorials,41,Conbinational Circuit,3,Conversion,4,Counter,1,D-Queue,1,Data Bus,1,Data types,1,data-structure,1,Decoder,1,Demux,1,Devices,2,DFS,1,Division Algo,1,Doubly Linked List,1,Encoder,1,ERD,1,FIFO,1,Flip flop,5,flow chart,1,Function Overloading,1,Graph,5,Hashing,1,Infix To Postfix,1,Infix To Prefix,1,Information System,5,Inheritance,1,Insertion Sorting,1,Instate buffer,1,Instruction Cycle,1,Interrupt,1,IT Concepts,13,IT/Networking,27,java,13,K-Map,3,Kruskal's Algorithm,1,LAN,1,Linked List,5,Logic gates,1,Micro-operation,1,MUX,2,Networking,1,Number System,2,Operator Overloading,1,OSI Model,1,Other topics,2,Pipeline,1,Polymorphism,1,Postfix Evaluation,1,Prefix Evaluation,1,Prim's Algorithm,1,Priority Queue,1,Queue,5,Radix Sort Algo,1,Register,1,Router,1,RTL,1,SDLC,13,Searching,3,Security,1,Selection Sorting,1,Shell,1,Shell Sort Algo,1,Shifter,1,Singly Linked List,1,Software,3,Sorting,5,SSAD tutorials,40,Stack,1,Storage Medium,5,String,8,Swiching,1,System,9,System Analyst,1,System Model,6,Tree,5,Unix,2,Virtual Function,1,
ltr
item
All Computer Topics: What is Stack|Example of Stack
What is Stack|Example of Stack
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3zF6P1CeubkhLo5R6REczvytEOKj_rT99KrP8K0EbXh5rGUfJjMz-2aFzNtWOGG45-169mZBDhrizqN7jPpGPDlw4kXWad07xdFEBcCzJReykYRciTEZZAhZZeKDr25Bo07ntH9bFJp4/s320/stack.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3zF6P1CeubkhLo5R6REczvytEOKj_rT99KrP8K0EbXh5rGUfJjMz-2aFzNtWOGG45-169mZBDhrizqN7jPpGPDlw4kXWad07xdFEBcCzJReykYRciTEZZAhZZeKDr25Bo07ntH9bFJp4/s72-c/stack.jpg
All Computer Topics
https://allcomputertopics.blogspot.com/2012/12/what-is-stackexample-of-stack.html
https://allcomputertopics.blogspot.com/
https://allcomputertopics.blogspot.com/
https://allcomputertopics.blogspot.com/2012/12/what-is-stackexample-of-stack.html
true
4800178732260946995
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content