C++



Swaping Consective values using link list

#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;
};
void dis(){
cout<<"                     Built & Designed by Arslan Malik Aareez"<<endl;
cout<<"                          www.CWorldbyAS.blogspot.com"<<endl;
cout<<"                     Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<"                     Twitter  : www.twitter.com/itsaareez"<<endl;

}
void st(){
for( int i=0;i<=79;i++){
cout<<'*';
}
}
int main(){
int menu;
int sel;

st();
dis();
st();


struct Node *p = new Node;
struct Node *head = new Node;
int hold;



ask:
cout<<"Select the operation : \n\n1. Insertion of new value\n2. Swap\n3. Print\n4. Exit";
cin>>menu;
system("cls");
switch(menu){
case 1:
cout<<"Enter Value in List : ";
cin>>p->value;
p->next = NULL;
head = p;
system("cls");
cout<<"Do you want to enter another value in List ? \n\n1. Yes\n2. No";
cin>>sel;

while(sel!=2){
system("cls");
struct Node *q = new Node;
q->next=NULL;
cout<<"Enter Value in List : ";
cin>>q->value;
system("cls");
p->next = q;
p= p->next;
cout<<"Do you want to enter another value in List ? \n\n1. Yes\n2. No";
cin>>sel;

}
system("cls");
goto ask;
break;
case 2:

p = head;

while (p!=NULL && p->next!=NULL){
hold = p->value;
p->value = p->next->value;
p->next->value = hold;
p = p->next->next;
}
goto ask;

break;
case 3:
    system("cls");
p=head;
cout<<"Your series after swaping"<<endl;
while(p->next!=NULL){
cout<<" "<<p->value;
p = p->next;
}
cout<<" "<<p->value<<"\n\n";
system("pause");
system("cls");
goto ask;
break;
case 4:
return 0;
break;
}


}

Selection Sort

#include <iostream>
using namespace std;


void in(int a[] , int n){

int p;
int temp;

for(int i=0;i<n;i++){
temp = a[i];
p = i;
while(p>0 && temp< a[p-1]){
a[p] = a[p-1];
p--;
}
a[p]=temp;
}
for(int i=0;i<n;i++){
cout<<" "<<a[i];
}


}
void swap(int *a , int *b){
int hold = *a;
*a = *b;
*b = hold;
}

void selection_sort(int a[], int n){
int min,j,i,hold;
for(i=0;i<n-1;i++){
min = i;
for(j=i+1;j<n;j++){
if(a[j]<a[min]){
min = j;
}
}

swap(&a[i],&a[min]);


}
for(int k=0;k<n;k++){
cout<<" "<<a[k];
}



}

void selection_sot(int a[],int s){
int i,j,temp;
for(i=0;i<s;i++){
      for(j=i+1;j<s;j++){
           if(a[i]>a[j]){
              swap(&a[i],&a[j]);
            }
      }
  }
for(int k=0;k<s;k++){
cout<<" "<<a[k];
}
}


int main(){
int a[]= {12,0,54,-22,12,76,-33,4,87,98,45};
int n = sizeof(a)/sizeof(int);
selection_sot(a,n);

}

Quick Sort


#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places the pivot element at its
   correct position in sorted array, and places all smaller (smaller than pivot)
   to left of pivot and all greater elements to right of pivot */
int partition (int arr[], int l, int h)
{   int j;
    int x = arr[h];    // pivot
  int i = l - 1;  // Index of smaller element

    for (j =l; j <= h- 1; j++)
    {
        // If current element is smaller than or equal to pivot
        if (arr[j] <= x)
        {
            i++;    // increment index of smaller element
            swap(&arr[i], &arr[j]);  // Swap current element with index
        }
    }
    swap(&arr[i+1], &arr[h]);
    return (i+1);
}

/* arr[] --> Array to be sorted, l  --> Starting index, h  --> Ending index */
void quickSort(int arr[], int l, int h)
{
    if (l < h)
    {
        int p = partition(arr, l, h); /* Partitioning index */
        quickSort(arr, l, p - 1);
        quickSort(arr, p + 1, h);
    }
}

