Wednesday, December 8, 2021

Priority queue deleting elements after ascending inputs Gaand Fadd

 using namespace std;
#include<iostream>
#define N 5
class pq {
public:
    int* arr;
    int front;
    int rear;
    pq() {
        front = -1;
        rear = -1;
        arr = new int[N];
    }
    int isEmpty() {
        if((front == -1 && rear == -1) || (front > rear))
            return 1;
        else
            return -1;
    }
    int isFull() {
        if(rear == N -1)
            return 1;
        else
            return -1;
    }
    void ins(int elt) {
        if(isFull() == 1){
            cout<<"Full"<<endl;
        }else if((front == -1) && (rear == -1)){
            front++;
            rear++;
            arr[rear] = elt;
        } else {
            
                  if(arr[rear] > elt){

                    arr[rear + 1] = arr[rear];
                    arr[rear] = elt;
                    int current = rear;
                    rear++;
                    while(current!=front){
                        if(arr[current -1] > arr[current]){
                              int temp;
                              temp = arr[current -1];
                              arr[current -1] = elt;
                              arr[current] = temp;
                          }
                          current = current -1;
                    }
                          
                    
                
                } else {
                    rear++;
                    arr[rear] = elt;
                }
        }
    }

    void del() {
        if(isEmpty() == 1){
            cout<<"Empty"<<endl;
        } else {
            front++;
        }

    }
    void show() {
        cout<<"\n____________________"<<endl;
        for(int i = 0; i<N; i++){
            cout<<arr[i];
        }
    }


};
int main() {

    pq ob;
    if(ob.isEmpty() == 1)
        cout<<"isEmpty == Yes";
    else
        cout<<"isEmpty == NO";

    ob.ins(4);
    ob.ins(2);
    ob.ins(1);
    ob.ins(3);
    ob.ins(5);


    


    ob.show();

    cout<<"xxx"<<ob.rear<<endl;


    

    return true;
}

Binary Search Algorithm

 #include<stdio.h>
int show(int* a, int n, int data) {
    int l = 0;
    int r = n - 1;
    int mid ;
    while(l <= r) {
         mid = ( l + r )/2;

        if(data == a[mid]) {
            return mid;
            break;
        } else {
            if(data < a[mid]){
                r = mid -1;
            
            } else {
                l = mid + 1;
            }
        }
    }
    
    return -1;
}
int main() {
    int a[10] = {5, 9, 17, 23, 25, 45, 59, 63, 71, 89};
    int* arr = a;
    int key = 59;
    int x = show(arr, 10, key);
    if(x == -1)
        printf("Data not found");
    else
        printf("%d", x);
    return 0;
}

Circular Queue Insertion Deletion

 using namespace std;
#include<stdio.h>
#define N 5
class q{
    public:
    int* arr;
    int front;
    int rear;

    q() {
        arr = new int[N];
        front = -1;
        rear = -1;
    }
    
    int isFull() {
        if((front == 0 && rear == N-1) || (front == rear + 1))
          return 0;
        else
          return -1;
    }
    
    void enq(int x) {
        if(isFull() == 0) {
            printf("\nQueue Full\n");
        } else if(front == -1 && rear == -1) {
                front = 0;
                rear = 0;
                arr[rear] = x;
        } else {
            rear = (rear + 1) % N;
            arr[rear] = x;
        }
    }
    
    int isEmpty() {
        if(front == -1 && rear == -1)
         return 0;
        else
         return -1;
    }
    
    void dq() {
        if(isEmpty() == 0) {
            printf("\nQueue is Empty\n");
        } else if(front == rear) {
            printf("\nDeleted element %d\n", arr[rear]);
            front = -1;
            rear = -1;
        } else {
            printf("\nDeleted Element %d\n", arr[front]);
            front = (front + 1) % N;
        }
    }
    
};
int main() {
    q ob;
    
    if( ob.isEmpty() == 0)
        printf("isEmpty = Yes");
    else
        printf("isEmpty = No");
        
    ob.dq();
    
    ob.enq(1);
    ob.enq(2);
    ob.enq(3);
    ob.enq(4);
    ob.enq(5);
    ob.dq();
    ob.enq(6);
    ob.dq();
    ob.dq();
    ob.dq();
    ob.dq();
    ob.dq();
    printf("\nFront and Rear After All Del %d and %d", ob.front, ob.rear);
    printf("\n");
    for(int i = 0; i<=4; i++) {
        printf("%d \t", ob.arr[i]);
    }
}

Tuesday, November 30, 2021

Coding Challange 2

 Question : arr[1, 2, 3, 4, 5, 6, 7] ko arr[4, 5, 6, 7, 1, 2, 3] banana hai .


#include<stdio.h>
void reverse(int* arr) {
    int frst = 0;
    int temp;
    int i;
    int j;
    while(frst != 3) {
        temp = arr[0];
        for(i = 1; i<=6; i++) {
            arr[i - 1] = arr[i];
        }
        arr[6] = temp;
        frst++;
    }
    for( j = 0; j<=6; j++) {
        printf("%d", arr[j]);
    }
}
int main() {
    int arr[7] = {1, 2, 3, 4, 5, 6, 7};
    reverse(arr);
    return 0;
}
//4567 123

Monday, November 29, 2021

StrCpy Implementation

 #include<stdio.h>
void fncpy(char* str2, char* str1) {
    int i = 0;
    while(*str1 != '\0') {
        str2[i] = *str1;
        i++;
        str1++;
    }
    *str1 = '\n'; //Isse lagana padega qki compiler khud ye copy nahi kareaga
    printf("Content from str2 to str1 is %s", str2);
}
int main() {
    char str1[5] = "atul";
    char str2[5];
    fncpy(str2, str1);
    return 0;
}

Strcmp implementation by self, while mein dhyan de do conditions par check kar raha hai

 #include<stdio.h>
int fnCmp(char* str1, char* str2) {
    while( (*str1 != '\0' && *str2 != '\0') && (*str1 == *str2)) {
        str1++;
        str2++;
    }
    if(*str1 == *str2)
       return 0;
    else
       return *str1 - *str2;
}
int main() {
    char str1[] = "abc";
    char str2[] = "abc";
    int x = fnCmp(str1, str2);
    if(x == 0) {
        printf("Strings Are Same");
    } else {
        printf("Strings Are not Same");
    }
    return 0;
}

Strings {pata nahi direct concatination bhi hota hai}

 #include<stdio.h>

int main() {
    
    char name[10] = {'s', 't', 'r', 'i', 'n', 'g'};
    printf("%s", name);
    char *str = "Hello World";
    printf("\n");
    while(*str != '\0') {
        printf("%c", *str);
        str = str + 1;
    }


    printf("\n");
    printf("%c", "Hello"[0]);


    printf("\n");
    printf("%c", *("Atul" + 0));


    printf("\n");


    printf("Ashish\
    Madarchod\
    Hai \n");


    printf("Ashish""Madarchod""Tha""Hai""Aur""Rehega");
}

Priority queue deleting elements after ascending inputs Gaand Fadd

 using namespace std; #include<iostream> #define N 5 class pq { public:     int* arr;     int front;     int rear;     pq() {         ...