Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

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

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

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;
}

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;
}

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;
}

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

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;
}