Tuesday, December 23, 2014

Print all possible combinations of r elements in a given array of size n

#include<stdio.h>
 int data[30];
void fun(int arr[],int i,int N,int r,int index)
{
int j;
if(r==index)
{
for(i=0;i<r;i++)
{
printf("%d",data[i]);
}
printf("\n");
return;
}
for(j=i;j<N;j++)
{
data[index]=arr[j];
fun(arr,j+1,N,r,index+1);
}
}

int main()
{
 int arr[5]={1,2,3,4,5};
 int r=3;
 fun(arr,0,5,r,0);
 return 0;
}

Sunday, December 21, 2014

C++ program to print all distinct elements in a given array


#include <iostream>
#include <algorithm>
using namespace std;

void printDistinct(int arr[], int n)
{
    // First sort the array so that all occurrences become consecutive
    sort(arr, arr + n);
    int i=0;

    // Traverse the sorted array
    while(i<n)
    {
    if(arr[i]==arr[i+1])
    {
    i=i+1;
    }
    else
    {
    cout<<arr[i]<<endl;
    i++;
    }
    }
}

// Driver program to test above function
int main()
{
    int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    printDistinct(arr, n);
    return 0;
}

Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!


#include<stdio.h>
int main()
{
int fact[10];
int i,n;
float sum=0;
fact[1]=fact[0]=1;
printf("Enter value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact[i]=fact[i-1]*i;
sum+=(float)1/fact[i];
}
printf("%f",sum);
return 0;
}

Replace all ‘0’ with ‘5’ in an input Integer

 /* Use of array to store all digits is not allowed.*/
#include<stdio.h>
static int x=0;
int fun(int num)
{
    if(num==0)
    {
    return;
    }
    else
    {
    int rem=num%10;
    if(rem==0)
    {
     rem=5;
    }
    num=num/10;
    fun(num);
    x=x*10+rem;
    }
return x;
}
int main()
{
printf("num is %d ",fun(105));
return 0;
}

Saturday, December 20, 2014

"QQ.h" file for Queue Operation

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int info;
    struct node *link;
} *front=NULL,*rear=NULL;


void insert(int item)
{

    struct node *tmp;
    tmp=(struct node*)malloc(sizeof(struct node));
    if(tmp==NULL)
        {
        printf("Memory  not avaliable\n");
        return ;
        }
    tmp->info=item;
    tmp->link=NULL;
    if(front==NULL)
    {
        front=tmp;
    }
    else
        rear->link=tmp;
        rear=tmp;
}


int del()
{
    struct node *tmp;
    int item;
    if(isEmpty())
        {
            printf("Queue underflow\n");
            exit(1);
        }
    tmp=front;
    item=tmp->info;
    front=front->link;
    free(tmp);
    return item;
}

"stack.h" file

 /* Stack Operation with push and pop */
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

struct stack
{
    int arr[MAX];
    int top;
};


void initstack(struct stack *s)
{
    s->top=-1;
}


void push(struct stack *s,int item)
{
    if(s->top==MAX-1)
    {
    printf("stack is full\n");
    }
    else
    {
    s->top++;
    s->arr[s->top]=item;
    }
}


int pop(struct stack *s)
{
    int data;
    if(s->top==-1)
    {
    printf("Stack is empty.\n");
    exit(1);
    }
    else
    {
    data=s->arr[s->top];
    s->top--;
    return data;
    }
}

Reverse of Queue using Stack

 /*  1.First insert all  elements in rear side of Queue.
     2.Secondly,delete elements one by one from   front    of Queue and puts those in a stack .
  3.Then pop and print the elements until empty */

#include<stdio.h>
#include "stack.h"  //given below
#include"QQ.h"   //given below

int main()
{
    int n,arr[20],i,j=0;
    struct stack s;
    initstack(&s);
    printf("Enter no");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter values:");
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
        insert(arr[i]);
    }
    while(j!=n)
    {
        push(&s,del());
        j++;
    }
    printf("Reverse is ");
    while(s.top!=-1)
    {
        printf("%d",pop(&s));
    }
    printf("\n");
