Monday, December 23, 2019

STACK

Program to Reverse a String Using Stack Data Structure

#include <stdio.h>
#include <string.h>

#define max 100
int top,stack[max];


void push(char x){

      // Push(Inserting Element in stack) operation
      if(top==max-1){
          printf("stack overflow");
      }  else {
          stack[++top]=x;
      }  

}

void pop(){

    

    // Pop (Removing element from stack)
          
      printf("%c",stack[top--]);
}


main()
{

   char str[]="I Love Programming";

   
   int len = strlen(str), i;

   
   for(i=0; i<len; i++)
        push(str[i]);

    
   for(i=0; i<len; i++)    
      pop();        
}












Friday, November 15, 2019

BINARY TREE

Implementation of Binary Tree

*************
#include<stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;

node *create()
{
node *p;
int x;
printf("Enter data(-1 for no data):");
scanf("%d",&x);

if(x==-1)
return NULL;

p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}

void preorder(node *t) //address of root node is passed in t
{
if(t!=NULL)
{
printf("\n%d",t->data); //visit the root
preorder(t->left); //preorder traversal on left subtree
preorder(t->right); //preorder traversal om right subtree
}
}
int main()

node *root;
root=create();
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
return 0;
}

Binary Search Tree

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
 int data;
 struct node *left;
 struct node *right;
};


void inorder(struct node *root)
{
 if(root)
 {
  inorder(root->left);
  printf("  %d",root->data);
  inorder(root->right);
 }
}

int main()
{
 int n , i;
 struct node *p , *q , *root;
 printf("Enter the number of nodes to be insert: ");
 scanf("%d",&n);

 printf("\nPlease enter the numbers to be insert: ");

 for(i=0;i<i++)
 {
  p = (struct node*)malloc(sizeof(struct node));
  scanf("%d",&p->data);
  p->left = NULL;
  p->right = NULL;
  if(i == 0)
  {
   root = p; // root always point to the root node
  }
  else
  {
   q = root;   // q is used to traverse the tree
   while(1)
   {
    if(p->data > q->data)
    {
     if(q->right == NULL)
      {
      q->right = p;
      break;
      }
     else
      q = q->right;
    }
    else
    {
     if(q->left == NULL)
      {
      q->left = p;
      break;
      }
     else
      q = q->left;
    }
   }

  }

 }

printf("\nBinary Search Tree nodes in Inorder Traversal: ");
inorder(root);
printf("\n");

return 0;
}


























Saturday, November 2, 2019

