Program to find the last index of an element of array using recursive function.

 // coded by Vaibhav Yadav


#include <iostream>

#define ll long long

ll index(ll a[],ll size,ll x);

using namespace std;

int main() {

    ll n,i,x;

    cout<<"Enter Your Number of Elements in array: ";

    cin>>n;

    ll a[n];

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

        cout<<"Enter Your Element no "<<i+1<<": ";

        cin>>a[i];

    }

    cout<<"Enter the element you want to search for: ";

    cin>>x;

    if(index(a,n,x)>=0)

    cout<<"Element Is found at position "<<index(a,n,x)<<endl;

    else

    cout<<"Element is not found"<<endl;

    return 0;

}

ll index(ll a[], ll size, ll x){

    if(a[size-1]==x)

    return size-1;

    else

    index(a,size-1,x);

}

Singly Linked List Operation.

 // creation of singly linked lists

// coded by Vaibhav Yadav

#include<stdio.h>

#include<stdlib.h>

// Declaring the structure that will contain the info part only and linked List

struct node

{

    int info;

    struct node *link;

};

struct node *create_list(struct node *start);

struct node *addat_beg(struct node *start,int data);

struct node *addat_end(struct node *start,int data);

void display(struct node *start);

void search(struct node *start,int item);

struct node *reverse(struct node *start);

struct node *delete(struct node *start,int item);

struct node *addat_after(struct node *start,int data,int item);

struct node *addat_before(struct node *start, int data ,int item);


int main(void){

    struct node *start  = NULL;

    int choice,data,item,pos;

    while(1){

        printf("\n");

        printf("====================================\n");

        printf("Enter Your Operation From the below\n");

        printf("1.Create Linked  List\n");

        printf("2.Reversal of Linked List\n");

        printf("3.Searching of an element\n");

        printf("4.Delete an element\n");

        printf("5.Add at after the data\n");

        printf("6.Add at before the data\n");

        printf("7.Display the data\n");

        

        printf("Enter Your choice: ");

        scanf("%d",&choice);

        printf("\n");

        

        switch(choice){

            case 1: start = create_list(start);

                    break;

            case 2: start = reverse(start);

                    break;

            case 3: printf("Enter the element you want to search for\n");

                    scanf("%d",&item);

                    search(start,item);

                    break;

            case 4: printf("Enter the item you want to delete\n");

                    scanf("%d",&item);

                    start = delete(start,item);

                    break;

            case 5: printf("Enter the item you want to insert\n");

                    scanf("%d",&item);

                    printf("which data you want to insert after\n");

                    scanf("%d",&data);

                    start = addat_after(start,data,item);

                    break;

            case 6: printf("Enter the item you want to insert\n");

                    scanf("%d",&item);

                    printf("Enter data you want to enter before\n");

                    scanf("%d",&data);

                    start = addat_before(start,data,item);

                    break;

            case 7: display(start);

                    break;

            default:

            printf("Wrong Choice Bro\n");

        }

    }

    return 0 ;

}

// Definition of all the function starts from here 

// This Funciton will add the element at beginning of the List

struct node *addat_beg(struct node *start, int data){

    struct node *tmp;

    tmp = (struct node *)malloc(sizeof(struct node));

    tmp -> info = data;

    tmp -> link = start;

    start = tmp;

    return start;

} // end of beginning of the List


// this function will add the element at the end of List

struct node *addat_end(struct node *start, int data){

    struct node *tmp,*p;

    p = start;

    tmp = (struct node *)malloc(sizeof(struct node));

    tmp -> info = data;

    while(p->link!=NULL)

    p = p -> link;

    p->link = tmp;

    tmp -> link = NULL;

    return start;

} // end of the function


// this funciton will displya the content of the page in the linke List