return 0;
}

get_Minimum.h file

/* save as "get_Minimum.h" file  */
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct stack
{
int arr[MAX];
int getMin[MAX];
int top;
};
void initstack(struct stack *s)
{
s->top=-1;
}
void push(struct stack *s,int item)
{
if(s->top==MAX-1)
{
printf("stack is full\n");
}
else
{
if(s->top==-1)
{
s->top++;
s->getMin[s->top]=item;
s->arr[s->top]=item;
}
else if(item<=(s->getMin[s->top]))
{
   printf("push in else if is %d \n",s->getMin[s->top]);
s->top++;
s->getMin[s->top]=item;
s->arr[s->top]=item;
}
else
{
int x=s->getMin[s->top];
printf("push in else is %d\n",s->getMin[s->top]);
s->top++;
s->arr[s->top]=item;
s->getMin[s->top]=x;
}
}


}
int pop(struct stack *s)
{
int data,min;
if(s->top==-1)
{
printf("Stack is empty.\n");
exit(1);
}
else
{
data=s->arr[s->top];
min=s->getMin[s->top];
s->top--;
printf("\n minimum is %d \n",min);
return data;
}
}

How to design a stack such that getMinimum() should be O(1) ??


#include<stdio.h>
#include<stdlib.h>
#include"get_Minimum.h"
#define MAX 10
int main()
{
struct stack s;
int i,item;
int  ch;
initstack(&s);
while(1)
{
printf("Enter 1. for Push\n");
printf("Enter 2. for pop  with minimum value\n");
printf("Enter 3. for exit.\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter item:");
scanf("%d",&item);
push(&s,item);
break;
case 2:
printf("poped element is %d \n",pop(&s));
break;
case 3:
exit(1);
break;
default:
printf("Wrong Input\n");
}
}
return 0;

}http://www.geeksforgeeks.org/design-and-implement-special-stack-data-structure/

Thursday, December 18, 2014



Implement Queue Using Link List in c

#include<stdio.h>
#include<stdlib.h>
#include "QUEUE.h"  //given before
int main()
{
struct node *s1=NULL,*s2=NULL;
int choice ,item;
while(1)

{
printf("\n1.Insert\n");
printf("2. Delete\n");
printf("3. Display the element at the front\n");
printf("4. Display all element of the queue\n");
printf("5. Quit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input the element for adding in queue:");
scanf("%d",&item);
insert(&s1,&s2,item);
break;
case 2:
printf("deleted item is %d",del(&s2));
break;
case 3:
printf("Element at the front of Queue is %d\n",peek(s2));
break;
case 4:
display(s2);
break;
case 5:
exit(1);
}
}
return 0;
}

Queue Operation in c

/* save as QUEUE.h  and include it in  main program */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
} ;
void insert(struct node **front,struct node **rear,int item)
{
struct node *tmp;
struct node *rr=*rear;
struct node *ff=*front;
tmp=(struct node*)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf("Memory  not avaliable\n");
return ;
}
if(*front==NULL)
{
tmp->info=item;
tmp->link=NULL;
*front=tmp;
*rear=tmp;
}
else
{
while(rr->link!=NULL)
{
(rr)=(rr)->link;
}
tmp->info=item;
tmp->link=NULL;
(rr)->link=tmp;
}

}
int del(struct node **front)
{
struct node *tmp;
int item;
if(*front==NULL)
{
printf("Queue underflow\n");
exit(1);
}
tmp=*front;
item=tmp->info;
*front=(*front)->link;
free(tmp);
return item;
}
int peek(struct node *front)
{
struct node *tmp;
int item;
if(front==NULL)
{
printf("underflow\n");
exit(1);
}
return (front)->info;
}
int isEmpty(struct node *front)
{
if(front==NULL)
{
return 1;
}
else
{
return 0;
}
}
void display(struct node *front)
{
struct node *ptr;
ptr=front;
if(front==NULL)
{
printf("Queue is Empty\n");
return ;
}
printf("Queue elements :\n\n");
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr=ptr->link;
}
}

