C++ and Data Structures Shortned Programs
C++ and Data Structures Shortned Programs
#include <iostream>
class test
{
public:
void first()
{
cout << "From the CLASS to check if it works." << endl;
}
};
void main()
{
test t;
t.first();
}
//Array of objects
#include <iostream>
class employee
{
private:
int id;
char name[30];
public:
void getdata();
void putdata();
};
void employee::getdata()
{
cout << "Enter ID : ";
cin >> id;
cout << "Enter Name : ";
cin >> name;
}
void employee::putdata()
{
cout << id << " ";
cout << name << " ";
cout << endl;
}
int main()
{
employee emp;
emp.getdata();
emp.putdata();
return 0;
}
//CONSTRUCTOR
#include <iostream>
class testconstr
{
public:
testconstr()
{
cout << "Running from the test constructor" << endl;
}
};
void main()
{
testconstr c;
}
//DESTRUCTOR
#include<iostream>
class test
{
public:
test()
{
cout << "constructor executed." << endl;
}
~test()
{
cout << "Distructor Executed." << endl;
}
};
void main()
{
test t;
}
//FUNCTION OVERLOADING
#include <iostream>
#include<iostream>
class temp
{
private:
int count;
public:
temp():count(5){}
void operator++()
{
count = count + 1;
}
void display()
{
cout << "count " << count<<endl;
}
};
int main()
{
temp t;
++t;
t.display();
return 0;
}
//single inheritance
#include<iostream>
#include<string>
class organization
{
private:
string name;
string sector;
public:
organization()
{
name = "Junaid";
sector = "IT";
}
void showdata()
{
cout << "Name is :" << name<<endl;
cout << "Sector is :" << sector<<endl;
}
};
class employee:public organization
{
private:
string name;
int ID;
public:
employee()
{
name = "JUNAID";
ID = 3019;
}
void showname()
{
cout << "Name is :" << name << endl;
cout << "ID id :" << ID << endl;
}
};
int main()
{
employee obj;
obj.organization::showdata();
cout << endl;
obj.employee::showname();
return 0;
}
//MULTIPLE INHERITANCE
#include<iostream>
class A
{
public:
A()
{
cout << "From class A" << endl;
}
};
class B
{
public:
B()
{
cout << "From class B" << endl;
}
};
class C:public B, public A
{
public:
C()
{
cout << "From class c" << endl;
}
};
void main()
{
C c;
}
//Hierarchical Inheritance
#include<iostream>
class A
{
public:
void show_A()
{
cout << "Class A" << endl;
}
};
class B:public A
{
public:
void show_B()
{
cout << "Class B" << endl;
}
};
class C :public A
{
public:
void show_C()
{
cout << "Class C" << endl;
}
};
int main()
{
B b;
cout << "Calling from class B" << endl;
b.show_B();
b.show_A();
C c;
cout << "Calling from class C" << endl;
c.show_C();
c.show_A();
return 0;
}
//I/O STREAM
#include<iostream>
int main()
{
char sample[] = "Welcome ";
cout << sample << "- To the I/O stream " << endl;
return 0;
}
//FILE STREAM
#include<iostream>
#include<fstream>
int main()
{
char data[100];
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file :" << endl;
cout << "Enter your Name ";
cin.getline(data, 100);
cout << "Enter your age :";
cin >> data;
cin.ignore();
outfile << data << endl;
outfile.close();
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
cout << data << endl;
infile.close();
return 0;
}
//EXCEPTION HANDLING
#include<iostream>
int main()
{
int x = -1;
cout << "Before Try" << endl;
try {
cout << "Inside try" << endl;
if (x < 0)
{
throw x;
cout << "After trow (Never executed)" << endl;
}
}
catch (int x) {
cout << "Execption caught" << endl;
}
cout << "After catch (Will be executed)" << endl;
return 0;
}
//STACK using array
#include<iostream>
#define max 5
using namespace std;
int top = 0;
class stack
{
int item[max];
public:
void push(int);
void pop();
void display();
};
void stack::push(int x)
{
if (top == max)
{
cout << "\n STACK IS FULL " << endl;
}
else
{
item[top] = x;
top = top + 1;
}
}
void stack::pop()
{
if (top == 0)
{
cout << "\n STACK IS EMPTY " << endl;
}
else
{
cout << "ELEMENT POPED " << endl;
cout << "\t" << item[--top] << endl;
}
}
void stack::display()
{
int i;
for(i=top-1;i>=0;--i)
cout << "\t" << item[i] << endl;
}
void main()
{
stack t;
int x, ch;
cout << "STACK USING ARRAY" << endl;
cout << "1.push" << endl;
cout << "2.pop" << endl;
cout << "3.delete" << endl;
cout << "4.exit" << endl;
do
{
cout << "Enter your choice :";
cin >> ch;
switch (ch)
{
case 1:
cout << "Enter the Value :";
cin >> x;
t.push(x);
break;
case 2:
t.pop();
break;
case 3:
cout << "STACK using Array" << endl;
t.display();
break;
case 4:
cout << "EXIT THE PROGRAM" << endl;
break;
default:
break;
}
}while (ch != 4);
}
//QUEUE USING ARRAY
#include<iostream>
#define max 3
using namespace std;
class que
{
int i, a[max], x;
public:
void insert();
void deleted();
void display();
};
void que::insert()
{
cout << "Enter the Element : ";
cin >> x;
if (top >= max)
{
cout << "QUEUE IS FULL" << endl;
}
else
{
a[top] = x;
top++;
}
}
void que::deleted()
{
if (top <= 0)
{
cout << "QUEUE IS EMPTY" << endl;
}
else
{
cout << a[front] << " DELETED " << endl;
for (int i = 1; i <= top; i++)
{
a[i - 1] = a[i];
top--;
}
}
}
void que::display()
{
for (int i = front; i < top; i++)
{
cout << " " << a[i] << endl;
}
}
void main()
{
int ch;
que b;
cout << "\t QUEUE USING ARRAY" << endl;
cout << "\n\t 1.insert";
cout << "\n\t 2.delete";
cout << "\n\t 3.display";
cout << "\n\t 4.exit";
do
{
cout << "\n Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1:
b.insert();
break;
case 2:
b.deleted();
break;
case 3:
b.display();
break;
case 4:
cout << "EXIT THE PROGRAM" << endl;
break;
default:
break;
}
} while (ch != 4);
}
//INFIX TO POSTFIX
#include<iostream>
#include<string>
#include<ctype.h>
#include<process.h>
#include<math.h>
class postfix
{
char lex[50], st[50], pex[50];
int top, p;
public:
postfix(char a[])
{
strcpy_s(lex, a);
top = -1;
p = 0;
}
void convert();
void push(char);
void pop();
};
void postfix::convert()
{
for (int i = 0; i < strlen(lex); i++)
{
if (isalpha(lex[i]))
{
pex[p] = lex[i];
p++;
}
else
if ((lex[i] == '(') || (lex[i] == '+') || (lex[i] == '-') ||
(lex[i] == '/') || (lex[i] == '*') || (lex[i] == '%') || (lex[i] == '^'))
{
push(lex[i]);
}
else if (lex[i] == ')')
{
pop();
}
else
{
cout << "INCORRECT expression \n";
exit(i);
}
}
pex[p] = '\0';
cout << "\n POSTFIX expression:" << pex << endl;
}
void postfix::push(char s)
{
st[++top] = s;
}
void postfix::pop()
{
pex[p] = st[top];
p++;
top = top - 2;
}
void main()
{
char str[30];
cout << "\n INFIX TO POSTFIX EXPRESSION";
cout << "\n Enter the Expression";
cin >> str;
postfix pf(str);
pf.convert();
}
//BINARY TREE
#include<iostream>
struct node
{
int data;
struct node* left, * right;
};
node* newnode(int data)
{
node* temp = new node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printinorder(struct node* node)
{
if (node == NULL)
return;
printinorder(node->left);
cout << node->data << " ";
printinorder(node->right);
}
int main()
{
struct node* root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
cout << "\n Inorder traversel of Binary Tree ";
printinorder(root);
cout << endl;
return 0;
}
//ADDITION OF TWO POLYNOMIALS USING POINTERS
#include<iostream>