Sunday, 20 March 2022

4 - Skip List

 Skip List in C

#include<stdio.h>

# define L 3

struct node {
    int val;
    struct node* next[3];
} *head = 0, *temp, *ptr1 = 0, *ptr2 = 0, *new1, *prev;

int main() {
    int ch, x, cost, c = 0, max, min;
    char res, name[30];
    do {
        printf("1.Add\n2.Display\n3.Search\nChoose any 1: \t");
        scanf("%d", &ch);
        switch(ch) {
            case 1:
                do {
                    new1 =(struct node*)malloc(sizeof(struct node));
                    for(int i=0; i<L; i++) {
                        new1->next[i] = 0;
                    }
                    printf("Enter data for the node : \t");
                    scanf("%d", &new1->val);
                    for(int i=0; i<L; i++) {
                        new1->next[i] = 0;
                    }
                    if(head == 0) {
                        head = new1;
                        c++;
                    } else {
                        temp = head;
                        while(temp->next[0] != 0) {
                            temp = temp->next[0];
                        }
                        temp->next[0] = new1;
                        c++;
                    }
                    printf("\nDo you want to add more: \t");
                    scanf(" %c", &res);
                } while(res == 'y');

                int ct = 1;
                for(int i=1; i<L; i++) {
                    temp = head->next[i-1];
                    for(prev = head; temp != 0; temp = temp->next[i-1]) {
                        ct++;
                        if(ct % L == 0) {
                            prev->next[i] = temp;
                            prev = temp;
                        }
                    }
                    ct = 1;
                }
            break;

            case 2:
                temp = head;
                for(int i=0; i<L; i++) {
                    printf("\n\n*******\tLevel %d\t********\n\n", i+1);
                    while(temp != 0) {
                        printf("%d -> ", temp->val);
                        temp = temp->next[i];
                    }
                    printf("NULL");
                    temp = head;
                }
            break;

            case 3:
                do {    
                    printf("\nEnter the value to be searched : \t");
                    scanf("%d", &x);
                    prev = head;
                    if(head == 0 || x < head->val) {
                        printf("\n\nData can't be found");
                    } else {
                        int i = L-1, ct = 0, found = 0;
                        temp = head;
                        while(i >= 0 && found == 0) {
                            if(temp->val == x) {
                                found = 1;
                                ct++;
                                printf("\n%d compared with %d", x, temp->val);
                                break;
                            }
                            else if(temp->val < x) {
                                ct++;
                                printf("\n%d compared with %d", x, temp->val);
                                prev = temp;
                                temp = temp->next[i];
                            }
                            else if(temp == 0) {
                                i--;
                                temp = prev->next[i];
                            } else {
                                printf("\n%d compared with %d", x, temp->val);
                                ct++;
                                i--;
                                temp = prev->next[i];
                            }
                        }
                        if(found == 0) {
                            printf("\n\n******** Key not found *********\n");
                        } else {
                            printf("\n\n********************************************************************************\n\nNo of comparisons are: %d\n\n********************************************************************************\n", ct);
                        }
                    }
                    printf("\nDo you want to search more: \t");
                    scanf(" %c", &res);
                } while(res == 'y');
            break;
        }
        printf("\n\n****************************************************\n****************************************************\n\nDo you want to continue: \t");
        scanf(" %c", &res);
    } while(res == 'y');
}

Sunday, 13 March 2022

IBM Data Science Professional Certificate - By Himanshu Agarkar


1. What is Data Science? (7.5 hours)



2. Tools for Data Science (8.5 hours)



3. Data Science Methodology (5 hours)



4. Python for Data Science, AI & Development (6 hours)



- Himanshu Agarkar

Monday, 7 March 2022

3 - Stack Using Singly Linked List

3 - Stack Using Singly Linked List

GFG

#include <bits/stdc++.h> using namespace std; struct Node { int data; Node* link; }; Node* top; void push(int data) { Node* temp = new Node(); if (!temp) { cout << "\nStack Overflow"; exit(1); } temp->data = data; temp->link = top; top = temp; } int isEmpty() { return top == NULL; } int peek() { if (!isEmpty()) return top->data; else exit(1); } void pop() { Node* temp; if (top == NULL) { cout << "\nStack Underflow" << endl; exit(1); } else { temp = top; top = top->link; free(temp); } } void display() { Node* temp; if (top == NULL) { cout << "\nStack Underflow"; exit(1); } else { temp = top; while (temp != NULL) { cout << temp->data << "-> "; temp = temp->link; } } } int main() { push(11); push(22); push(33); push(44); display(); cout << "\nTop element is " << peek() << endl; pop(); pop(); display(); cout << "\nTop element is " << peek() << endl; return 0; }

