Fds Program
Fds Program
Fds Program
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int a,b,c;
c=0;
scanf("%d",&a);
scanf("%d",&b);
c=a+b;
getch();
OUTPUT :
the sum is 30
PROGRAM:2
//CIRCUMFERENCE AND AREA OF CIRCLE
#include<stdio.h>
int main()
int rad;
scanf("%d", &rad);
ci = 2 * PI * rad;
return (0);
OUTPUT:
Circumference : 6.28
PROGRAM –3
#include<conio.h>
void main()
int a,b;
clrscr();
scanf("%d",&a);
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf(" swapped!!!");
printf("a=%d",a);
printf("b=%d",b);
getch();
OUTPUT:
enter 2 numbers to swap
enter number a
10
Enter number b
20
swapped! ! ! 20 10
PROGRAM 4:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
scanf("%d%d%d",&a,&b,&c);
if (a>b&&a>c)
printf("\n a is greatest");
else if(b>c)
printf("\n b is greatest");
else
printf("\n c is greatest");
getch();
}
OUTPUT:
c is greatest
PROGRAM:5
//ODD OR EVEN
#include <stdio.h>
int main()
int number;
scanf("%d", &number);
if(number % 2 == 0)
else
printf("%d is odd.", number);
return 0;
OUTPUT 1:
Enter an integer: 7
7 is odd.
OUTPUT 2:
Enter an integer:8
8 is even
PROGRAM:6
#include <stdio.h>
int main()
int year;
scanf("%d",&year);
if(year%4 == 0)
if( year%100 == 0)
if ( year%400 == 0)
else
else
return 0;
OUTPUT 1:
OUTPUT 2:
//SUM OF N NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
int i,num,sum;
clrscr();
sum=0;
printf("enter a number");
scanf("%d",&num);
for(i=0;i<=num;i++)
sum=sum+i;
getch();
}
OUTPUT:
enter a number5
the sum is 15
PROGRAM:8
//QUADRATIC ROOTS
#include<stdio.h>
#include<math.h>
int main()
float a,b,c;
float d,root1,root2;
scanf("%fx^2%fx%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d < 0)
return 0;
OUTPUT:
PROGRAM:9
//SUM OF N DIGITS
#include<stdio.h>
#include<conio.h>
void main()
int n,s,r;
clrscr();
scanf("%d",&n);
s=0;
while(n>0)
r=n%10;
s=s+r;
n=n/10;
getch();
}
OUTPUT:
enter the number22
the sum of digits is 4
PROGRAM:10
//PRIME NUMBER
#include <stdio.h>
int main()
int n, i, flag = 0;
scanf("%d", &n);
if(n%i == 0)
flag = 1;
break;
if (n == 1)
}
else
if (flag == 0)
else
return 0;
OUTPUT 1:
29 is a prime number.
OUTPUT 2:
//ARMSTRONG NUMBER
#include<stdio.h>
#include<conio.h>
void main()
int n,t,s,r;
clrscr();
scanf(“%d”, &n);
t=n;
s=0;
while(n>0)
r=n%10;
s=s+(r*r*r);
n=n/10;
if(t==s)
getch();
OUTPUT 1:
OUTPUT 2:
//PALINDROME
#include <stdio.h>
int main()
int n, reverse = 0, t;
scanf("%d", &n);
t = n;
while (t != 0)
t = t/10;
if (n == reverse)
else
return 0;
OUTPUT 1:
Enter a number to check if it is a palindrome or not
121
OUTPUT 2:
122
PROGRAM 13 :
//FACTORIAL OF A NUMBER
#include<stdio.h>
#include<conio.h>
void main()
Int i,n,fact=1;
clrscr();
scanf(“%d”,&n);
for(i=1;i<=n;i++)
fact=fact*i;
getch();
OUTPUT:
PROGRAM:14
//FIBONACCI SERIES
#include<conio.h>
#include<stdio.h>
void main()
int a=-1,b=1,c,i,n;
clrscr();
scanf("%d",&n);
for(i=0;i<+n;i++)
c=a+b;
printf("\n%d",c);
a=b;
b=c;
getch();
OUTPUT :
13
21
34
PROGRAM:15
//STRING PALINDROME
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
char s[20],a[20];
clrscr();
printf("enter a string");
scanf("%s",s);
strcpy(a,strrev(s));
if(strcmp(a,strrev(s))==0)
printf("the string is palindrome");
else
getch();
OUTPUT 1:
OUTPUT 2:
PROGRAM:16 (A)
//LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
Int i,n,a[10],se;
clrscr();
scanf(“%d”,&n);
printf(“Enter the values”);
for(i=0;i<n:i++)
scanf(“%d”,&a[i]);
printf(“Elements to be searched”);
scanf(“%d\t”,&se);
for(i=0;i<n;i++)
if(se==a[i])
break;
if(i==n)
getch();
OUTPUT 1:
Element to be searched : 60
Element to be searched : 11
PROGRAM:16 (b)
//BINARY SEARCH
#include<stdio.h>
#include<conio.h>
void main()
int a[10],i,n,se,u,l,mid,c=0;
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
l=0;
u=n-1;
while(l<=u)
mid=(l+u)/2;
if(se==a[mid])
C=1;
break;
else if(se<a[mid])
u=mid-1;
else
l=mid+1;
if(c==0)
else
printf(“Element found”);
}
getch();
OUTPUT 1:
Element found
OUTPUT 2:
//SORTING
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n,temp,a[20];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d",a[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<n;i++)
printf("%d \t",a[i]);
getch();
OUTPUT:
13 7 42 98 35 63
your sorted array is 7 13 35 42 63 98
PROGRAM:18
//MATRIX ADDITION
#include<stdio.h>
#include<conio.h>
void main()
int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j;
clrscr();
scanf("%d %d",&r1,&c1);
scanf("%d %d",&r2,&c2);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
for (i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
printf("%d \t",a[i][j]);
printf("\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for (i=0;i<r2;i++)
for(j=0;j<c2;j++)
printf("%d \t",b[i][j]);
printf("\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
}
for (i=0;i<r1;i++)
for(j=0;j<c1;j++)
printf("%d \t",c[i][j]);
printf("\n");
getch();
OUTPUT:
enter the rows and columns of first matix2
2
enter the rows and columns of second matrix2
2
enter the elements of the first matrix1
2
3
4
enter the elements of the second matrix5
6
2
1
the final matrix is --->
6 8
5 5
PROGRAM:19
//MATRIX MULTIPLICATION
#include<stdio.h>
#include<conio.h>
void main()
int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;
clrscr();
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
if(c1!=r2)
else
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}}
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
printf("\n");
getch();
OUTPUT 1:
OUTPUT 2:
enter the rows and columns of matrix a and b 3333
resultant matrix is
6 6 6
6 6 6
6 6 6
PROGRAM:21
//CALL BY VALUE
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
scanf("%d %d",&i,&j);
printf("inside the main function before swapping \n");
printf("X=%d Y=%d",i,j);
swap(i,j);
printf("X=%d Y=%d",i,j);
getch();
int t;
t=x;
x=y;
y=t;
OUTPUT:
get values of i&j10
20
inside the main function before swapping x=10 y=20
swapped values x=20,y=10
inside main function after swapping x=10 y=20
PROGRAM:22
//CALL BY REFERENCE
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
scanf("%d %d",&i,&j);
swap(&i,&j);
getch();
int t;
t=*x;
*x=*y;
*y=t;
OUTPUT:
get values of i&j10
32
inside the main function before swapping x=10 y=32
swapped values x=32,y=10
inside main function after swapping x=32 y=10
PROGRAM:23
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
int num,f;
scanf("%d",&num);
f=fact(num);
printf("\nfactorial of %d is %d",num,f);
getch();
int fact(int n)
if(n==1)
return 1;
else
return(n*fact(n-1));
OUTPUT:
enter a number:6
factorial of 6 is:720
PROGRAM:24
#include<stdio.h>
#include<conio.h>
struct student
int rno,m1,m2,m3,age;
float avg;
char name[20],dept[10];
};
void main()
struct student s;
scanf("%d", &s.rno);
scanf("%s", &s.name);
scanf("%s", &s.age);
scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
s.avg=(s.m1+s.m2+s.m3)/3;
getch();
OUTPUT:
12
karthik
CSE
20
40
18
90
//UNION-BOOK DETAILS
#include<stdio.h>
#include<conio.h>
union book
char name[20];
int bookno;
float price;
void main()
scanf("%s",&b1.name);
scanf("%s",&b1.bookno);
scanf("%f",&b1," price\n");
getch();
}
OUTPUT:
enter the book details
enter the book name
wings of fire
the book name is wings of fire
enter the bookno
22
the bookno is 22
enter the book price
250
the book price is 250
DATA STRUCTURES
PROGRAM NO : 1(a)
//array implementation of list
#include<stdio.h>
#include<conio.h>
int a[20],n,i,j,ch,elem,pos;
void create();
void insert();
void del();
void display();
void insertend();
void insertanypos();
void delend();
void delanypos();
void delanyele();
void main()
{
clrscr();
do
{
printf("\n mainmenu");
printf("\n *********");
printf("\n 1.create");
printf("\n 2.insert");
printf("\n 3.del");
printf("\n 4.display");
printf("\n 5.exit");
printf("\n enter the choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{create();
break;}
case 2:
{insert();
break;}
case 3:
{del();
break;}
case 4:
{display();
break;}
case 5:
printf("\n end");
getch();
break;
}}
while(ch<5);
getch();
}
void create()
{printf("\n enter the size of the list:\t");
scanf("%d",&n);
printf("enter the elements in the list:\t");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display()
{printf("\n the elements in the list are:\t");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void insert()
{clrscr();
do
{
printf("\n 1.insert at the end");
printf("\n 2.insert at any pos");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertend();
break;
case 2:
insertanypos();
break;
}}
while(ch<3);
}
void del()
{clrscr();
do
{
printf("\n 1.delete at the end");
printf("\n 2.delete at any pos");
printf("\n 3.delete any given element");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
delend();
break;
case 2:
delanypos();
break;
case 3:
delanyele();
break;
}}
while(ch<4);
}
void insertend()
{
n=n+1;
printf("\n enter the element to be inserted:\t");
scanf("%d",&elem);
a[n-1]=elem;
display();
}
void insertanypos()
{
n=n+1;
printf("\n enter the position to be inserted:\t");
scanf("%d",&pos);
printf("\n enter the element to be inserted:\t");
scanf("%d",&elem);
for(j=n-1;j>=pos-1;j--)
a[j]=a[j-1];
a[pos-1]=elem;
display();
}
void delend()
{
elem=a[n-1];
printf("\n the delete element is %d",elem);
n=n-1;
display();
}
void delanypos()
{printf("\n enter the position to be deleted:\t");
scanf("%d",&pos);
printf("\n the deleted element is%d",a[pos-1]);
for(j=pos-1;j<n;j++)
a[j]=a[j+1];
n=n-1;
display();
}
void delanyele()
{
display();
printf("\n enter the element to be deleted:\t");
scanf("%d",&elem);
for(i=0;i<n;i++)
{
if(a[i]==elem)
{
pos=1;
for(j=pos;j<n;j++)
a[j]=a[j+1];
}}
n=n-1;
display();
}
OUTPUT :
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:1
Enter the size of the list:3
Enter the elements in the list: 5 6 9
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:2
1.insert at the end
2.insert at any pos
enter the choice:1
enter the elements to be inserted 7
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:4
the elements in the list are 5 6 9 7
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:3
1.delete at end
2.delete at any pos
3.delete any given element
enter the chioce:2
enter the position to be deleted:3
the deleted element is 9
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:4
the elements in the list are 5 6 7
main menu
***********
1.create
2.insert
3.delete
4.display
5.exit
Enter the choice:5
end
Main menu
**********
• Creation
• Modification
• Deletion
• Display
• Exit
Enter your choice : 3
• Delete at the beginning
• Delete at the mid
• Delete at the end
• Enter your choice: 3
After deletion: 10 15
Main menu
**********
• Creation
• Modification
• Deletion
• Display
• Exit
Enter your choice : 4
10 15
Main menu
*********
• Creation
• Modification
• Deletion
• Display
• Exit
Enter your choice : 5
PROGRAM N0 : 2(a)
//array implementation of stack ADT
#include<stdio.h>
#include<stdio.h>
#define SIZE 100
void main()
{
int top=-1,i,stack[SIZE],n,ch,item;
clrscr();
printf("enter the size of the stack:\n");
scanf("%d",&n);
do
{
printf("\n main menu\n");
printf("*********");
printf("\n1.push");
printf("\n2.pop");
printf("\n3.display");
printf("\n4.exit");
printf("\nenter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(top>=n-1)
{
printf("\n stack overflow");
getch();
break;
}
printf("\n enter the item:\t");
scanf("%d",&item);
top++;
stack[top]=item;
break;
case 2:
if(top<0)
{
printf("\n stack underflow");
getch();
break;
}
item=stack[top];
top--;
printf("the popped element is%d",item);
getch();
break;
case 3:
if(top<0)
{
printf("\nstack empty!");
getch();
break;
}
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
getch();
break;
case 4:
printf("\nbye");
getch();
break;
}
}
while(ch<4);
}
OUTPUT :
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 12
Stack after pushing an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 65
Stack after pushing an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 76
Stack after pushing an element
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 3
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
stack after poping an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after poping an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 12
Stack after pushing an element 12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after popng an element
Stack is empty
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 4
Exiting !!!!
PROGRAM NO :2(B)
//linked list implementation of list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*head=NULL;
typedef struct node NODE;
void create();
void display();
void modify();
void del();
void main()
{
int ch;
clrscr();
do
{
clrscr();
printf("main menu\n");
printf("*********");
printf("\n 1.creation");
printf("\n 2.modification");
printf("\n 3.deletion");
printf("\n 4.display");
printf("\n 5.exit");
printf("\n enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{create();
break;}
case 2:
{modify();
break;}
case 3:
{del();
break;}
case 4:
{display();
break;}
case 5:
printf("\nbye");
getch();
break;
}
getch();
}
while(ch<5);
}
void create()
{
NODE*temp,*t,*prev;
int i,j,ch,d,pos,n;
printf("\n enter the size of elements:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
clrscr();
printf("\n enter the elements:\t");
scanf("%d",&d);
temp=(NODE*)malloc(sizeof(NODE));
temp->next=NULL;
temp->info=d;
if(head==NULL)
head=temp;
else
{
printf("\n 1.insert at the beginning");
printf("\n 2.insert at the mid");
printf("\n 3.insert at the end");
printf("\n enter the choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
temp->next=head;
head=temp;
break;
}
case 2:
{
t=head;
printf("\n enter the position to insert:\t");
scanf("%d",&pos);
for(j=1;j<pos;j++)
{
prev=t;
t=t->next;
}
temp->next=prev->next;
prev->next=temp;
break;
}
case 3:
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next=temp;
break;
}
}
printf("\n the current list:\t");
display();
}
}
}
void modify()
{
NODE *t,*prev;
int old,neww;
t=head;
printf("\n the current list:\t");
display();
printf("\n enter the item to be modified:\t");
scanf("%d",&old);
while(t!=NULL)
{
if(t->info==old)
{
printf("\n enter the new value:\t");
scanf("%d",&neww);
t->info=neww;
}
else
{
t=t->next;
}
printf("\n no match found");
printf("\n after mofification");
display();
}
}
NODE*t,*prev;
int pos,ch,i;
t=head;
printf("\n delete at the beginning");
printf("\n delete at mid");
printf("\n delete at end");
printf("\n enter the choice");
printf("%d",&ch);
t=head;
switch(ch)
{
case 1:
{
head=head->next;
free(t);
break;
}
case 2:
{
printf("\n enter he position to delete\t");
scanf("%d",&pos);
for(i=1;i<pos;i++)
{
prev=t;
t=t->next;
}
prev->next=t->next;
free(t);
break;
}
case 3:
{
while(t->next!=NULL)
{
prev=t;
t=t->next;
}
prev->next=NULL;
free(t);
}
}
printf("\n after deletion\t");
display();
}
void display()
{
NODE*t;
t=head;
if(t==NULL)
printf("\n list is empty");
else
while(t->next!=NULL)
{
printf("%d\t",t->info);
t=t->next;
}
}
OUTPUT:
Main menu
1.push
2.pop
3.display
4.edit
Enter your choice : 1
Enter the elements to be pushed : 12
Stack after pushing an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 65
Stack after pushing an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 76
Stack after pushing an element
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 3
76
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
stack after poping an element
65
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after poping an element
12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 1
Enter the elements to be pushed : 12
Stack after pushing an element 12
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 2
Stack after popng an element
Stack is empty
Main menu
****************
1.push
2.pop
3.display
4.exit
Enter your choice: 4
Exiting !!!!
OUTPUT:
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Enter the item 2
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Enter the item 3
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Overflow
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 2
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 3
2
Main menu
****************
1.insert
2.delete
3.display
4.exit
Enter your choice: 4
Exiting
PROGRAM NO : 3(B)
//linked list implementation of queue
#include<stdio.h>
#include<conio.h>
#inlcude<stdlib.h>
int d;
struct queue
{
struct queue*next;
}*front=NULL*rear=NULL;
typedef struct queue q;
void create();
void display();
void del();
void main()
{
int ch;
clrscr();
do
{
printf("main menu\n");
printf("************");
printf("\n 1.insert");
printf("\n 2.deletion");
printf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice :\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
printf("\n exit");
break;
}
}
while (ch<4);
getch();
}
void create ()
{
q*temp,*t;
printf("\n enter the data:\t");
scanf("%d",&d);
temp=(q*)malloc(sizeof(q));
temp->data=d;
temp->next=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
void del()
{
q*t;
if(front==NULL)
printf("underflow");
else
t=front;
front=front->next;
}
void display()
{
q*t;
if(front==NULL)
printf("\n queue is underflow");
else
{
t=front;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
}
}
Output :
Main menu
**********
1.insert
2.delete
3.display
4.exit
1
Enter the data 1
Main menu
**********
1.insert
2.delete
3.display
4.exit
1
Enter the data 2
Main menu
**********
1.insert
2.delete
3.display
4.exit
1
Enter the data 3
Main menu
**********
1.insert
2.delete
3.display
4.exit
2
Main menu
**********
1.insert
2.delete
3.display
4.exit
3
23
Main menu
**********
1.insert
2.delete
3.display
4.exit
4
Exit
PROGRAM NO: (4)
//infix to postfix conversion
#include<stdio.h>
#include<conio.h>
charinf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
int main()
{
clrscr();
printf("\n enter the infix expression:");
scanf("%s",&inf);
postfix();
getch();
return 0;
}
void postfix()
{
inti,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while (st[top]>=1)
post[j++]=pop();
push(2);
break;
case'*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case'^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\n the evaluated postfix expression is :%s",post);
}
void push(int element)
{
top++;
st[top]=element;
}
char pop()
{
int e1;
char e;
e1=st[top];
top--;
switch(e1)
{
case 1:e='+';
break;
case 2:e='-';
break;
case 3:e='*';
break;
case 4:e='/';
break;
case 5:e='^';
break;
}
return e;
}
OUTPUT:
enter the infix expression:3-6(6+8/5*7)+8
the evaluated postfix expression is :36685/7*+-8+
PROGRAM NO :5
//TREE TRAVERSAL
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
void preorder(struct node* root){
if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root) {
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}
struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node *root, int value) {
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node *root, int value){
root->right = createNode(value);
return root->right;
}
int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
OUTPUT:
Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->
PROGRAM NO :6
//BINARY SEARCH TREE
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node*rlink;
struct node*llink;
}*tmp=NULL;
typedefstruct node NODE;
NODE*create();
void insert();
void preorder(NODE*);
voidinorder(NODE*);
voidpostorder(NODE*);
void del();
void main()
{
intn,i,m,key;
//struct node *tmp=NULL;
clrscr();
do
{
printf("\n\n0.create\n\n1.insert\n\n2.preorder\n\n3.postorder\n\n4.inorder\n\n5.exit\
n6.Delete\n");
printf("\n\nenterur choice");
scanf("%d",&m);
switch(m)
{
case 0:
tmp=create();
break;
case 1:
insert (tmp);
break;
case 2:
printf("\n\n display tree in preorde traversal\n\n");
preorder(tmp);
break;
case 3:
printf("\n\n display tree in postorder\n\n");
postorder(tmp);
break;
case 4:
printf("\n\n inorder\n\n");
inorder(tmp);
break;
case 5:
exit(0);
case 6:
{
del();break;
}
} }
while(n!=5);
getch();
}
void insert(NODE*root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE*create()
{
NODE*newnode;
int n;
newnode=(NODE*)malloc(sizeof(NODE));
printf("\n\nenter the data");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
voidpostorder(NODE*tmp)
{
if(tmp!=NULL)
{
postorder(tmp->llink);
postorder(tmp->rlink);
printf("%d->",tmp->data);
}
}
voidinorder(NODE*tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d->",tmp->data);
inorder(tmp->rlink);
}
}
void preorder(NODE*tmp)
{
if(tmp!=NULL)
{
printf("%d->",tmp->data);
preorder(tmp->llink);
preorder(tmp->rlink);
}
}
void del()
{
NODE *temp,*prev,*curr,*prev2;
intele;
printf("enter the numbers to be deleted:");
scanf("%d",&ele);
temp=tmp;
while(temp->data!=ele)
{
if(ele<=temp->data)
{
prev=temp;
temp=temp->llink;
}
else
{
prev=temp;
temp=temp->rlink;
}
}
printf("element identified %d",temp->data);
if(temp->llink==NULL && temp->rlink== NULL)
{
puts("0node");
if(temp==prev->rlink)
prev->rlink=NULL;
else
prev->llink=NULL;
}
else if((temp->llink!=NULL)&&(temp->rlink!=NULL))
{
puts("2nodes");
curr=temp->rlink;
while(curr->llink!=NULL)
{
prev2=curr;
curr=curr->llink;
}
if(temp==prev->rlink)
prev->rlink = curr;
else
prev->rlink=curr;
prev2->llink=NULL;
}
else
{
puts("1 node");
printf("%d",prev->data);
if(prev->rlink==temp)
{
if(temp->llink==NULL)
{puts("tar is right child");
prev->rlink=temp->rlink; }
else
prev->rlink=temp->llink;
}
else
{
if (temp->llink==NULL)
prev->llink=temp->rlink;
else
prev->llink=temp->llink;
}
}
}
OUTPUT :
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
enter ur choice 0
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
enter ur choice 1
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
enter ur choice 1
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
enter ur choice 2
6->34->27->
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
enter ur choice 3
display tree in postorder
27->34->6->
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
enter ur choice 4
inorder
6->27->34->
0.create
1.insert
2.preorder
3.postorder
4.inorder
5.exit
6.delete
PROGRAM : 7(A)
//INSERTION-SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,temp;
clrscr();
printf("Get the size of the array");
scanf("%d",&n);
printf("Enter the elements in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
OUTPUT:
Get the size of the array5
Enter the elements in the array2
7
5
1
9
The sorted array is
1
2
5
7
9
PROGRAM NO : 7(B)
//BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,temp,a[20],n;
clrscr();
printf(“get the size of the array”);
scanf(“%d”,&n);
printf(“get the element one by one “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;I,=n-1;i++)
{
temp=a[i];
for(j=I;j>=1;j--)
{
if(temp<a[j-1])
a[j]=a[j-1];
else
break;
}
a[j]=temp;
}
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
getch();
}
OUTPUT:
get the size of array
6
get the elements one by one
28
91
12
54
33
26
12 26 28 33 54 91
PROGRAM NO : 8(A)
//quick sort
#include<stdio.h>
#include<conio.h>
int i,j,n,pivot,a[20];
void quick(int a[],int left,int right);
void swap(int a[],int i,int j);
void main ()
{
int i,n,a[20];
clrscr();
printf(“enter the limit:”);
scanf(“%d”,&n);
printf(“enter the elements:”);
for(i=0;i<n;i++);
scanf(“%d”,a[i]);
quick(a,0,n-1);
printf(“the sorted list is:”);
for(i=0;i<n;i++)
printf(“\n %d “,a[i]);
getch();
}
void quick(int a[],int first,int last)
{
if (first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i<last)
i++;
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
OUTPUT:
OUTPUT:
PROGRAM NO: 9
//hashing
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
int create(int);
voidlinear_probing(int[],int,int);
void display(int[]);
void main()
{
int a[max],num,key,i;
charans;
clrscr();
printf("\nCollision handling by linear probing\n");
for(i=0;i<max;i++)
a[i]=-1;
do
{
printf("\nEnter the next element in the list:");
scanf("%d",&num);
key=create(num);
linear_probing(a,key,num);
printf("\nDo you want to continue?(y/n)");
scanf("%s",&ans);
}
while(ans=='y'||ans=='y');
if(ans=='n')
display(a);
getch();
}
int create(intnum)
{
int key=num%max;
return key;
}
voidlinear_probing(int a[max],intkey,intnum)
{
int probe;
if(a[key]==-1)
{
a[key]=num;
probe=-1;
}
else
{
if(key==max-1)
probe=0;
else
probe=key+1;
}
while((probe!=-1)&&(probe!=key))
{
if(a[probe]==-1)
{
a[probe]=num;
probe=-1;
}
else
{
if(probe==max-1)
probe=0;
else
probe++;
}
}
if(probe==-1)
printf("\nElements successfully added...");
else
printf("\nHash table is full...");
}
void display(int a[max])
{
int i;
printf("\nThe hash table is:\n");
for(i=0;i<max;i++)
printf("\na[%d]\t%d",i,a[i]);
}
OUTPUT:
collision handling by linear probing
enter the next element in the list:1
elements successfully added…..
Do you want to continue? (y/n) y
Enter the next element in the list:17
Elements successfully added…
Do you want to continue? (y/n) y
Enter the next element in the list:35
Elements successfully added…..
Do you want to continue? (y/n) y
Enter the next element in the list:87
Elements successfully added…
Do you want to continue? (y/n) y
Enter the next element in the list:90
Elements successfully added…
Do you want to continue? (y/n) y
Enter the next element in the list:66
Elements successfully added…
Do you want to continue? (y/n) n
The hash table is:
a[0] 90
a[1] 1
a[2] -1
a[3] -1
a[4] -1
a[5] 35
a[6] 66
a[7] 17
a[8] 87
a[9] -1