/* Function to print an array */
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// Driver program to test above functions
int main()
{
    int arr[] = {1, 7, 4, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quickSort(arr, 0, n-1);
    printf("Sorted array: \n");
    printArray(arr, n);
    return 0;

}

Queue

#include <iostream>
#define size 50
using namespace std;
int main(){
int arr[size];
int r = -1;
int f = -1;
int count = 0;
int selection;
if (count>=size){
             cout<<"Queue is overflow."<<endl;
             system("pause");
             return 0;
             }
again:
cout<<"\nSelect one option below: "<<endl;
cout<<"\n1.Insert\n2.Delete\n3.Print\n4.Exit"<<endl;
cin>>selection;
switch(selection){
case 1:
while(count<size ){
cout<<"\n\nEnter Number : "<<endl;
cin>>r;
arr[count] = r;          
count++;
if (f=-1){
          f=0;
          }
          goto again;
}
system("pause");
break;
case 2:
     if (f == -1 || r == -1){
           cout<<"Queue is empty"<<endl;
           }
     if (count==f){
         arr[f] = 0;
         count = -1;
         f = -1;        
         goto again;      
                   }
         arr[f] = 0;
         f++;  
     system("pause");
     goto again;
     break;
case 3:
for (int i = 0 ; i<count ; i++){
    if (arr[i]==0){
cout<<" "<<endl;
                   }
                   else {
    cout<<"At position "<<i<<" value is "<<arr[i]<<endl;
}
    }
    system("pause");
    goto again;
    break;
case 4:
     return 0;
}
}

Printing Odd at start and Even at end in Link List using concatenation

#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;

};
void dis(){
cout<<"                     Built & Designed by Arslan Malik Aareez"<<endl;
cout<<"                          www.CWorldbyAS.blogspot.com"<<endl;
cout<<"                     Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<"                     Twitter  : www.twitter.com/itsaareez"<<endl;

}
void st(){
for( int i=0;i<=79;i++){
cout<<'*';
}
}
void push(Node *a ,int value){

}
int main(){
int menu;
int sel;

st();
dis();
st();


struct Node *p1 = new Node;
struct Node *head1 = new Node;
struct Node *p2 = new Node;
struct Node *head2 = new Node;




int val;
p1->next = NULL;
p1->value= 0;
p2->next=NULL;
p2->value = 0;
ask:
cout<<"Select the operation : \n\n1. Insert\n2. Arrange\n3. Print\n4. Exit"<<endl;
cin>>menu;
system("cls");
switch(menu){
case 1:
cout<<"Enter Value in List : ";
cin>>val;
if (val%2==0){
p2->value = val;
p2->next = NULL;
}
else {
p1->value = val;
p1->next = NULL;

}
head1 = p1;

head2 = p2;
system("cls");
cout<<"Do you want to enter another value in List ? \n\n1. Yes\n2. No";
cin>>sel;

while(sel!=2){
system("cls");
cout<<"Enter Value in List : ";
cin>>val;
if (val%2==0){
struct Node *q2 = new Node;
q2->value = val;
q2->next = NULL;
p2->next = q2;
   p2 = q2;

}
else{
struct Node *q1 = new Node;
q1->value = val;
q1->next=NULL;
p1->next = q1;
   p1 = q1;
}
system("cls");

cout<<"Do you want to enter another value in List ? \n\n1. Yes\n2. No";
cin>>sel;

}
system("cls");
goto ask;
break;
case 2:
p1 = head1;
if (head2->value==0){
p2 = head2->next;
}
else{
p2 = head2;
}
while(p1->next!=NULL){
p1 = p1->next;
}

p1->next = p2;

goto ask;

break;
case 3:
    system("cls");

if (p1->next==NULL && p1->value==0){
cout<<"Your have not entered any value in list. "<<endl;
system("pause");
system("cls");
goto ask;
}
p1=head1;
cout<<"Your series after arrangement is :"<<endl;
while(p1->next!=NULL){
cout<<" "<<p1->value;
p1 = p1->next;
}
cout<<" "<<p1->value<<"\n\n";

system("pause");
system("cls");
goto ask;
break;
case 4:
return 0;
break;
}


}

Multiplication of Matrix using arrays

#include <iostream>
using namespace std;
int main(){
int c[2][2]={{0,0},{0,0}};
int a[2][2]={{1,2},{1,2}};
int b[2][2]={{2,1},{1,1}};
    int i,j,k;
for ( j=0;j<2;j++){

for ( i=0;i<2;i++)
  for( k=0;k<2;k++){
c[i][j] = c[i][j]+a[i][k]*b[k][j];
}
}
for(int u=0;u<2;u++){
for(int w=0;w<2;w++){
cout<<c[u][w]<<"    ";
}
cout<<endl;
}


}