DATA STRUCTURE

Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Merge Sort
Heap Sort  
[1]  Bubble Sort :        
#include <stdio.h>
int main()
{
    int a[20],n,i,j,temp;
    printf("\nHow many numbers are there : ");
    scanf("%d",&n);
    
    printf("\nEnter the numbers ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
   for(i=0; i<n-1; i++)
    {
        for(j=0; j< n-1-i; j++)
        {
            if(a[j] > a[j+1])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
    printf("\nNumbers in sorted order are : ");
    for(i=0; i<n; i++)
    {
        printf("\n%d",a[i]);
    }
    return 0;
}

[2] Insertion Sort : 
#include <stdio.h>
/*Program Insertion Sort*/
int main()
{
    int a[20], i, j, n, temp;

    printf("Enter how many numbers are there :");
    scanf("%d",&n);
    
    printf("Enter the numbers : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        temp=a[i];
        for(j=i-1; j>=0; j--)
        {
            if(temp > a[j])
            {
                break;
            }
            else
                a[j+1] = a[j];
        }
        a[j+1] = temp;
    }

    printf("\nSorted numbers are : \n ");
    for(i=0; i<n; i++)
        printf("\n%d",a[i]);

    return 0;

}


[3]Selection Sort :
#include <stdio.h>

int main()
{
    int a[20],n,i,j,temp;
    printf("\nHow many numbers are there : ");
    scanf("%d",&n);

    
    printf("\nEnter the numbers ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(a[i] > a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    printf("\nNumbers in sorted order are : ");
    for(i=0; i<n; i++)
    {
        printf("\n%d",a[i]);
    }
    return 0;
}


[4] Quick Sort :
#include<stdio.h>
void quicksort(int number[25],int first,int last){
   int i, j, pivot, temp;

   if(first<last){
      pivot=first;
      i=first;
      j=last;

      while(i<j){
         while(number[i]<=number[pivot]&&i<last)
            i++;
         while(number[j]>number[pivot])
            j--;
         if(i<j){
            temp=number[i];
            number[i]=number[j];
            number[j]=temp;
         }
      }

      temp=number[pivot];
      number[pivot]=number[j];
      number[j]=temp;
      quicksort(number,first,j-1);
      quicksort(number,j+1,last);

   }
}

int main(){
   int i, count, number[25];

   printf("How many elements are u going to enter?: ");
   scanf("%d",&count);

   printf("Enter %d elements: ", count);
   for(i=0;i<count;i++)
      scanf("%d",&number[i]);

   quicksort(number,0,count-1);

   printf("Order of Sorted elements: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
}



                                                                                                         

Thursday, October 10, 2019

ARRAY (INSERTION,DELETION,REVERSING)

INSERTION

#include<stdio.h>

int main() {
   int arr[30], element, num, i, location;

   printf("\nEnter no of elements :");
   scanf("%d", &num);

   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

   printf("\nEnter the element to be inserted :");
   scanf("%d", &element);

   printf("\nEnter the location");
   scanf("%d", &location);

   //Create space at the specified location
   for (i = num; i >= location; i--) {
      arr[i] = arr[i - 1];
   }

   num++;
   arr[location - 1] = element;

   //Print out the result of insertion
   for (i = 0; i < num; i++)
      printf("n %d", arr[i]);

   return (0);
}
************************************************
[2]DELETION
#include<stdio.h>

int main() {
   int arr[30], num, i, loc;

   printf("\nEnter no of elements :");
   scanf("%d", &num);

   //Read elements in an array
   printf("\nEnter %d elements :", num);
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

   //Read the location
   printf("\n location of the element to be deleted :");
   scanf("%d", &loc);

   /* loop for the deletion  */
   while (loc < num) {
      arr[loc - 1] = arr[loc];
      loc++;
   }
   num--;  // No of elements reduced by 1

   //Print Array
   for (i = 0; i < num; i++)
      printf("\n %d", arr[i]);

   return (0);

}

**************************************
REVERSING
#include<stdio.h>

int main() {
   int arr[30], i, j, num, temp;

   printf("\nEnter no of elements : ");
   scanf("%d", &num);

   //Read elements in an array
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

   j = i - 1;   // j will Point to last Element
   i = 0;       // i will be pointing to first element

   while (i < j) {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;             // increment i
      j--;          // decrement j
   }

   //Print out the Result of Insertion
   printf("\nResult after reversal : ");
   for (i = 0; i < num; i++) {
      printf("%d \t", arr[i]);
   }

   return (0);
}
****************************
DELETION OF DUPLICATE
#include<stdio.h>

int main() {
   int arr[20], i, j, k, size;

   printf("\nEnter array size : ");
   scanf("%d", &size);

   printf("\nAccept Numbers : ");
   for (i = 0; i < size; i++)
      scanf("%d", &arr[i]);

   printf("\nArray with Unique list  : ");
   for (i = 0; i < size; i++) {
      for (j = i + 1; j < size;) {
         if (arr[j] == arr[i]) {
            for (k = j; k < size; k++) {
               arr[k] = arr[k + 1];
            }
            size--;
         } else
            j++;
      }
   }

   for (i = 0; i < size; i++) {
      printf("%d ", arr[i]);
   }

   return (0);

}*********************

Saturday, October 5, 2019

Reversing Array using pointer

Reversing Array using pointer***********

#include <stdio.h>
#include <stdlib.h>
int main()

{

int *a,n,i,j,temp;

printf("Enter size of array:");
scanf("%d",&n);

a=calloc(sizeof(int),n);
printf("Enter %d Elements:",n);
for(i=0;i<n;i++)

{

scanf("%d\t",a+i);

}



for(i=0,j=n-1;i<j;i++,j--)

{

temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}

printf("After reversing the array:\n");
for(i=0;i<n;i++)

{

printf("%d",*(a+i));

}


return 0;

}

Sorting Using Pointer



Sorting Using Pointer
**************************************
#include <stdio.h>
void sort(int n, int* ptr)
{
    int i,j,t;
    
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
         if(*(ptr+j) < *(ptr+i))
         {
             t            =  *(ptr+i);
             *(ptr+i) =  *(ptr+j);
             *(ptr+j) =    t;
             
         }
        }
    }

for(i=0;i<n;i++)

printf("%d\t",*(ptr+i));
}

int main()
{
    int n=5;
    int arr[]={5,44,11,6,7};
    sort(n,arr);
    return 0;
}

Thursday, October 3, 2019

Polymorphism

Run time Polymorphism(Function Overriding)
****************************************************
Function overriding means when the child class contains the method which is already present in the parent class.
#include<iostream>
using namespace std;
class A {
    public:
    void disp(){
    cout<<"super class function"<<endl;
    }
};
class B : public A
{
    public:
    void disp(){
        cout<<"Sub class function"<<endl;
        
    }
};

int main()
{
    A obj;
    obj.disp();
    
    B obj2;
    obj2.disp();
    
    return 0;
}

Output:
Super class function
Sub class function

::::::::Compile time Polymorphism:::::::::

The polymorphism which is implemented at the compile time is known as compile-time polymorphism. Method overloading is an example of compile-time polymorphism.

Method overloading: Method overloading is a technique which allows you to have more than one function with the same function name but with different functionality.

#include <iostream>
using namespace std;
class Add{
    public:
    int sum(int num1,int num2)
    {    
        return num1+num2;
    }
    int sum(int num1,int num2,int num3)
    {
        return num1+num2+num3;
    }
};

int main()
{
    Add obj;
    cout<<"output:"<<obj.sum(10,20)<<endl;
    cout<<"output:"<<obj.sum(10,20,30)<<endl;
 return 0;

}