Showing posts with label C - Language. Show all posts
Showing posts with label C - Language. Show all posts

Array Implementation of Stack (Using predefined Value)

// program to implement array

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

struct stack{

      int top;

      int capacity;

      int *array;

};

struct stack *create(int size){

      struct stack *s = (struct stack *)malloc(sizeof(struct stack));

      if(!s){

            printf("Not enough space\n");

            return NULL;

      }

      s->capacity = size;

      s->top = -1;

      s->array = malloc(s->capacity*sizeof(int));

      if(!s->array){

            printf("Not enough space\n");

            return NULL;

      }

      return s;

}

int isempty(struct stack *s){

      return (s->top==-1);

}

int isfull(struct stack *s){

      return (s->top==s->capacity-1);

}

int size(struct stack *s){

      return (s->top+1);

}

void push(struct stack *s,int data){

      if(isfull(s)){

            printf("stack overflow\n");

            return;

      }

      else

      s->array[++s->top]=data;

}

int pop(struct stack *s){

      if(isempty(s)){

            printf("Stack is underflow\n");

            return -1;

      }

      return s->array[s->top--];

}

int peek(struct stack *s){

      if(isempty(s)){

            printf("stack is Empty\n");

            return -1;

      }

      else

      return (s->array[s->top]);

}

int main()

{

      int capacity=15,i;

      struct stack *stk = create(capacity);

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

            push(stk,i);

      }

      printf("Top element in stack %d \n",peek(stk));

      printf("Deleted element in stack %d \n",pop(stk));

      printf("Top element in stack %d \n",peek(stk));

      printf("No of Element in Stack %d \n",size(stk));


    return 0;

}


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







Round G 2022 - Kick Start 2022 Walktober Problem (C Code)

// Below is the Solution of the Round G 2022 -kickstart 2022 :) 

// Contact me if any question persist in your mind, I'm ready to help you :) 

// HAPPY CODING

#include <stdio.h>

int main(){

    int t,count=0;

    scanf("%d",&t);

    while(t--){

        int M,N,P,k=0,i,j;

        scanf("%d%d%d",&M,&N,&P);

        int max[N],pl[N],n[N];

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

        max[i]=0;

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

            for(j=0;j<N;j++){

                scanf("%d",&n[j]);

                if((i+1)!=P){

                if(max[j]<n[j])

                max[j]=n[j];

                }

                else{

                    pl[j]=n[j];

                }

            }

        }

        int ans=0;

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

            if(max[i]>pl[i])

            ans+=(max[i]-pl[i]);

            else

            ans+=0;

        }

        printf("Case #%d: %d\n",++count,ans);

    }

    return 0;

}

Program to print the smallest word in an sentence (Coding Ninja Solutions)

#include <stdio.h>

#include <string.h> // Here I've to add this header file for strcat() function

int main() {

    char name[]="Vaibhav Yadav is good boy";

    char ccname[]=" "; // This is to add space at the end of sentence so that condition can read it.

    strcat(name,ccname); // this functions append the space to last character

    int i,j,k,min=50,count=0;

    for(i=0;name[i]!='\0';i++){

        if(name[i]!=' '){

        count++;

        }

        else{

            if(count<min)

            {

                min=count;

                k  = i;

                k-=count;

            }

                count=0;

        }

    }

    

    // printf("%d ",min);

    for(i=k;name[i]!=' ';i++)

    printf("%c",name[i]);

    k = 0;

    return 0;

}

Problem Solution of GBus count, Coding Practice with Kick Start Session #3 - Kick Start 2022 in C Language.

 


Problem

There exist some cities that are built along a straight road. The cities are numbered 1,2,3, from left to right.

There are N GBuses that operate along this road. For each GBus, the range of cities that it serves is provided: the i-th gBus serves the cities with numbers between Ai and Bi, inclusive.

We are interested in a particular subset of P cities. For each of those cities, we need to find out how many GBuses serve that particular city.

Input

The first line of the input gives the number of test cases, T. Then, T test cases follow; each case is separated from the next by one blank line. (Notice that this is unusual for Kickstart data sets.)

In each test case:

  • The first line contains one integer N: the number of GBuses.
  • The second line contains 2N integers representing the ranges of cities that the buses serve, in the form A1 B1 A2 B2 A3 B3 ... AN BN. That is, the first GBus serves the cities numbered from A1 to B1 (inclusive), the second GBus serves the cities numbered from A2 to B2 (inclusive), and so on.
  • The third line contains one integer P: the number of cities we are interested in, as described above. (Note that this is not necessarily the same as the total number of cities in the problem, which is not given.)
  • Finally, there are P more lines; the i-th of these contains the number Ci of a city we are interested in.

Output

For each test case, output one line containing Case #xy, where x is the number of the test case (starting from 1), and y is a list of P integers, in which the i-th integer is the number of GBuses that serve city Ci.

Limits

Memory limit: 1 GB.
1T10.

Test Set 1

Time limit: 60 seconds.
1N50.
1Ai500, for all i.
1Bi500, for all i.
1Ci500, for all i.
1P50.

Test Set 2

Time limit: 120 seconds.
1N500.
1Ai5000, for all i.
1Bi5000, for all i.
1Ci5000, for all i.
1P500.

Here is the solved code for this problem in c language:

#include<stdio.h>

int main()

{

    int t,ccase=0;

    scanf("%d",&t);

    while(t--)

    {

        int a,i,j,k;

        scanf("%d",&a);

        int c = 2*a;

        int b[c];

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

        scanf("%d",&b[i]);

            

        int p;

        scanf("%d",&p);

        int q[p];

        int count[p];

        

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

        count[i]=0;

        

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

        scanf("%d",&q[i]);

        

        for(j=0;j<p;j++)

        {

        for(k=0;k<c;k++)

        {

            if((q[j] >= b[k]) && (q[j] <= b[k+1])) {

            count[j]++;

            }

            k+=1;

        }

      }

      printf("Case #%d: ",++ccase);

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

      printf("%d ",count[i]);

      printf("\n");

     }

    return 0;

}


Operator Overloading in C++

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