Multiplication of Diagonal Elements of matrix

#include <iostream>
using namespace std;



int main(){
int val;
int a[2][2] = {{1,2},{1,3}};
val = a[0][0];

for (int j = 1 ; j<2 ; j++){

val *=a[j][j];
}
cout<<val;


}


Merge Sort

#include <iostream>
using namespace std;
void merge (int *a, int n, int m) {
int i=0, j=m, k;
int *x = new int;
for ( k = 0; k < n; k++) {
x[k] = j == n ? a[i++] : i == m ? a[j++] : a[j] < a[i] ? a[j++] : a[i++];
}
for (i = 0; i < n; i++) {
a[i] = x[i];
}
delete(x);
}

void merge_sort (int *a, int n) {
if (n < 2)
return;
int m = n / 2;
merge_sort(a, m);
merge_sort(a + m, n - m);
merge(a, n, m);
}

int main () {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof (int);
int i;
for (i = 0; i < n; i++){
cout<<" "<<a[i];
}
merge_sort(a, n);
cout<<"\n";
for (i = 0; i < n; i++)
{
cout<<" "<<a[i];
}
return 0;

}

LinkList Creation

#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
int main(){
 struct Node *head;
 struct Node *temp;
 struct Node *n;

 n = new Node;
 n->data = 1;
 head = n;
 temp = n;
 n = new Node;
 n->data = 2;
 temp->next = n;
 temp = temp->next;
 n = new Node;
 n->data = 3;
 temp->next = n;
 temp = temp->next;
 n->next = NULL;

}

Linear Search

#include <iostream>
#define SIZE 10
using namespace std;
int main(){

int a[SIZE] = {1,20,34,24,5,11,7,8,9,10};
int value = 24;
for (int i=0; i <SIZE ; i++){
if (a[i]==value){
cout<<"Value found is "<<value;
}
}



}


Intersection using Link List



Insertion Sort

#include <iostream>
using namespace std;

int main(){
int temp;
int p;
int a[6] = {0,2,1,5,3,7};

 for(int i = 0 ; i <6 ; i++){
 temp = a[i];
 p = i;
 while(p>0 && temp<a[p-1]){
  a[p] = a[p-1];
  p--;
 }
 a[p] = temp;
 }
 for (int i = 0;i<6 ; i++){
  cout<<" "<<a[i];
 }

}


Insertion & Deletion in Array

#include <iostream>
using namespace std;
int size=0;
void insertion(int *a , int val){

*a = val;
size++;
}
void del(int a[], int loc){

for(int i=loc-1;i<size;i++){
a[i] = a[i+1];
}
size--;

}
int main(){
int a[size];

insertion(&a[size],1);
insertion(&a[size],4);
insertion(&a[size],9);
insertion(&a[size],3);
insertion(&a[size],8);
cout<<"\n After Insertion";
for(int i = 0 ;i<size ; i++){
cout<<" "<<a[i];
}
cout<<"\n After Deletion";
del(a,3);
for(int i = 0 ;i<size ; i++){
cout<<" "<<a[i];
}

}


Height of Minimal Tree

To Buy this Code Click Here to Contact on Facebook
OR Send an Email to itsaareez1@yahoo.com


HeapSort+CountofNodes+BST+Node Search


To Buy this Code Click Here to Contact on Facebook
OR Send an Email to itsaareez1@yahoo.com


Finding Max using array


#include <iostream>
using namespace std;


int main(){
int a[] = {3,5,1,2,766,1,3,87};
int i;

int max = a[1];

for (i=0;i<8;i++){
if(max<a[i]){
max=a[i];
}
}
cout<<"Max is "<<max;
}

Bubble Sort using link List

