Container adapters

18
C++ Algorithms Mohammed Sikander Team Lead Cranes Varsity [email protected]

Transcript of Container adapters

Page 1: Container adapters

C++ Algorithms

Mohammed SikanderTeam LeadCranes [email protected]

Page 2: Container adapters

Mohammed Sikander www.cranessoftware.com

Container adapters, are interfaces created by limiting functionality in a pre-existing container and providing a different set of functionality. When you declare the container adapters, you have an option of specifying which sequence containers form the underlying container.

These are:

Stack

Queue

priority_queue

Page 3: Container adapters

Mohammed Sikander www.cranessoftware.com

The stack class is a container adapter that gives the programmer the functionality of a stack (LIFO) data structure.

The class template acts as a wrapper to the underlying container - only a specific set of functions is provided.

The stack pushes and pops the element from the back of the underlying container, known as the top of the stack.

Page 4: Container adapters

Mohammed Sikander www.cranessoftware.com

Function Name Prototype Description

push void push (const value_type& val); Insert element

pop void pop(); Remove top element

top value_type& top(); Returns a reference to the top element in the stack.

empty bool empty() const; Test whether container is empty

Page 5: Container adapters

Mohammed Sikander www.cranessoftware.com

#include <stack>

int main( ) {

stack<int> s;

int ele , ch;

for(;;) {

cout <<"1. Insert 2. Delete 3. Terminate \n";

cin >> ch;

switch(ch)

{case 1 : cout <<"Enter the element to insert : ";

cin >> ele;

s.push(ele);

break;

case 2 : if(s.empty() == false) {

cout <<"The deleted element is " << s.top()<<endl;

s.pop();

}

break;

default : return 0;

}

}

}

Page 6: Container adapters

Mohammed Sikander www.cranessoftware.com

#include <stack>int main( ){

string str;cout << “Enter the string : “;cin >> str;stack <char> stk;for(int i = 0 ; i < str.length() ; i++)

stk.push(str[i]);

cout <<“The reverse string is \n”;while(stk.empty() == false ){

cout <<stk.top() ;stk.pop();

}}

Page 7: Container adapters

Mohammed Sikander www.cranessoftware.com

Given a sequence consisting of parentheses, determine whether the expression is balanced. A sequence of parentheses is balanced if every open parentheses can be paired uniquely with a closed parentheses that occurs after the former. Also, the interval between them must be balanced. You will be given three types of parentheses: (, {, and [.

{[()]} {[(])} {{[[(())]]}}

Page 8: Container adapters

Mohammed Sikander www.cranessoftware.com

bool checkparenthesis(string s) {stack <char> stk;for(int i = 0 ; i < s.length() ; i++) {

if(s[i] == '[' || s[i] == '(' || s[i] == '{')stk.push(s[i]);

else {if(stk.empty() == true)

return false;if(s[i] == ']' && stk.top() == '[‘) {

stk.pop(); continue;}else if(s[i] == '}' && stk.top() == '{‘) {

stk.pop(); continue;}if(s[i] == ')' && stk.top() == '(‘) {

stk.pop(); continue; }

return false; }

}return stk.empty() ;}

Page 9: Container adapters

Mohammed Sikander www.cranessoftware.com

The queue class is a container adapter that gives the programmer the functionality of a queue (FIFO) data structure.

The queue pushes element from back (rear) and pops the element from front of the underlying container.

Page 10: Container adapters

Mohammed Sikander www.cranessoftware.com

Function Name Prototype Description

push void push (const value_type& val); Insert element

pop void pop(); Remove next element

front value_type& front(); Returns a reference to the first element in the queue.

back value_type& back(); Returns a reference to the last element in the queue.

empty bool empty() const; Test whether container is empty

Page 11: Container adapters

Mohammed Sikander www.cranessoftware.com

#include <queue>

int main( ) {

queue<int> q;

int ele , ch;

for(;;) {

cout <<"1. Insert 2. Delete 3. Terminate \n";

cin >> ch;

switch(ch)

{

case 1 : cout <<"Enter the element to insert : ";

cin >> ele;

q.push(ele);

break;

case 2 : if(q.empty() == false)

{

cout <<"The deleted element is " << q.front()<<endl;

q.pop();

}

break;

default : return 0;

}

}

}

Page 12: Container adapters

Mohammed Sikander www.cranessoftware.com

A priority_queue is a container providing sorted-order access to elements.

You can insert elements in any order, and then retrieve the "lowest" of these values at any time.

Priority queues in C++ STL use a heap structure internally.

Page 13: Container adapters

Mohammed Sikander www.cranessoftware.com

Function Name Prototype Description

push void push (const value_type& val); Insert element

pop void pop(); Remove next element

top value_type& top(); Returns a reference to the top element in the priority queue.

empty bool empty() const; Test whether container is empty

Page 14: Container adapters

Mohammed Sikander www.cranessoftware.com

#include <queue>int main( ) {

priority_queue <int> pq;int ele , ch;for(;;) {

cout <<"1. Insert 2. Delete 3. Terminate \n";cin >> ch;switch(ch){case 1 : cout <<"Enter the element to insert : ";

cin >> ele;pq.push(ele);break;

case 2 : if(pq.empty() == false) {cout <<"The deleted element is " << pq.top()<<endl;pq.pop();

} break;default : return 0;}

}}

Page 15: Container adapters

Mohammed Sikander www.cranessoftware.com

#include <queue>int main( ){

priority_queue <int> pq;int ele;

for(int i = 0 ; i < 5 ; i++){

cin >> ele;pq.push(ele);

}

while(pq.empty() == false){

cout << pq.top() <<" ";pq.pop( );

}}

#include <queue>int main( ){priority_queue <int , vector<int> , greater<int>> pq;

int ele;

for(int i = 0 ; i < 5 ; i++){

cin >> ele;pq.push(ele);

}

while(pq.empty() == false){

cout << pq.top() <<" ";pq.pop( );

}}

Page 16: Container adapters

Mohammed Sikander www.cranessoftware.com

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

sweetness =(1×=(1× Least sweet cookie + 2× 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a

sweetness ≥K.You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.

Input Format The first line consists of integers N, the number of cookies and K, the minimum

required sweetness, separated by a space.The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.

Output Format Output the number of operations that are needed to increase the cookie's

sweetness ≥K≥K.Output −1−1 if this isn't possible.

Page 17: Container adapters

Mohammed Sikander www.cranessoftware.com

Sample Input6 7 1 2 3 9 10 12

Sample Output2

Explanation Combine the first two cookies to create a cookie

with sweetness =1×1+2×2 = 5After this operation, the cookies are 3,5,9,10,12.Then, combine the cookies with sweetness 3 and sweetness 5, to create a cookie with resulting sweetness =1×3+2×5= 13Now, the cookies are 9,10,12,13.All the cookies have a sweetness ≥7.

Thus, 2 operations are required to increase the sweetness.

Page 18: Container adapters

Mohammed Sikander www.cranessoftware.com

int main() {int n , k , ele ;

priority_queue<int , std::vector<int>, std::greater<int>> pq;

cin >> n >> k;for(int i = 0 ; i < n ; i++){

cin >> ele;pq.push(ele);

} int count = 0;while(pq.top() < k){

count++;int x = pq.top();pq.pop();if(pq.empty() == true){

cout << -1 << endl;return 0;

}

int y = pq.top();pq.pop();

int res = x * 1 + y * 2;pq.push(res);

} // End of Whilecout << count << endl;return 0;

}