Tuesday, 31 January 2017

Evaluating Postfix expression using stack : C program

#include<stdio.h>
#define MAX 50
int stack[MAX];
int top=-1;
void push(int item)
{
if(top == MAX-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=item;
}

int pop()
{
if(top == -1)
{
printf("stack empty");
return;
}
int temp;
temp=stack[top];
top=top-1;
return temp;
}
main()
{
char ch;
int val,v3,v2,v1,i;
printf("enter the postfix expression\n At the end type #");
fflush(stdin);
scanf("%c",&ch);
while(ch != '#')
{
if(ch=='a' || ch == 'b'|| ch == 'c')
{
printf("enter the value of %c",ch);
scanf("%d",&val);
push(val);
}

if(ch == '+'||ch == '-'||ch == '*'||ch == '/')
{
v1=pop();
v2=pop();
switch(ch)
{
case '+': v3=v2+v1;
break;
case '-': v3=v2-v1;
break;
case '*': v3=v2*v1;
break;
case '/': v3=v2/v1;
break;
}
push(v3);
}
fflush(stdin);
scanf("%c",&ch);
}
val=pop();
printf("\nresultant is %d",val);
}





Infix to Postfix Conversion using stacks using Array. ONLY FOR EXPRESSIONS WITHOUT BRACKETS : C program

#include<stdio.h>
#define MAX 50
char stack[MAX];
int top=-1;

int preced(char ch)
{
 switch(ch)
 {
  case '+':
  case '-':
     return 1;
     break;
  case '*': 
  case '/':
     return 2;
     break;
  case '#':
     return 0;
     break;
  default:
     return 3;
 }
}

void push(char item)
{
 if(top == MAX-1)
 {
  printf("stack is full");
  return;
 }
 top=top+1;
 stack[top]=item;
}

void pop()
{
 if(top == -1)
 {
  printf("stack empty");
  return;
 }
 printf("%c",stack[top]);
 top=top-1;
}
main()
{
char ch;
push('#');
printf("enter the given infix expression\n At the end type #");
fflush(stdin);
scanf("%c",&ch);
while(ch != '#')
{
 while(preced(stack[top])>=preced(ch))
 {
  pop();
 }
 push(ch);
 scanf("%c",&ch);
}
while(stack[top]!= '#')
 pop();
}


Monday, 30 January 2017

Implementing Stack using Linked List

#include <stdio.h>
#include <stdlib.h>
struct node
{
 int data;
 struct node *next;
};
struct node *top=NULL,*c,*p;
push()
{
 int val;
 printf("\nenter a value to insert into stack\n");
 scanf("%d",&val);
 struct node * newnode=malloc(sizeof(struct node));
 newnode->data=val;
 newnode->next = NULL;
 if(top == NULL)
  top = newnode;
 else
 {
  newnode->next=top;
  top=newnode;
 }
}
pop()
{
 if(top == NULL)
 {
  printf("\n Stack is EMPTY..Deletion is not possible");
  return;
 }
 printf("\n Deleted element = %d\n",top->data);
 c=top;
 top=top->next;
 free(c);
}
display()
{
 if(top == NULL)
 {
  printf("stack is empty");
  return;
 }
 else
 {
  for(c=top;c!=NULL;c=c->next)
   printf("%d\t",c->data);
 
 }
}
main()
{
 int opt;
  while(1)
 {
  printf("\nPress 1. Push\t 2. Pop\t3. Display \t4. Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}
 }

}

Sum of array elements using recursion in C

What is a recursive solution to summing up a list of numbers? First you should note that the sum of [2 13 4 25 66 71 82 91]) is equal to 2 + sum of [ 13 4 25 66 71 82 91]

#include <stdio.h>
int sum(int *a,int index,int n)
{
if(index == n)
return 0;
return(a[index]+sum(a,index+1,n));
}
main()
{
int n,i,a[100];
printf("enter size of array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array sum =%d",sum(a,0,n));
}

(OR)

#include <stdio.h>
int sum(int *a,int n)
{
if(n<=0)
return 0;
return(*a+sum(++a,--n));
}
main()
{
int n,i,a[100];
printf("enter size of array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array sum =%d",sum(a,n));
}


Write a C program to generate fibonacci series upto n terms using recursion
#include <stdio.h>
int fib(int n)
{
if(n == 0 || n ==1)
return n;
return(fib(n-1)+fib(n-2));
}
main()
{
int n,i;
printf("enter number of terms\n");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\n %d\t",fib(i));
}

Reversing every word in a given line of text: C program

Reversing every word in a given line of text
Program:
#include <stdio.h>
#include <string.h>
main()
{
char a[100],*token,st[10][50];
int i=0,j,l;
puts("enter line of text");
gets(a);
token = strtok(a," ");
while(token != NULL)
{
 strcpy(st[i],token);
 token=strtok(NULL," ");
 i++;
}
l=--i;
for(j=0;j<i;j++,i--)
{
     strcpy(a,st[j]);
     strcpy(st[j],st[i]);
     strcpy(st[i],a);
}
for(j=0;j<=l;j++)
 printf("\n%s",st[j]);

}

Sunday, 29 January 2017

Recursive solution to Total number of sequences : C Program with Memoization

Write a recursive function to Find the total number of sequences of length n (using H and T) such that no two Hs are next to each other.

#include <stdio.h>
int a[20];
int total_seq(int n)
{
if(n == 1)
return 2;
if(n ==2)
return 3;
if(a[n]!=-1 )
return a[n];
a[n]=total_seq(n-1)+total_seq(n-2);
return a[n];
}
main()
{
int n,i;
printf("enter n value");
scanf("%d",&n);
for(i=0;i<20;i++)
a[i]=-1;
printf("\n Total number of sequences = %d",total_seq(n));
}