#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;
};
void dis(){
cout<<"                     Built & Designed by Arslan Malik Aareez"<<endl;
cout<<"                          www.CWorldbyAS.blogspot.com"<<endl;
cout<<"                     Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<"                     Twitter  : www.twitter.com/itsaareez"<<endl;

}
void st(){
for( int i=0;i<=79;i++){
cout<<'*';
}
}
int main(){
int menu;
int sel;

st();
dis();
st();


struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
struct Node *h = new Node;


ask:
cout<<"Select the operation : \n\n1. Insertion of new value\n2. Bubble Sort\n3. Print\n4. Exit";
cin>>menu;
system("cls");
switch(menu){
case 1:
cout<<"Enter Value in List : ";
cin>>p->value;
p->next = NULL;
head = p;
system("cls");
cout<<"Do you want to enter another value in List ? \n\n1. Yes\n2. No";
cin>>sel;

while(sel!=2){
system("cls");
struct Node *q = new Node;
q->next=NULL;
cout<<"Enter Value in List : ";
cin>>q->value;
system("cls");
p->next = q;
p= p->next;
cout<<"Do you want to enter another value in List ? \n\n1. Yes\n2. No";
cin>>sel;

}
system("cls");
goto ask;
break;
case 2:
system("cls");
r=head;
p=head->next;

while (r->next!=NULL){
while(p->next!=NULL){
if (r->value>p->value){

h->value = r->value;
r->value = p->value;
p->value = h->value;
}
p= p->next;
}

if (r->value>p->value){

h->value = r->value;
r->value = p->value;
p->value = h->value;
}

r=r->next;
p=r->next;
}




goto ask;

break;
case 3:
    system("cls");
p=head;
cout<<"Your series after acending order"<<endl;
while(p->next!=NULL){
cout<<" "<<p->value;
p = p->next;
}
cout<<" "<<p->value<<"\n\n";
system("pause");
system("cls");
goto ask;
break;
case 4:
return 0;
break;
}


}


BST Creation & Traversal

#include <iostream>
using namespace std;
struct Node {
Node *right;
int value;
Node *left;
};


struct Node *head = NULL;
void insert (Node *root , Node *temp)
{

if (root==NULL){
cout<<"Root is created";
root = temp;
}
else
{
if (root->value < temp->value){
if (root->right==NULL){
root->right=temp;
}
else{
insert(root->right,temp);
}
}
if (root->value > temp->value){
if (root->left==NULL){
root->left = temp;
}
else{
insert(root->left,temp);
}
}
}

}

void preorder(Node *root){

if (root!=NULL){

cout<<" "<<root->value;
preorder(root->left);
preorder(root->right);
}


}
void inorder(Node *root){
if (root!=NULL){
inorder(root->left);
cout<<" "<<root->value;
inorder(root->right);
}

}
void postorder(Node *root){
if (root!=NULL){
postorder(root->left);
postorder(root->right);
cout<<" "<<root->value;

}
}


int main(){
int i;
    int menu;
struct Node *p = new Node;
struct Node *r = new Node;
ask:
    cout<<"Select any option : \n1.Insert\n2.Print\n3.Exit"<<endl;
    cin>>menu;
    system("cls");
    switch(menu){
    case 1:
cout<<"Enter Value : ";
cin>>p->value;
p->right = p->left = NULL;
insert (head,p);
head = p;
system("cls");
cout<<"Do you want to insert any other value? \n1. Yes\n2. No";
cin>>i;
system("cls");
    while(i!=2){
struct Node *q = new Node;
cout<<"Enter Value : ";
cin>>q->value;
q->right = q->left = NULL;
insert (p,q);
system("cls");
cout<<"Do you want to insert any other value? \n1. Yes\n2. No";
    cin>>i;
    system("cls");
}
system("cls");
goto ask;

break;
   
    case 2:
       int sel;
       cout<<"1. Pre-Order\n2. In-Order\n3. Post-Order";
       cin>>sel;
       system("cls");
       switch(sel){
        case 1:
        r = head;
        if (r==NULL){
        cout<<"Tree is empty.\n\n";
        }
       else
        {
          cout<<"Pre-Order List is"<<endl;
        preorder(r);
        }
        break;
        case 2:
        r = head;
        if (r==NULL){
        cout<<"Tree is empty.\n\n";
        }
        else{
cout<<"In-Order List is"<<endl;
        inorder(r);
        }
        break;
        case 3:
        r = head;
        if (r==NULL){
        cout<<"Tree is empty.\n\n";
        }
        else{
        cout<<"Post-Order List is"<<endl;
        postorder(r);
        }
        break;
       }
       cout<<"\n\n";
       system("pause");
       system("cls");
    goto ask;
    break;
   
case 3:
return 0;
break;
}



}


