Sunday, January 31, 2021

Stack in STL C++

 Stacks are linear data structures. The last added element is removed first and the first added element is removed at the last. So it  is a LLIFO or FILO data structure.

Functions associated with stack :

empty() : returns true if stack is empty else false

size() : returns stack size

top() : returns a top reference to the top most element of the stack

push(): to add the element at the top of the stack.

pop() : to delete the top most element of the stack

void showstack(stack <int>s)

{

while(!s.empty())

{

cout<<'\t'<<s.top(); s.pop();


} //while ends

cout<<'\n';

} //to print stack

int main()

{

stack<int> s;

s.push(10); 

s.push(30);

s.push(20); 

s.push(5); 

s.push(1); 

cout<<showstack(s);

cout<<s.size(); cout<<s.top();

cout<<s.top(); 

showstack(s);









0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home