IIT Bombay Data Structures and Algorithms Prof Ajit

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Stacks using STL Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1

The Stack Class IIT Bombay • Last in First Out (Push and Pop – Both from the top) • Container Adaptor • Created using existing container. • Must use #include<stack> at the beginning of the program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2

The Stack Class: Member Functions IIT Bombay • Member Functions • empty() • size() • top() • push() • pop() • swap() Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3

Program 1: Basic concepts of stack IIT Bombay #include <iostream> #include <stack> using namespace std; int main () { stack<char> s 1; // In this example, we will see how to // push three characters ‘a’, ‘b’ and ‘c’ // on a stack return 0; } Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4

Program 1: Stack (Contd. ) IIT Bombay Empty stack of type char Since Stack is empty, “Stack is empty as expected” is printed Element a pushed in Element b pushed in Element c pushed in Size of stack: 3 int main () { stack<char> s 1; int i=0; char element='a'; if(s 1. empty()==true) cout << "Stack is empty as expected" << endl; else cout << "Stack is not empty" << endl; for(i=0; i<=2; i++) { s 1. push(element); cout<<"Element "<<element<<" pushed in"<<endl; element++; // successive ‘next’ character } cout << "Size of stack: " << s 1. size() << endl; Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5

Program 1: Stack (contd. ) IIT Bombay while(s 1. empty()!=true) { cout << "Top element: " << s 1. top() << endl; s 1. pop(); cout << "Element popped" << endl; } Top element: c Element popped Top element: b Element popped Top element: a Element popped Since all elements are popped out, stack is empty } if(s 1. empty()==true) cout << "Stack is empty" << endl; else cout << "Stack is not empty" << endl; return 0; Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6

Points to remember IIT Bombay Never do the following: • Accessing ‘top’ when stack is empty • Popping elements when stack is empty • Also note, that if we had implemented the stack ourselves, we would have to write all member functions. – Such is the advantage of using STL Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7

Stack: swap() IIT Bombay • Swaps contents of two stacks • Should be of the same type • Sizes may be different int main () { stack<int> s 1, s 2; int i=0; for(i=0; i<=2; i++) { s 1. push(i); } for(i=0; i<=5; i++) { s 2. push(i); } cout << "Size of stacks : " << s 1. size() << "t" << s 2. size() << endl; swap(s 1, s 2); cout << "Size of stacks : " << s 1. size() << "t" << s 2. size() << endl; return 0; } Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8

Take-home activity IIT Bombay Practice problem • Assume that the function swap() is not available in STL • Write your own swap function, and test the above program using it References • https: //en. wikipedia. org/wiki/Stack_(abstract_data_type) • http: //www. cplus. com/reference/stack/ Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9

IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10
- Slides: 10