void display(struct node *start){

    int count = 0;

    struct node *p;

    if(start == NULL){

        printf("linked list is empty\n");

        return;

    }

    p = start;

    printf("List is : \n ");

    while(p!=NULL){

        printf("%d  ",p->info);

        p = p->link;

        count++;

    }

    printf("\nIt contain total of %d element\n",count);

    printf("\n\n");

} //end of the function

// Now this function will create a linked List

struct node *create_list(struct node *start){

    int data,i,n;

    printf("Enter Your the number of nodes\n");

    scanf("%d",&n);

    start = NULL;

    if(n==0){

        return start;

    }

    printf("Enter the 1 element to be inserted : ");

    scanf("%d",&data);

    start = addat_beg(start,data);

    for(i=2;i<=n;i++){

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

        scanf("%d",&data);

        start = addat_end(start,data);

    }

    display(start);

    return start;

} // end of create_list


// this function will reverse the linked List

struct node *reverse(struct node *start){

    struct node *prev, *ptr, *next;

    prev = NULL;

    ptr = start;

    while(ptr!=NULL){

        next = ptr->link;

        ptr->link=prev;

        prev = ptr;

        ptr = next;

    }

    start = prev;

    display(start);

    return start;

} // end of reverse function


// this funcion will search the linked list for an item in item

void search(struct node *start, int item){

    struct node *p;

    if(start == NULL){

        printf("List is empty");

        return;

    }

    int pos = 1;

    p=start;

    while(p!=NULL){

        if(p->info==item){

            printf("Element is found at position %d: \n",pos);

            return;

        }

        p=p->link;

        pos++;

    }

    printf("Element is not found in the linked list\n");

} //end of search function 


// this function will delete the element from the linked list

struct node *delete(struct node *start, int item){

    struct node *p,*tmp;

    if(start==NULL){

        printf("List is empty no operation can be performed here\n");

        return start;

    }

    // check wheather element is not the first element of the List if first then delete the node exactly here

    if(start->info==item){

        tmp = start;

        start = tmp->link;

        free(tmp);

        return start;

    }

    p = start;

    while(p!=NULL){

        if(p->link->info == item){

            tmp = p->link;

            p->link = tmp->link;

            free(tmp);

            display(start);

            return start;

        }

        p=p->link;

    }

    printf("Element is not found at the list\n");

    return start;

} // end of delete function 



// this funciton will add item before a data

struct node *addat_after(struct node *start,int data, int item){

    struct node *p,*tmp;

    if(start ==NULL){

        printf("List is empty\n");

        return start;

    }

    // if item to be inserted before a node

    if(start->info == data){

        tmp = (struct node *)malloc(sizeof(struct node));

        tmp->info = item;

        tmp->link = start;

        start = tmp;

        return start;

    }

    // if data is not present in first node then this code  will be executed

    p=start;

    while(p!=NULL){

        if(p->info == data){

            tmp = (struct node *)malloc(sizeof(struct node));

            tmp->info = item;

            tmp->link = p->link;

            p->link = tmp;

            printf("Element is Inserted\n");

            return start;

        }

        p=p->link;

    }

    printf("Give Element is not present in this list so data can't be inserted in this\n");

    return start;

} // end of the function


// this function will add item before an element

struct node *addat_before(struct node *start, int data , int item){

    struct node *p,*tmp;

    if(start==NULL){

        printf("List is empty no operation could be performed in this condtion\n");

        return start;

    }

    if(start->info == data){

        printf("Element is inserted at position 1\n");

        tmp = (struct node *)malloc(sizeof(struct node));

        tmp->info=item;

        tmp->link = start;

        start = tmp;

        return start;

    }

    p=start;

    while(p!=NULL){

        if(p->link->info==data){

            printf("Item has been inserted\n");

            tmp = (struct node *)malloc(sizeof(struct node));

            tmp->info=item;

            tmp->link = p->link;

            p->link=tmp;

            return start;

        }

        p=p->link;

    }

    return start;

} // end of the function







Operator Overloading in C++

#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0){       re...