All Codes
All Codes
3 CONCEPT
4 HIERARCHIAL INHERITANCE
5 INHERITANCE
7 MULTIPLE INHERITANCE
8 POLYMORPHISM
9 SINGLE INHERITANCE
13 LINKED LIST
14 REVERSE IN K GROUP
15 LOOP
16 DUPLICATES
17 MERGE LIST
18 SORT
19 PALINDROME
20 ADD
21 CLONE
22 MERGE SORT
EXAMPLE OF CLASS CREATION/PUBLIC/
PRIVATE/CONSTRUCTOR/GETTER SETTER
#include<iostream>
class Hero {
//proper es
private:
int health;
public:
char *name;
char level;
Hero() {
//Paramerterised Constructor
Hero(int health) {
//copy constructor
Hero(Hero& temp) {
char *ch = new char[strlen(temp.name) + 1];
strcpy(ch, temp.name);
this->name = ch;
this->health = temp.health;
this->level = temp.level;
void print(){
int getHealth() {
return health;
char getLevel() {
return level;
void setHealth(int h) {
health = h;
level = ch;
strcpy(this->name, name);
}
return meToComplete ;
//Destructor
~Hero() {
};
int main() {
// Hero a;
// Hero b;
// b. meToComplete = 10 ;
// //Sta c
// Hero a;
// //Dynamic
// delete b;
// Hero hero1;
// hero1.setHealth(12);
// hero1.setLevel('D');
// hero1.setName(name);
// //hero1.print();
// Hero hero2(hero1);
// //hero2.print();
// hero1.name[0] = 'G';
// hero1.print();
// hero2.print();
// hero1 = hero2;
// hero1.print();
// hero2.print();
// //Hero ;
// //object created sta cally
// Hero ramesh(10);
// ramesh.print();
// //dynamically
// h->print();
// temp.print();
/*
//sta c alloca on
Hero a;
a.setHealth(80);
a.setLevel('B');
//dynamicallly
b->setLevel('A');
b->setHealth(70);
*/
// //crea on of Object
// Hero ramesh;
// //use se er
// ramesh.setHealth(70);
// ramesh.level = 'A';
return 0;
AMBUIGUITY
#include<iostream>
class A {
public:
void func() {
};
class B {
public:
void func() {
};
};
int main() {
C obj;
//obj.func();
obj.A::func() ;
obj.B::func();
return 0;
CONCEPT
#include<iostream>
class Student {
private:
string name;
int age;
int height;
public:
int getAge() {
return this->age;
};
int main() {
Student first;
return 0;}
HIERARCHIAL INHERITANCE
#include<iostream>
//Hierarchical Inheritance
class A {
public:
void func1() {
};
class B: public A {
public:
void func2() {
};
class C: public A {
public:
void func3() {
};
int main() {
A object1;
object1.func1();
B object2;
object2.func1();
object2.func2();
C object3;
object3.func1();
object3.func3();
return 0;
INHERITANCE
#include<iostream>
class Human {
private:
int height;
public:
int weight;
private:
int age;
public:
int getAge() {
return this->age;
void setWeight(int w) {
this->weight = w;
};
class Male: private Human {
public:
string color;
void sleep() {
int getHeight() {
return this->height;
};
int main() {
Male m1;
/*
Male object1;
object1.setWeight(84);
object1.sleep();
*/
return 0;
class Animal {
public:
int age;
int weight;
public:
void speak() {
};
};
};
int main() {
GermanShepherd g;
g.speak();
return 0;
MULTIPLE INHERITANCE
#include<iostream>
class Animal {
public:
int age;
int weight;
public:
void bark() {
};
class Human {
public:
string color;
public:
void speak() {
};
};
int main() {
Hybrid obj1;
obj1.speak();
obj1.bark();
return 0;
POLYMORPHISM
#include<iostream>
class A {
public:
void sayHello() {
return 1;
}
};
class B {
public:
int a;
int b;
public:
int add() {
return a+b;
*/
void operator() () {
};
class Animal {
public:
void speak() {
};
void speak() {
};
int main() {
Dog obj;
obj.speak();
/*
B obj1, obj2;
obj1.a = 4;
obj2.a = 7;
obj1 + obj2;
obj1();*/
/*
A obj;
obj.sayHello();
*/
return 0;
}
SINGLE INHERITANCE
#include<iostream>
class Animal {
public:
int age;
int weight;
public:
void speak() {
};
};
int main() {
Dog d;
d.speak();
return 0;
}
CIRCULAR LINKED LIST
#include<iostream>
#include<map>
class Node {
public:
int data;
Node* next;
//constrcutor
Node(int d) {
this->data = d;
this->next = NULL;
~Node() {
if(this->next != NULL) {
delete next;
next = NULL;
cout << " memory is free for node with data " << value << endl;
};
//empty list
if(tail == NULL) {
tail = newNode;
else{
//non-empty list
while(curr->data != element) {
//empty list
if(tail == NULL) {
return ;
do {
} while(tail != temp);
cout << endl;
//empty list
if(tail == NULL) {
cout << " List is empty, please check again" << endl;
return;
else{
//non-empty
prev = curr;
if(curr == prev) {
tail = NULL;
tail = prev;
//empty list
if(head == NULL) {
return true;
if(temp == head ) {
return true;
return false;
if(head == NULL)
return false;
while(temp !=NULL) {
//cycle is present
if(visited[temp] == true) {
return true;
visited[temp] = true;
return false;
int main() {
// insertNode(tail, 5, 3);
//print(tail);
// insertNode(tail, 3, 5);
// print(tail);
/*
insertNode(tail, 5, 7);
print(tail);
insertNode(tail, 7, 9);
print(tail);
insertNode(tail, 5, 6);
print(tail);
insertNode(tail, 9, 10);
print(tail);
insertNode(tail, 3, 4);
print(tail);
deleteNode(tail, 5);
print(tail);
*/
if(isCircularList(tail)) {
else{
return 0;
class Node {
public:
int data;
Node* prev;
Node* next;
//constructor
Node(int d ) {
this-> data = d;
this->prev = NULL;
this->next = NULL;
}
~Node() {
if(next != NULL) {
delete next;
next = NULL;
cout << "memory free for node with data "<< val << endl;
};
while(temp != NULL) {
int len = 0;
while(temp != NULL) {
len++;
return len;
}
void insertAtHead(Node* &tail, Node* &head, int d) {
//empty list
if(head == NULL) {
head = temp;
tail = temp;
else{
head = temp;
if(tail == NULL) {
tail = temp;
head = temp;
else{
tail = temp;
void insertAtPosi on(Node* & tail, Node* &head, int posi on, int d) {
//insert at Start
if(posi on == 1) {
insertAtHead(tail,head, d);
return;
int cnt = 1;
temp = temp->next;
cnt++;
insertAtTail(tail,head,d);
return ;
if(posi on == 1) {
delete temp;
else
int cnt = 1;
prev = curr;
cnt++;
delete curr;
int main() {
print(head);
//cout << getLength(head) << endl;
insertAtHead(tail,head, 11);
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
insertAtHead(tail,head, 13);
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
insertAtHead(tail,head, 8);
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
insertAtTail(tail,head, 25);
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
deleteNode(7, head);
print(head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
return 0;
#include<map>
class Node {
public:
int data;
Node* next;
//constructor
Node(int data) {
//destructor
~Node() {
//memory free
if(this->next != NULL) {
delete next;
this->next = NULL;
cout << " memory is free for node with data " << value << endl;
};
head = temp;
tail = temp;
if(head == NULL) {
return ;
while(temp != NULL ) {
void insertAtPosi on(Node* &tail, Node* & head, int posi on, int d) {
//insert at Start
if(posi on == 1) {
insertAtHead(head, d);
return;
int cnt = 1;
temp = temp->next;
cnt++;
insertAtTail(tail,d);
return ;
}
void deleteNode(int posi on, Node* & head) {
if(posi on == 1) {
delete temp;
else
int cnt = 1;
prev = curr;
cnt++;
delete curr;
//empty list
if(head == NULL) {
return true;
}
Node* temp = head -> next;
if(temp == head ) {
return true;
return false;
if(head == NULL)
return false;
while(temp !=NULL) {
//cycle is present
if(visited[temp] == true) {
return true;
visited[temp] = true;
}
return false;
if(head == NULL)
return NULL;
if(fast != NULL) {
if(slow == fast) {
cout << "present at " << slow -> data << endl;
return slow;
return NULL;
if(head == NULL)
return NULL;
Node* intersec on = floydDetectLoop(head);
return slow;
return;
int main() {
//print(head);
insertAtTail(tail, 12);
//print(head);
insertAtTail(tail, 15);
//print(head);
//print(head);
//cout << "head " << head -> data << endl;
//cout << "tail " << tail -> data << endl;
//deleteNode(4, head);
cout << "head " << head -> data << endl;
cout << "tail " << tail -> data << endl;
//print(head);
if(floydDetectLoop(head) != NULL) {
else
cout << "loop starts at " << loop -> data << endl;
removeLoop(head);
print(head);
/*
print(head);
if(isCircularList(head)) {
else{
*/
return 0;
LINKED LIST
#include<iostream>
class Node {
public:
int data;
Node* next;
//constructor
Node(int data) {
//destructor
~Node() {
//memory free
if(this->next != NULL) {
delete next;
this->next = NULL;
cout << " memory is free for node with data " << value << endl;
};
head = temp;
tail = temp;
void insertAtPosi on(Node* &tail, Node* & head, int posi on, int d) {
//insert at Start
if(posi on == 1) {
insertAtHead(head, d);
return;
int cnt = 1;
temp = temp->next;
cnt++;
insertAtTail(tail,d);
return ;
if(posi on == 1) {
delete temp;
else
int cnt = 1;
prev = curr;
cnt++;
delete curr;
int main() {
return 0;
REVERSE IN K GROUP
/****************************************************************
class Node
public:
int data;
Node *next;
Node(int data)
this->data = data;
this->next = NULL;
};
*****************************************************************/
//base call
if(head == NULL) {
return NULL;
}
//step1: reverse first k nodes
int count= 0;
prev = curr;
curr = next;
count++;
if(next != NULL) {
return prev;
LOOP
/*************************************************
class Node {
public :
int data;
Node *next;
Node(int data) {
};
*************************************************/
if(head == NULL)
return NULL;
if(fast != NULL) {
if(slow == fast) {
return slow;
}
return NULL;
if(head == NULL)
return NULL;
if(intersec on == NULL)
return NULL;
return slow;
return NULL;
Node* startOfLoop = getStar ngNode(head);
if(startOfLoop == NULL)
return head;
return head;
DUPLICATES
/************************************************************
class Node
public:
int data;
Node* next;
Node(int data)
this->data = data;
this->next = NULL;
};
************************************************************/
//empty List
if(head == NULL)
return NULL;
while(curr != NULL) {
if( (curr -> next != NULL) && curr -> data == curr -> next -> data) {
delete(nodeToDelete);
return head;
MERGELIST
/************************************************************
class Node {
public:
T data;
Node* next;
Node(T data) {
next = NULL;
this->data = data;
~Node() {
if (next != NULL) {
delete next;
};
************************************************************/
curr1 = curr2;
curr2 = next2;
else {
if(first == NULL)
return second;
if(second == NULL)
return first;
solve(first, second);
else
{
solve(second, first);
SORT
/********************************
class Node
public:
int data;
Node *next;
Node(int data)
this->data = data;
this->next = NULL;
};
********************************/
tail = curr;
while(curr != NULL) {
if(value == 0) {
insertAtTail(zeroTail, curr);
else if(value == 1) {
insertAtTail(oneTail, curr);
else if(value == 2) {
insertAtTail(twoTail, curr);
//merge 3 sublist
}
else {
//setup head
delete zeroHead;
delete oneHead;
delete twoHead;
return head;
PALINDROME
class Solu on{
private:
}
return slow;
while(curr != NULL) {
prev = curr;
curr = next;
return prev;
public:
return true;
while(head2 != NULL) {
if(head2->data != head1->data) {
return 0;
return true;
};
ADD
class Solu on
private:
prev = curr;
curr = next;
return prev;
//empty list
if(head == NULL) {
head = temp;
tail = temp;
return;
else
tail = temp;
int carry = 0;
int val1 = 0;
if(first != NULL)
int val2 = 0;
if(second !=NULL)
carry = sum/10;
if(first != NULL)
if(second != NULL)
return ansHead;
public:
{
//step 1 - reverse input LL
first = reverse(first);
second = reverse(second);
//step2 - add 2 LL
//step 3
ans = reverse(ans);
return ans;
};
CLONE
class Solu on
private:
if(head == NULL) {
head = newNode;
tail = newNode;
else
tail = newNode;
while(head != NULL) {
cout << " head data: " << head->data <<" ";
cout << " head random data" << head -> arb ->data;
else
public:
while(temp != NULL) {
originalNode = next;
cloneNode = next;
originalNode = head;
cloneNode = cloneHead;
else
}
cloneNode = cloneNode -> next;
original->next =
copy->next = copy->next?copy->next->next:copy->next;
original = original->next;
copy = copy->next;
return cloneHead;
};
MERGESORT
class Solu on
private:
if(head == NULL) {
head = newNode;
tail = newNode;
else
tail = newNode;
while(head != NULL) {
while(head != NULL) {
cout << " head data: " << head->data <<" ";
cout << " head random data" << head -> arb ->data;
}
else
public:
while(temp != NULL) {
originalNode = next;
cloneNode = next;
originalNode = head;
cloneNode = cloneHead;
else
original->next =
copy->next = copy->next?copy->next->next:copy->next;
original = original->next;
copy = copy->next;
return cloneHead;
};
SIR NOTES