Skip to content

Instantly share code, notes, and snippets.

View pbesra's full-sized avatar
🎯
Focusing

Prakash Besra pbesra

🎯
Focusing
View GitHub Profile
@pbesra
pbesra / main.cpp
Last active January 5, 2019 14:15
AVL Tree | C++
#include <iostream>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
int height;
node(int data){
this->data=data;
@pbesra
pbesra / main.cpp
Created January 4, 2019 19:24
Difference between Height of left subtree and Height of right subtree | C++
#include <iostream>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int data){
this->data=data;
this->left=nullptr;
@pbesra
pbesra / main.cpp
Created January 3, 2019 17:58
sub-string | C++
#include <iostream>
#include <vector>
using namespace std;
void subString(string a, int n){
int x=0, i=0, j=0;
vector<string> v;
string r="";
r=r+a[0];
v.push_back(r);
while(i<n){
@pbesra
pbesra / main.cpp
Created January 3, 2019 14:36
if two strings are Anagram | C++
#include <iostream>
using namespace std;
bool ifAnagram(string s, string a){
int c1[26]; // count of each character from a to z for string s
int c2[26]; // count of each character from a to z for string a
// if string lengths of s and that a are different then no anagram
if(s.length()!=a.length()){
return false;
}
// initially c1, c2 store zero for every character
@pbesra
pbesra / main.cpp
Created January 3, 2019 14:16
Substring search in main string using KMP algorithm | C++
#include <iostream>
using namespace std;
//
int searchPattern(string s, string pattern){
int n=s.length();
int m=pattern.length();
int i=0;
while(i<n){
if(s[i]==pattern[0]){ // if char at index=0 of pattern equals char at index=i of main string
int j=0;
@pbesra
pbesra / main.c
Created December 24, 2018 17:47
Reading file in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *ptr_file; // pointer to file
char buf[1000]; // stores each line of file
char file[100]="H/file.bin";
ptr_file =fopen(file, "r"); // opens file in read mode
if (!ptr_file){ // checks if there is no file
return 1;
@pbesra
pbesra / main.cpp
Created December 21, 2018 18:50
working with header file | cpp file | custom class
//-------------------- node.h--------------
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
template <class T>
class node{
public:
T data;
node<T>* next;
@pbesra
pbesra / main.cpp
Created December 5, 2018 16:56
Working with header files and cpp files in C++
// If a project is big, then it may include more than one file. In fact, there may be hundreds of files.
// In C++, there is file called header file and another one called cpp file. They work together to accomplish something
// header file is where class/struct is defined and then there is another file, let's call it header_cpp, methods defined in header
//file are given bodies in header_cpp file. Finally there is a main file, let's call it, main.cpp, this is a driver file.
// Let's take an example
// --------HEADER FILE, test.h-----------
#ifndef TEST_H // include guard
#define TEST_H
@pbesra
pbesra / main.cpp
Created November 15, 2018 13:02
Activity Selection Problem | C++
// activity selection problem
#include <iostream>
#include <vector>
using namespace std;
void schedule(int* a, int* s, int* f, int n){
vector<int> act;
act.push_back(0);
int ft=f[0];
@pbesra
pbesra / main.cpp
Created November 15, 2018 12:04
Adjacency list in c++ | Graph
#include <iostream>
using namespace std;
// creates nodes to store vertices
class node{
public:
int dest;
node* next;
};
// creates adjacency list with head as node type
class adList{