Posts

Binary Tree

  #include <iostream> using namespace std ; struct Node {     int data ;     Node * left ;     Node * right ; }; // Function to create a new node Node * createNode ( int value ) {     Node * newNode = new Node ();     newNode -> data = value ;     newNode -> left = nullptr ;     newNode -> right = nullptr ;     return newNode ; } // Function to insert a node in the binary tree Node * insertNode ( Node * root , int value ) {     if ( root == nullptr ) {         root = createNode ( value );     } else if ( value < root -> data ) {         root -> left = insertNode ( root -> left , value );     } else {         root -> right = insertNode ( root -> right , value );     }     return root ; } // Inorder Traversal (Left, Root, Righ...

Insertion and selection sort

  // Insertion Sort void insertionSort ( int arr [], int n ) {     for ( int i = 1 ; i < n ; i ++ ) {         int key = arr [ i ];         int j = i - 1 ;         while ( j >= 0 && arr [ j ] > key ) {             arr [ j + 1 ] = arr [ j ];             j -- ;         }         arr [ j + 1 ] = key ;     } } // Selection Sort void selectionSort ( int arr [], int n ) {     for ( int i = 0 ; i < n - 1 ; i ++ ) {         int minIdx = i ;         for ( int j = i + 1 ; j < n ; j ++ ) {             if ( arr [ j ] < arr [ minIdx ]) {                 minIdx = j ;             }   ...

Binary Search

  void binarySearch ( int target ){         sort ();         int left = 0 ;         int right = MAX ;         while ( left <= right ) {             int mid = left + ( right - left ) / 2 ;             if ( sorted [ mid ] == target ) {                 cout << "Element found at " << mid << "in sorted array" ;                 return ;             } else if ( sorted [ mid ] < target ) {                 left = mid + 1 ;             } else {                 right = mid - 1 ;             }         }       ...

Circular Queue

  #include <iostream> #define MAX 5 using namespace std ; class CircularQueue { private:     char queue [ MAX ];     int front , rear ; public:     CircularQueue () {         front = rear = 0 ;     }     // Insert an element into the queue     void insert ( char element ) {         if (( rear + 1 ) % MAX == front ) {             cout << "Queue Overflow! Cannot insert element. \n " ;             return ;         }         queue [ rear ] = element ;         cout << "Inserted " << element << " into the queue. \n " ;         rear = ( rear + 1 ) % MAX ;     }     // Delete an element from the queue     void remove () {         if ( front ...

Infix to postfix

  #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 100 char stack [ MAX ]; char infix [ MAX ], postfix [ MAX ]; int top = - 1 ; int precedence ( char symbol ){     switch ( symbol )     {     case '^' :         return 3 ;     case '/' :     case '*' :         return 2 ;     case '+' :     case '-' :         return 1 ;     default :         return 0 ;     } } char pop (){     char c ;     if ( top ==- 1 ){         printf ( "Stack Underflow \n " );         exit ( 1 );     }     c = stack [ top ];     top -- ;     return c ; } void push ( char c ){     if ( top == MAX - 1 ){         printf ( "Stack Overflow \n " );   ...

Linked List

  #include <stdio.h> #include <stdlib.h> #define red     " \x1b [31m" #define RESET   " \x1b [0m" struct node {   int data ;   struct node * link ; }; char choice ; void quit (){     printf ( "Back to menu? (y/n):" );     scanf ( " %c " , & choice ); } // Count nodes int count_of_node ( struct node * head ){   int count = 0 ;   if ( head == NULL ){     printf ( "Linked List is empty \n " );     return 0 ;   }   struct node * ptr = head ;   while ( ptr != NULL ){     count ++ ;     ptr = ptr -> link ;   }   printf ( "Number of nodes: %d \n " , count );   // quit();   return count ; } // Traverse the linked list void traverse ( struct node * head ){   if ( head == NULL ){     printf ( "Linked List is empty \n " );     return ;   }   struct node * ptr = head ;   printf...

Array

  #include <iostream> using namespace std ; // Function to generate the array int generateArray ( int arr [], int max , int & size ) {     cout << "Enter elements of array: " ;     for ( int i = 0 ; i < max ; i ++ ) {         cin >> arr [ i ];         size ++ ;     }     return 0 ; } // Function to display the array int displayArray ( int arr [], int size ) {     cout << "Current array: " ;     for ( int i = 0 ; i < size ; i ++ ) { // Traverse only up to `size`         cout << arr [ i ] << " " ;     }     cout << endl ;     return 0 ; } // Function to insert an element into the array int insertInArray ( int arr [], int max , int & size ) {     if ( size >= max ) { // Prevent insertion if the array is full     ...