Own Code

#include <bits/stdc++.h>
using namespace std;

struct node {
    struct node* prev;
    int val;
    struct node* next;
} *head = 0, *ptr1, *ptr2, *temp, *new1;

int main() {
    int ch, x, c = 0;
    char res;
    do {
        printf("\n1.Push\n2.Pop\n3.Display\nChoose any 1 : \t");
        scanf("%d", &ch);
        switch(ch) {
            case 1:
                do {
                    printf("\nEnter value : \t");
                    scanf("%d", &x);
                    new1 = (struct node*)malloc(sizeof(struct node));
                    new1->prev = 0;
                    new1->val = x;
                    new1->next = 0;
                    if(head == 0) {
                        head = new1;
                        c++;
                    } else {
                        temp = head;
                        while(temp->next != 0) {
                            temp = temp->next;
                        }
                        temp->next = new1;
                        new1->prev = temp;
                        c++;
                    }
                    printf("\nDo you want to add more values ?: \t");
                    scanf(" %c", &res);
                } while(res == 'y');
            break;

            case 2:
            break;

            case 3:
                temp = head;
                while(temp->next != 0) {
                    temp = temp->next;
                }
                while(temp != head) {
                    printf("%d\n", temp->val);
                    temp = temp->prev;
                }
                printf("%d\n", head->val);
            break;
        }
        printf("\nDo you want to continue? : \t");
        scanf(" %c", &res);
    } while(res == 'y');
}

Sunday, 6 March 2022

2 - Polynomial Linked List

 2 Polynomial Linked List

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

struct node  {
    int coeff;
    int exp;
    struct node* next;
}*head1 = 0, *head2 = 0, *ptr1 = 0, *ptr2 = 0, *head = 0, *fin = 0, *temp, *fin;

struct node* insert(struct node* head, int co, int ex) {
    struct node* new1 = malloc(sizeof(struct node));
    new1->coeff = co;
    new1->exp = ex;
    new1->next = 0;
    if(head == 0 || ex > head->exp) {
        new1->next = head;
        head = new1;
    } else {
        temp = head;
        while(temp->next != 0 && temp->next->exp >= ex) {
            temp = temp->next;
        }
        new1->next = temp->next;
        temp->next = new1;
    }
    return head;
}

struct node* create(struct node* temp) {
    int terms, co, ex;
    printf("\nEnter no of terms : \t");
    scanf("%d", &terms);
    for(int i=0; i<terms; i++) {
        printf("\nEnter coeff and exp for %d term : \t", i+1);
        scanf("%d %d", &co, &ex);
        temp = insert(temp, co, ex);
    }
    return temp;
}

struct node* polyadd(struct node* head1, struct node* head2) {
    ptr1 = head1, ptr2 = head2;
    while(ptr1 != 0 && ptr2 != 0) {
        if(ptr1->exp == ptr2->exp) {
            fin = insert(fin, ptr1->coeff + ptr2->coeff, ptr1->exp);
            ptr1 = ptr1->next;
            ptr2 = ptr2->next;
        }
        else if(ptr1->exp > ptr2->exp) {
            fin = insert(fin, ptr1->coeff, ptr1->exp);
            ptr1 = ptr1->next;
        } else {
            fin = insert(fin, ptr2->coeff, ptr2->exp);
            ptr2 = ptr2->next;
        }
    }
    return fin;
}

void display(struct node* head) {
    if(head == NULL) {
        printf("List Empty");
    } else {
        temp = head;
        while(temp!=0) {
            printf("%d x^%d", temp->coeff, temp->exp);
            temp = temp->next;
            if(temp != NULL) {
                printf(" + ");
            } else {
                printf("\n");
            }
        }
    }
}

int main() {
    printf("\nCreate 1st polynomial");
    head1 = create(head1);
    printf("\n******************************************************\n");
    display(head1);

    printf("\nCreate 2nd polynomial");
    head2 = create(head2);
    printf("\n******************************************************\n");
    display(head2);
   
    fin = polyadd(head1, head2);
    printf("\n******************************************************\n");
    display(fin);
}

