Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. Show all posts

Sunday, 31 July 2016

Selection Sort



#include<stdio.h>
void s_sort(int* ,int);
int main()
{
int i;

// intialize array and give value
int a[]={-6,4,-8,0,2,3,1};
int n=(sizeof(a))/(sizeof(int));
printf("%d\n",n);

// define function
s_sort(a,n);

// print sorted array
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

void s_sort(int* a,int n)
{
int i,j,temp;

// intialize loop for sorting elements
for(i=0;i<(n-1);i++)
{
// define minimum value

int min = i;

// find minimum value in array
for(j=i+1;j<n;j++)
{
if (a[j] < a[min]) 
            min = j;
}

// if minimum value is find in array then swap 
if(min!=i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}


Binary Search



#include<stdio.h>
void b_search(int* a,int n,int value)
{
int low=0,high=n-1,flag=0,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==value)
{
flag=1;
break;
}
else if(a[mid]>value)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(flag==0)
{
printf("Value is not found\n");
}
else
{
printf("Value is found\n");
}

}
int main()
{
int value;
int a[]={1,2,3,4,5,6,7,8,9,20};
int n=(sizeof(a))/(sizeof(int));
printf("enter the value you want to find\n");
scanf("%d",&value);
b_search(a,n,value);
return 0;
}


Linear Search



#include<stdio.h>
void l_search(int* a,int n,int value)
{
int flag=0,i;

// initilize loop
for(i=0;i<n;i++)
{
if(a[i]==value)
{
flag=1;
}
}

// print value found or not
if(flag==0)
{
printf("Value is not found\n");
}
else
{
printf("Value is found\n");
}
}

int main()
{
int value;

// initialize array and value
int a[]={1,2,3,4,5,6,7,8,9,20};
int n=(sizeof(a))/(sizeof(int));

// get value for finding
printf("enter the value you want to find\n");
scanf("%d",&value);

// define function
l_search(a,n,value);
return 0;
}

Evaluate Postfix


stack.h


#include <stdlib.h>
typedef struct stack
{
    int *values;
    int maxSize;
    int top;
}stack;

stack * createstack(int maxElements)
{
    //create stack
    stack *s;
    s = (stack *)malloc(sizeof(stack));
    
    //initialize
    s->values = (int *)malloc(sizeof(int)*maxElements);
    s->top = -1;
    s->maxSize = maxElements;
    
    //return
    return s;
}

void init(stack *s,int maxElements)
{
    s = (stack *)malloc(sizeof(stack));
    
    //initialize
    s->values = (int *)malloc(sizeof(int)*maxElements);
    s->top = -1;
    s->maxSize = maxElements;
}

void push(stack *s, int n)
{
    if(s->top == s->maxSize)
    {
        printf("stack is Full\n");
    }
    else
    {
        printf("Pushing Value:%d\n",n);

        //increament first then push
        ++s->top;
        s->values[s->top] = n;
    }
    return;
}

int pop(stack *s)
{
    int value;
    if(s->top == -1)
    {
        printf("stack is Empty\n");
    }
    else
    {
        //remove first then decrement
        value = s->values[s->top];
        s->top--;
        printf("Poping value:%d\n",value);
    }
    return  value;
}

void display(stack *s)
{
    int i=0;
    for(i=0;i<=s->top;i++)
    {
        printf("%d ",s->values[i]);
    }
}

imp.c


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"
int calc(int n1,int n2, char c){
  switch(c)
  {
    case '*':
      return n1*n2;
      break;

    case '/':
      return n1/n2;
      break;

    case '+':
      return n1+n2;
      break;

    case '-':
      return n1-n2;
      break;

    default:
      return 0;
  }
}

void main()
{
  int i=0,value=0;
  stack *s = createstack(10);
  char *exp = "10 10 10 9 * + +";
  for(i=0;i<strlen(exp);i++)
  {
    if(isdigit(exp[i]))
    {
      value = (value * 10) + (exp[i] - '0');
    }
    else if(exp[i] == '+' || exp[i] == '*' || exp[i] == '/' || exp[i] == '-'){
      value = calc(pop(s),pop(s),exp[i]);
    }
    else
    {
      push(s,value);
      value =0;
    }
  }
  printf("%d\n",value);
}

http://bebe.com

Tuesday, 26 July 2016

Infix To Postfix Converter



char_stack.h


typedef struct stack
{
char *values;
int maxSize;
int top;
}stack;

stack * createstack(int maxElements)
{
//create stack
    stack *s;
    s = (stack *)malloc(sizeof(stack));
    
    //initialize
    s->values = (char *)malloc(sizeof(char)*maxElements);
    s->top = -1;
    s->maxSize = maxElements;
    
    //return
    return s;
}

void push(stack *s, char n)
{
if(s->top == s->maxSize)
    {
        printf("stack is Full\n");
    }
    else
    {
        printf("Pushing Value:%c\n",n);

        //increament first then push
        ++s->top;
        s->values[s->top] = n;
    }
    return;
}

char pop(stack *s)
{
char value;
if(s->top == -1)
    {
        printf("stack is Empty\n");
    }
    else
    {
        //remove first then decrement
    value = s->values[s->top];
        s->top--;
        printf("Poping value:%c\n",value);
    }
    return  value;
}

int isEmpty(stack *s)
{
    if(s->top == -1)
        return 1;
    else
        return 0;
}

void display(stack *s)
{
    int i=0;
    for(i=0;i<=s->top;i++)
    {
        printf("%c",s->values[i]);
    }
}

Postfix.c


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "char_stack.h"
#define SIZE 30
#define OPERAND 100

int priority(char c)
{
int priority;
switch(c){
case '^':
priority = 4;
break;
case '/':
case '*':
priority = 3;
break;
case '+':
case '-':
priority = 2;
break;
case '(':
priority = 1;
break;
case '$':
priority = 0;
break;
default:
//default operand
priority = OPERAND;
break;
}
return priority;
}

char *inTopost(char *infix)
{
int i = 0;
char x;
//init postfix
char *postfix = (char*)malloc( SIZE *sizeof(char));
//init stack
stack *s = createstack(SIZE);

//push $ as starting char
push(s,'$');

//postfix counter
int j = 0;
//iterate over infix
for (i = 0; i < strlen(infix); i++){

//get infix charcter
   char in =  infix[i];

   printf("%d.Character:%c\n",i+1,in);

   //get top of stack character
   char tos = s->values[s->top];

   //check infix character

   //if input character is '(' push it
   if(in=='(')
   {
    push(s,in);
   }

   //if input character is ')' then pop all character till ')'
   else if(in== ')')
   {
    while((x=pop(s))!='(')
{
    postfix[j++]=x;
}
   }
   //if infix character priority=-1 then it is operand
else if(priority(in)==OPERAND)
{
//if operand to directly move to postfix
postfix[j++]= in;
}

//else check priority of stack character and infix character
else if(priority(in) > priority(tos)){

//if priority of infix is greater then or equal to infix then push character to stack
push(s,in);
}

//else pop all the from stack till infix priority is less or equal than stack
//and push infix character
else{
while(priority(in) <= priority(tos))
{
postfix[j++] = pop(s);
tos = s->values[s->top];
}
push(s,in);
}
printf("  %-27s",postfix);
display(s);
printf("\n\n");
}

//pop all stack character till its not '$'
while((x=pop(s))!='$')
{
      postfix[j++]=x;
}

// return postfix string
return postfix;
}

int main()
{
//initialize array
char *infix;

//read value
infix = "(A+B)*D+E/(F+G*D)+C";
//scanf("%s",infix);

//convert to postfix
char *postfix = inTopost(infix);

//print postfix value
printf("%s\n", postfix);
return 0;
}

Thursday, 21 July 2016