Tuesday, December 16, 2014

Queue Implemention Using Two Stacks in c

/* Queue implemention using two stack  */


#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
#define MAX 20
int main()
{
    struct stack s1,s2;
    int i,item;
    int  ch;
    initstack(&s1);
    initstack(&s2);
    while(1)
    {
        printf("Enter 1. for Push\n");
        printf("Enter 2. for pop\n");
        printf("Enter 3. for exit.\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
        printf("Enter item:");
        scanf("%d",&item);
        push(&s1,item);
        break;
        case 2:
        if(isEmpty(&s2))
        {   
        while(!isEmpty(&s1))
        {
        push(&s2,pop(&s1));
        }
        }
        printf("poped element is %d",pop(&s2));
        break;
        case 3:
        exit(1);
        break;
        default:
        printf("Wrong Input\n");
        }
    }
return 0;
}

Monday, December 15, 2014

Sort a stack elements using recursion in c

#include<stdio.h>
#include"stack.h"    //given before
void insertAtBottom(struct stack *S,int data)
{
   
    if(isEmpty(S))
    {
    push(S,data);
    return;
    }
    if(topElement(S) >data)
    {
    int temp=pop(S);   
    insertAtBottom(S,data);
    push(S,temp);
    }
    else
    {
    push(S,data);
    }
}
void Sort_Stack(struct stack *S)
{
    int data;
    if(isEmpty(S))
    {
    return ;
    }   
    else
    {
    data=pop(S);
    Sort_Stack(S);
    insertAtBottom(S,data);
    }
}

int main()
{
    struct stack s;
    int i=0,x;
    initstack(&s);
    printf("Before Reverse:\n");
    while(i<6)
    {
    printf("Enter value:");
    scanf("%d",&x);
    push(&s,x);
    i++;
    }
    Sort_Stack(&s);
    printf("After Reverse:");
    display(&s);
return 0;
}
   

Reverse the elements of stack using only stack operation(push() and pop()) in c

 Algorithm:
* Fiorst pop all the elements of the stack till it becomes empty.
* For each call in recursion ,insert the element at the bottom of the stack.

#include<stdio.h>
#include"stack.h"
void insertAtBottom(struct stack *S,int data)
{
    if(isEmpty(S))
    {
    push(S,data);
    return ;
    }
    else
    {
    int temp=pop(S);
    insertAtBottom(S,data);
    push(S,temp);
    }
}
void Reverse_Stack(struct stack *S)
{
    int data;
    if(isEmpty(S))
    {
    return ;
    }   
    else
    {
    data=pop(S);
    Reverse_Stack(S);
    insertAtBottom(S,data);
    }
}

int main()
{
    struct stack s;
    int i;
    initstack(&s);
    printf("Before Reverse:");
    for(i=0;i<=5;i++)
    {
    printf("%d-->",i);
    push(&s,i);
    }
    Reverse_Stack(&s);
    printf("After Reverse:");
    display(&s);
return 0;
}
   

program to check array of integers is palindrome or not using stack

/* program to check array of integers is palindrome or not using stack */

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>  // for bool type
#include"stack.h"   // stack operation included here
#define MAX 20
int n;
struct stack s;
bool palindrome(int m,int A[])
{
    int flag=0,j;
    for(j=m;j<n;j++)
    {
        if(A[j]==pop(&s))
        {
        flag=1;
        }
        else
        {
        flag=0;
        }
    }
return flag;
}
int main()
{
    int flag=0;   
    int i,j,A[10], item;
    int  ch;
    initstack(&s);
    printf("Enter no of integers :");
    scanf("%d",&n);
   
    for(i=0;i<n;i++)
    {
    printf("Enter items:");
    scanf("%d",&A[i]);
    }
     for(i=0;i<(n/2);i++)
    {
    push(&s,A[i]);
    }
    if(n%2==0)
    {
        if(palindrome(n/2,A))
        {
           printf("Palindrome\n");   
        }
        else
        {
           printf("Not palindrome\n");
        }
    }
   
    else
    {
        if(palindrome((n/2)+1,A))
        {
          printf("Palindrome\n");
        }
        else
        {
          printf("Not palindrome\n");
        }
    }
return 0;
}

