Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms

Similar presentations


Presentation on theme: "Data Structures and Algorithms"— Presentation transcript:

1 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

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

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

4 Program 1: Basic #include <iostream>
#include <stack> #include <vector> using namespace std; int main () { stack<char> s1; stack<int,std::vector<int> > s2; deque<float> d1(5,0.0); stack<float> s3 (d1); vector<char> v1(3,'a'); stack<char, vector<char> > s4 (v1); cout << "Size of stacks: " << s1.size() << "\t" << s2.size() << "\t" << s3.size() << "\t" << s4.size() << endl; return 0; } Empty stack of characters Empty stack of integers using vectors Output Size of stacks: Stack of float type of size 5 and initialized to 0.0 Stack of characters of size 3 and initialized to a Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

5 Program 2: Implement Stack
int main () { stack<char> s1; int i=0; char element='a'; if(s1.empty()==true) cout << "Stack is empty as expected" << endl; else cout << "Stack is not empty" << endl; for(i=0;i<=2;i++) { s1.push(element); cout<<"Element "<<element<<" pushed in"<<endl; element++; } cout << "Size of stack: " << s1.size() << endl; Empty stack of characters 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 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

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

7 What if stack was implemented using procedural approach?
Points to remember 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() Swaps contents of two stacks Should be of the same type
Size may be different int main () { stack<int> s1, s2; int i=0; for(i=0;i<=2;i++) { s1.push(i); } for(i=0;i<=5;i++) { s2.push(i); } cout << "Size of stacks : " << s1.size() << "\t" << s2.size() << endl; swap(s1,s2); return 0; } Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

9 Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay


Download ppt "Data Structures and Algorithms"

Similar presentations


Ads by Google