Skip to content

Commit

Permalink
brackets, stack, queue, deque, maps and sets
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Mar 7, 2024
1 parent 7ca5672 commit a31e5d9
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
77 changes: 77 additions & 0 deletions theory/data_structure/bracket_matching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>
#include <vector>
#include <stack>

std::vector<int> stringToDigit(char *s1) {

std::vector<int> ans;

for(int i = 0; s1[i] != '\0'; ++i) {
switch(s1[i]) {
case '{':
ans.emplace_back(1);
break;
case '}':
ans.emplace_back(2);
break;
case '(':
ans.emplace_back(3);
break;
case ')':
ans.emplace_back(4);
break;
case '[':
ans.emplace_back(5);
break;
case ']':
ans.emplace_back(6);
break;
default:
break;
}
}

return ans;

}

bool valid(std::vector<int> &brackets) {

std::stack<int> st;
int N = (int)brackets.size();

for(int i = 0; i < N; ++i) {
if(st.empty()) {
if(brackets[i] % 2 != 0)
st.push(brackets[i]);
else
return false;
}
else if (st.top() == brackets[i] - 1 && brackets[i] % 2 == 0)
st.pop();
else if (brackets[i] % 2 != 0)
st.push(brackets[i]);
else
return false;
}

return st.empty();

}

int main(void) {

std::vector<int> brackets;
int n;
std::cin >> n;

for(int i = 0; i < n; ++i) {
char s[300];
std::cin >> s;
brackets = stringToDigit(s);
if(valid(brackets))
std::cout << "Yes" << std::endl;
else
std::cout << "No" << std::endl;
}
}
17 changes: 17 additions & 0 deletions theory/data_structure/custom_comparators_STL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>



int main(){

std::vector<int> nums = {3,4,2,1,3,4,5,7,8,9,93,90,87,68};


}



29 changes: 29 additions & 0 deletions theory/data_structure/dictionary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <unordered_map>

/*
A dictionary is a translator from one type to another
(aplication of maps)
*/

std::unordered_map<char, int> dict;



int main(){

// converting a session of chars to int;
dict['{'] = 1;
dict['}'] = 2;
dict['('] = 3;
dict[')'] = 4;
dict['['] = 5;
dict[']'] = 6;

// we Can do the inverse operation, transforming each number
// in a corresponding char

for(std::unordered_map<char,int>::iterator it = dict.begin(); it != dict.end(); ++it)
std::cout << "key: " << it->first << "\n" << "element: " << it->second << std::endl;

}
35 changes: 35 additions & 0 deletions theory/data_structure/queues.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>
#include <deque>
#include <queue>

/*
# Queues, Deques and Priority Queues
-> Queues, data structure based in fifo "first in, first out",
we've a STL implementation in C++.
*/


int main(){

// Deques (like a double-linked-list)
std::deque<int> deq;
deq.push_back(1); // {1}
deq.push_front(2); // {2,1}
deq.push_back(3); // {2,1,3}

std::cout << deq.front() << "\n"; // {2}
deq.back(); // get's the element in last position
deq.front(); // get's the element in firts position

// Priority queues
std::priority_queue<int> pq;
pq.push(1); // O(log n)
pq.push(2);
pq.push(3);
// pq = {1,2,3}
pq.top(); // {3}


}
36 changes: 36 additions & 0 deletions theory/data_structure/unordered_map_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <unordered_map>
#include <unordered_set>



int main(){

// Unordered map operations
std::unordered_map<std::string, int> zipcode;
zipcode.emplace("Blanca",33456); // Insertion O(1)
zipcode["Blanca"] = 33009; // change O(1)

if(zipcode.find("Blanca") != zipcode.end()) // search O(1)
std::cout << "Found\n";
else
std::cout << "Not Found\n";
//zipcode.erase("Blanca"); // deletion O(1)

for(const auto &element: zipcode)
std::cout << "key: " << element.first << " " << "value: " << element.second << std::endl;

// Unordered set operations
// same functions as unordered map
std::unordered_set<std::string> friends;
friends.insert("Julia");
friends.insert("Martha");
friends.erase("Julia");
if(friends.find("Julia") != friends.end())
std::cout << "Found\n";
else
std::cout << "Not found\n";


}

0 comments on commit a31e5d9

Please sign in to comment.