Problem : https://www.hackerrank.com/challenges/java-exception-handling-try-catch/problem
Solution :
Solution :
#include <stdio.h>
#include<math.h>
void update(int *pa,int *pb) {
// Complete this function
int temp = *pa;
*pa = *pa + *pb;
*pb = abs(temp - *pb);
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Solution:
int Solution::braces(string A) {
// create a stack
stack<char>s;
// process string char by char
int l = A.length();
for(int i =0;i<l;i++){
char c = A[i];
// if c is ( or operator or operand , we push in s
if(c == '(' || (c >= 'a' && c<='z') || c == '+'||c=='-'||c=='*'||c=='/'){
s.push(c);
}
else{
// else
// pop unless we get opening bracket and check the length of popped elements
// if it is > 1
//else end processing and it is redundant
// (a + b) -> 5
int p = 0;
while(s.empty() == false && s.top()!='('){
p++;
s.pop();
}
s.pop();
if(p>1)continue;
else{
return 1;
}
}
}
return 0;
}
Problem :
Solution :
int Solution::solve(string A) {
int l = A.length();
stack<char>s;
int f = 0;
for(int i =0;i<l;i++){
char c = A[i];
if(c == '('){
s.push(c);
}else{
if(s.empty()){
f = 1;
break;
}
s.pop();
}
}
if(f == 1){
return 0;
}
if(!s.empty()){
return 0;
}
return 1;
}
Problem :
#include<iostream>
#include<stack>
using namespace std;
stack<int>s1;
stack<int>s2;
void push(int element){
s1.push(element);
}
int pop(){
if(!s2.empty()){
int r = s2.top();
s2.pop();
return r;
}else{
while(!s1.empty()){
int t = s1.top();
s1.pop();
s2.push(t);
}
int r = s2.top();
s2.pop();
return r;
}
}
int main(){
// s1 and s2
//push : 1 2 3 4
//pop : -> 1
//push : 5 -> 2 3 4 5
//pop : -> 2
push(1);
push(2);
push(3);
push(4);
cout<<pop();
push(5);
cout<<'\n'<<pop();
return 0;}
NOTE : Write code for queue properties and even do check for empty before popping the element from the queue ...
// stack using STL
#include<stack>
#include<iostream>
using namespace std;
int main(){
stack<int>s;
// push
s.push(2);
s.push(4);
s.push(5);
s.push(7);
// 2 4 5 7
//pop
s.pop(); // removed 7
//top
cout<<" top = "<<s.top(); // 5
// is empty
if(s.empty())cout<<"\nyes";
else cout<<"\nNO";
// size
cout<<" \nsize = "<<s.size(); // 3
}
CREATING STACK USING PYTHON
OPERATIONS:
1- PUSH
2- POP
OTHER OPERATIONS:
1- IS EMPTY
2- SIZE
3- TOP
CODE :
We can implement stack using :
1- list
2- collections.deque
3- queue.LifoQueue
1 - IMPLEMENTING STACK USING LIST
CODE:
stack = [] // list is created to be used as stack
stack.append('a') // append() push the element in stack which is actually a list
stack.append('b')
stack.append('c')
print(stack.pop()) // pop() is used to pop / delete element on top of listprint(stack.pop())print(stack.pop())print(stack)
2- IMPLEMENTING STACK USING collections.deque
from collections import deque // importing the librarystack = deque() // initializing a variablestack.append('a') // append function to insert elementstack.append('b')stack.append('c')print(stack.pop()) // pop() function to remove the top elementprint(stack.pop())print(stack.pop())print(stack)
from queue import LifoQueue
stack = LifoQueue(maxsize = 5) // initializing stack with maxsize = 5
print(stack.qsize()) // show the number of elements in the stack
stack.put('a') // put() is used to push elements in the stack
stack.put('b')
stack.put('c')
print("Full: ", stack.full()) // is the stack full or does stack contain maxsize elements
print(stack.get()) // return and remove the top element of the stack
print(stack.get())
print(stack.get())
print("Is Empty : ", stack.empty()) // test is the stack empty or not
STACK IN STL
Standard library provides us stack container with number of built in features.
STACK TEMPLATE :
template <class T, class Container = deque<T> > class stack;#include <iostream>
#include <stack> // for using stack container
using namespace std;
int main()
{
int sum = 0;
stack<int> mystack; // declaring stack
mystack.push(21); // inserting element
mystack.push(32);
mystack.push(23);
mystack.push(63);
mystack.push(212);
while (!mystack.empty()) { // checking the empty condition
sum = sum + mystack.top(); // checking the top element
mystack.pop(); // performing pop operation
}
cout << sum;
return 0;
}