Wednesday, 2 March 2022

1 - Singly Linked List

1 - Singly Linked List 

Rakhi Mam, 3rd Sem

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

typedef struct singly_list
{
    int data;
    struct singly_list *next;
}*SLL;

SLL getnode()
{
    SLL new1;
    new1 = malloc(sizeof(struct singly_list));
    new1->next = NULL;
    return new1;
}

SLL head = NULL, head_hash = NULL;

int main()
{
    
    int ch;
    char res;
    SLL temp, new1, t1, t2, t3, prev, curr;
    int p,w;
    do
    {
    
    printf("\n1. creation\n2. display\n3.deletion\n4. Insertion\n5.sorting\n6.Reversing\n7.Print Prime Numbers\n8.Deleting Duplicate\n9.Find Middle element");
    printf("\nEnter your choice:\t");
    scanf("%d", &ch);
    switch(ch)
    {
        
        case 1: //create
                do
                {
                if(head == NULL)
                {
                    head = getnode();
                    printf("\n Enter data of head node:\t");
                    scanf("%d",&head->data);
                    temp= head;
                }
                else
                {
                    new1 = getnode();
                    printf("\n Enter data of new node:\t");
                    scanf("%d",&new1->data);
                    temp->next = new1;
                    temp = new1;    
                }
                printf("\n do you want to add further data in the linked\t");
                scanf(" %c",&res);
                }while(res=='y');
        
                break;
                
        case 2://display
                temp = head;
                while(temp!=NULL)       
                {
                    printf("%d->",temp->data);
                    temp= temp->next;
                }
                printf("NULL");
        
        
                break;
                
        case 3: //deletion
                
            printf("\nEnter position of node to be deleted:\t");
            scanf("%d",&p);
            temp =head;
            w =1;
            if(p==1) //deletion of first node
            {
                t1 = head;
                head = temp->next;
                free(t1);
            }
            else //deletion of last and any element except first
            {
            while(w<p-1)
            {
                temp = temp->next;
                w++;
            }
            t1 = temp->next;
            temp->next = temp->next->next;
            free(t1);
            }
                break;

        case 4: //insertionn of new node at any position
                printf("\n Enter data of new node:\t");
                new1 = getnode();
                scanf("%d",&new1->data);
                printf("\n\t\t\tEnter position of new node:\t");
                scanf("%d",&p);
                temp = head;
                if(p==1)
                {
                
                new1->next= head;
                head= new1;
            }
            else
            {
                while(p>2 && temp!=NULL)
                {
                    temp = temp->next;
                    p--;
                    
                }
                if(temp==NULL)
                {
                    printf("\n\t\t\tINVALID position");
                }
                else
                {
                
                new1->next = temp->next;
                temp->next =new1;
               }
                
            }
        
                break;

        case 6: //reversing of linked list
                t1 = head;
                t2= t1->next;
                t3= t2->next;
                t1->next=NULL;
                do
                {
                    t2->next =t1;
                    t1 = t2;
                    t2= t3;
                    t3= t3->next;
                }while(t3!=NULL);
                t2->next = t1;
                head =t2;
                
                break;
        
        case 7:
                temp = head;
                while(temp!=NULL){
                    int c=0;
                    for(int i=2; i<temp->data; i++){
                        if(temp->data%i == 0){
                            c++;
                            break;
                        }
                    }
                    if(c==0){
                        printf("%d ", temp->data);
                    }
                    temp = temp->next;
                }
                break;

        case 8: //delete duplicate
            temp = head;
            while(temp->next != NULL) {
                t1 = temp;
                while(t1 != NULL) {
                    if(t1->next != NULL && temp->data == t1->next->data) {
                        t2 = t1->next;
                        t1->next = t2->next;
                        free(t2);
                    }
                    t1 = t1->next;
                }
                temp = temp->next;
            }
            break;

        case 9: //finding middle element
            t1 = head;
            t2 = head;
            while(t2->next != NULL && t2->next->next != NULL){
                t2 = t2->next->next;
                t1 = t1->next;
            }
            printf("Middle element is %d", t1->data);
        break;
        default:
                exit(0);
        
    }
    printf("\n Do you want to cont...");
    scanf(" %c", &res);
    
}while(res=='y');
    
}