Skip to content

Instantly share code, notes, and snippets.

@thepurpleowl
Last active July 2, 2020 19:25
Show Gist options
  • Save thepurpleowl/b18519bf8f51ed492a28495a42de8f4e to your computer and use it in GitHub Desktop.
Save thepurpleowl/b18519bf8f51ed492a28495a42de8f4e to your computer and use it in GitHub Desktop.
C++ practice
#include <iostream>
using namespace std;
struct node{
int val;
struct node *right, *left;
};
struct node *createNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->val = item;
temp->right = NULL;
temp->left = NULL;
return temp;
}
struct node *insert(struct node *root, int n){
if(root == NULL){
root = createNode(n);
}
else if(root->val<n){
root->right = insert(root->right, n);
}
else if(root->val>=n){
root->left = insert(root->left, n);
}
return root;
}
void inorder(struct node *root){
if(root != NULL){
inorder(root->left);
cout<<(*root).val<<" ";
inorder(root->right);
}
}
int main() {
// your code goes here
int arr[] = {23,1,45,67,89,9};
int len = sizeof(arr)/sizeof(arr[0]);
struct node *root = insert(root, arr[0]);;
for(int i=1;i<len;i++){
insert(root, arr[i]);
}
inorder(root);
return 0;
}
#include <iostream>
#include <map>
#include <string>
// #include <cctype>
#include <iterator>
using namespace std;
char lower(char ip){
if(ip>='A' && ip<='Z'){
ip = ip - ('Z'-'z');
}
return ip;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string str;
cin>>str;
for(int i=0;i<str.size();i++){
str[i] = tolower(str[i]);
}
map<char, int> m;
for(int i=0;i<str.length();i++){
char key = str.at(i);
map<char, int>::iterator found = m.find(key);
if(found != m.end()){
m[key] = m[key] + 1;
}
else{
m.insert(make_pair(key, 1));
// m[key] = 1;
}
}
// map<char, int>::iterator itr;
for(auto i=m.begin();i!=m.end();i++){
cout<<i->first<<" "<<i->second<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
void print_mat(vector<vector <int>> adj_mat){
for(int i=0;i<adj_mat.size();i++){
for(int j=0;j<adj_mat[i].size();j++){
cout<<adj_mat[i][j]<<" ";
}
cout<<endl;
}
}
void dfs(vector<int> stack, vector<vector <int>> adj_mat, int visited[]){
if(!stack.empty()){
int t_root = stack.back();
stack.pop_back();
if(visited[t_root]==0){
cout<<t_root<<endl;
visited[t_root] = 1;
for(int i=0;i<adj_mat[t_root].size();i++){
stack.push_back(adj_mat[t_root][i]);
}
dfs(stack, adj_mat,visited);
}
}
}
void print_dfs(int root, vector<int> stack, vector<vector <int>> adj_mat, int n){
stack.push_back(root);
int visited[n];
for(int i=0;i<n;i++){
visited[i] = 0;
}
dfs(stack, adj_mat, visited);
}
int main() {
int n, ne, temp;
cin>>n;
vector<vector <int>> adj_mat;
//adjacency matrix
for(int i=0;i<n;i++){
cin>>ne;
vector<int> temp_vec;
for(int j=0;j<ne;j++){
cin>>temp;
temp_vec.push_back(temp);
}
adj_mat.push_back(temp_vec);
}
int root;
vector<int> stack;
cin>>root;
print_dfs(root, stack, adj_mat, n);
// print_mat(adj_mat);
return 0;
}
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
struct node{
int val;
struct node *next;
};
struct node *create_node(){
struct node *root = (struct node *)malloc(sizeof(struct node));
root->val = INT_MIN;
root->next = NULL;
return root;
}
void print_array(int arr[], int n){
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void print_ll(struct node *root){
while(root!=NULL && root->val>INT_MIN){
cout<<root->val<<" ";
root = root->next;
}
cout<<endl;
}
int main() {
// your code goes here
int m,n;
cin>>m>>n;
int a1[m], a2[n];
for(int i=0;i<m;i++){
cin>>a1[i];
}
for(int i=0;i<n;i++){
cin>>a2[i];
}
sort(a1,a1+m);
sort(a2,a2+n);
print_array(a1,m);
print_array(a2,n);
struct node *root = create_node();
struct node *m_root = root;
int k =0, l=0, j=0;
int arr[m+n];
while(k<m && l<n){
if(a1[k]<a2[l]){
arr[j] = a1[k];
root->val = a1[k];
root->next = create_node();
root = root->next;
k++;
}
else{
arr[j] = a2[l];
root->val = a2[l];
root->next = create_node();
root = root->next;
l++;
}
j++;
}
while(k<m){
arr[j] = a1[k];
root->val = a1[k];
root->next = create_node();
root = root->next;
k++;
j++;
}
while(l<n){
arr[j] = a2[l];
root->val = a2[l];
root->next = create_node();
root = root->next;
l++;
j++;
}
print_array(arr,m+n);
print_ll(m_root);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment