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. • By default, underlying container is ‘deque’ • 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 IIT Bombay Uses <deque> char to store elements Uses a <vector> int to store elements Stack of size 5, of type float, all initialized to 0. 0 Stack of size 3, of type char, all initialized to ‘a’ Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay #include <iostream> #include <stack> #include <vector> using namespace std; int main () { stack<char> s 1; stack<int, std: : vector<int> > s 2; deque<float> d 1(5, 0. 0); stack<float> s 3 (d 1); vector<char> v 1(3, 'a'); stack<char, vector<char> > s 4 (v 1); 4
Program 1: Basic IIT Bombay } cout << "Size of stacks: “ << s 1. size() << "t" << s 2. size() << "t" << s 3. size() << "t" << s 4. size() << endl; return 0; Output Size of stacks: 0 0 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5 3 5
Program 2: Implement Stack 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++; } cout << "Size of stack: " << s 1. size() << endl; Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6
Program 2: Implement 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 7
Points to remember IIT Bombay • Accessing ‘top’ when stack is empty • Popping elements exceeding the size of stack What if stack was implemented using procedural approach? Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8
Stack: swap() IIT Bombay • Swaps contents of two stacks • Should be of the same type • Size 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 9
References IIT Bombay • 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 10
IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 11
- Slides: 11