Stack Operation With Push() & Pop() Function

/* Stack Operation With Push() & Pop() Function
Save as 'stack.h' and include it with main function  */
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
struct stack
{
    int arr[MAX];
    int top;
};
void initstack(struct stack *s)
{
    s->top=-1;
}
void push(struct stack *s,int item)
{
    if(s->top==MAX-1)
    {
    printf("stack is full\n");
   
    }
    else
    {
    s->top++;
    s->arr[s->top]=item;
    }
}
int pop(struct stack *s)
{
    int data;
    if(s->top==-1)
    {
    printf("Stack is empty.\n");
    exit(1);
    }
    else
    {
    data=s->arr[s->top];
    s->top--;
    return data;
    }
}

Implement Stack Using Array in C


 /* Implement Stack Using Array In C */


#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct stack
{
    int arr[MAX];
    int top;
};
void initstack(struct stack*);
void push(struct stack *s,int item);
int pop(struct stack *s);
int main()
{
    struct stack s;
    int i,item;
    int  ch;
    initstack(&s);
    while(1)
    {
        printf("Enter 1. for Push\n");
        printf("Enter 2. for pop\n");
        printf("Enter 3. for exit.\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
        printf("Enter item:");
        scanf("%d",&item);
        push(&s,item);
        break;
        case 2:
        i=pop(&s);
        printf("item poped: %d\n",i);
        break;
        case 3:
        exit(1);
        break;
        default:
        printf("Wrong Input\n");
        }
    }
return 0;
}
void initstack(struct stack *s)
{
    s->top=-1;
}
void push(struct stack *s,int item)
{
    if(s->top==MAX-1)
    {
    printf("stack is full\n");
   
    }
    else
    {
    s->top++;
    s->arr[s->top]=item;
    }
}
int pop(struct stack *s)
{
    int data;
    if(s->top==-1)
    {
    printf("Stack is empty.\n");
    exit(1);
    }
    else
    {
    data=s->arr[s->top];
    s->top--;
    return data;
    }
}

Sunday, December 14, 2014

Maximum SubArray sum in c++

#include<iostream>
using namespace std;
int max(int a,int b)
{
if(b>a)
{
a=b;
}
return a;
}

int fun_max_subArray(int Arr[],int n)
{
int ans=-10000;
for(int i=1;i<=n;i++)
{
for(int j=0;j<n;j++)
{
if((i+j)>n)
break;
int sum=0;
for(int k=j;k<(i+j);k++)
{
sum+=Arr[k];
ans=max(ans,sum);

}
}
}
return  ans;
}
int main()
{
int Arr[4]={3,-2,5,-1};
cout<<"maximum sum sub array is"<<fun_max_subArray(Arr,4);
}

remove duplicate elements from sorted link list in c

/* remove duplicate elements from sorted link list*/
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node{

int data;
struct node* next;
};
/* Function to get the middle of the linked list*/
void RemoveDuplicates_from_sortedList(struct node **head)
{
struct node *temp=*head;
struct node *del;

while(((temp)!=NULL)&&((temp)->next!=NULL))
{
if((temp->data)==(temp->next->data))
{
del=temp->next;
temp->next=del->next;
free(del);
temp=*head;
}
else
{
temp=temp->next;
}
}

}
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// A utility function to print a given linked list
void printList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
int main()
{
/* Start
/* Start with the empty list */
struct node* head = NULL;
int i;
push(&head,3);
push(&head,3);
push(&head,2);
push(&head,2);
push(&head,2);
push(&head,1);
push(&head, 1);
push(&head,1);
push(&head,1);
push(&head,1);
printf("Sorted List is :->");
printList(head);
RemoveDuplicates_from_sortedList(&head);
printf("\n\nAfter removing duplicate from sorted List is: ->");
printList(head);
return 0;
}