Binary Search

#include <iostream>
#define SIZE 40
using namespace std;
int binary_search(int a[] , int key , int min, int max){
while(min<=max){
int mid = (max+min)/2;
if(a[mid]==key){
return mid;
}
else if(a[mid]<key){
min = mid+1;
}
else {
max = mid-1;
}
}
return 0;
}
int main(){
int a[SIZE] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40};
int low = 0;
int high = 39;
int value = 12;
    cout<<"Value found is "<<a[binary_search(a,value,low,high)];

}



Queues Implementation using Single Link List

#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;
};
int main(){
int menu;
int sel;
int count=0;
struct Node *p = new Node;
struct Node *head = new Node;

for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<"                     Built & Designed by Arslan Malik Aareez"<<endl;
cout<<"                          www.CWorldbyAS.blogspot.com"<<endl;
cout<<"                     Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<"                     Twitter  : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}

    ask:
   
cout<<"1. Insert\n2. Delete\n3. Print\n4. Exit"<<endl;
    cin>>menu;
    switch(menu){
    case 1:
    count++;
    cout<<"Enter Value to insert : ";
    cin>>p->value;
    p->next = NULL;
    head = p;
    system("cls");
    cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
    cin>>sel;
            system("cls");
    while (sel!=2){
  struct Node *q = new Node;
count++;
    cout<<"Enter Value to insert : ";
    cin>>q->value;
       q->next = NULL;
    p->next = q;
    p = p->next;
    system("cls");
    cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
    cin>>sel;
    system("cls");
    }
    goto ask;
    break;
    case 2:
    count--;
    p = head;
    head = p->next;
    p->value = 0;
    p->next = NULL;
    system("cls");
    goto ask;
    break;
   
    case 3:
    system("cls");
    p = head;
    if (count==0){
       system("cls");

cout<<"Queue is empty.";
    }
    else{
cout<<"Your queue has the following values"<<endl;
   
    while (p->next!=NULL){
    cout<<" "<<p->value;
    p = p->next;
    }
    cout<<" "<<p->value;
    }
    cout<<"\n\n";
system("pause");
    system("cls");
goto ask;
break;
   
    case 4:
    return 0;
    break;
    }
 

}

Stack Implementation using Single Link List

#include <iostream>
using namespace std;
struct Node{
int value;
Node *next;

};

int main(){

for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<"                     Built & Designed by Arslan Malik Aareez"<<endl;
cout<<"                          www.CWorldbyAS.blogspot.com"<<endl;
cout<<"                     Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<"                     Twitter  : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}

int menu;
int sel;
 
struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
head->next = NULL;
head->value=0;
    ask:
cout<<"Enter Your Choice\n"<<endl;
    cout<<"1. Push"<<endl;
    cout<<"2. Pop"<<endl;
    cout<<"3. Print"<<endl;
    cout<<"4. Exit"<<endl;
    cin>>menu;
    system("cls");
switch (menu){
case 1:
cout<<"Enter Value to Push : ";
cin>>p->value;
p->next = NULL;
system("cls");
head = p;
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;

cin>>sel;
system("cls");
    while (sel!=2){
     struct Node *q = new Node;
      cout<<"\nEnter Value to Push : ";    
cin>>q->value;
q->next=NULL;
p->next = q;
     p = p->next;
     system("cls");
        cout<<"Do you want to push another value?"<<endl;
        cout<<"1. Yes"<<endl;
        cout<<"2. No"<<endl;
        cin>>sel;
        system("cls");

    }
    goto ask;

break;
case 2:
p = head;
if (p->next==NULL && p->value == 0){
cout<<"\nStack is Empty\n\n";
}
while (p->next!=NULL){
r = p;
p = p->next;
}
r->next = NULL;
p->value = 0;

goto ask;

break;
case 3:
system("cls");
p= head;

cout<<"Stack contains the following values "<<endl;

while (p->next!=NULL){
if (p->value==0)
{
cout<<" ";
}
else
{
cout<<" "<<p->value;
}

p=p->next;

}
cout<<" "<<p->value;
cout<<"\n";
goto ask;

break;

case 4:
return 0;
break;
}


}

No comments:

Post a Comment