1.Write a program for creating "Binary Search Tree(BST)" in 'C'
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct tree
{
int info;
struct tree *left,*right;
};
void create(struct tree **p,int input[],int n);
void inorder(struct tree *p);
void preorder(struct tree *p);
void postorder(struct tree *p);
void main()
{
int i,input[20],ch,n;
struct tree *root;
clrscr();
root=NULL;
printf("\nHow many nodes u want in tree(MAX=20)?\t");
scanf("%d",&n);
printf("\nEnter %d element for creating for B.S.T:",n);
for(i=0;i<n;i++)
scanf("%d",&input[i]);
create(&root,input,n);
printf("\nThe Preorder Traversal Of B.S.T.:");
preorder(root);
printf("\nThe postorder Traversal of B.S.T.:");
postorder(root);
printf("\nThe inorder Traversal of B.S.T.:");
inorder(root);
getch();
}
void create(struct tree **root,int input[],int n)
{
int i;
struct tree *temp,*newnode,*prev;
newnode=(struct tree*)malloc(sizeof(struct tree));
newnode->info=input[0];
newnode->left=NULL;
newnode->right=NULL;
*root=newnode;
for(i=1;i<n;i++)
{
temp=*root;
prev=NULL;
while(temp != NULL)
{
prev = temp;
if(temp->info>input[i])
temp=temp->left;
else
temp=temp->right;
}
newnode=(struct tree*)malloc(sizeof(struct tree));
newnode->info=input[i];
newnode->left=NULL;
newnode->right=NULL;
if(input[i]<prev->info)
prev->left=newnode;
else
prev->right=newnode;
}
}
void inorder(struct tree *p)
{
if(p != NULL)
{
inorder(p->left);
printf("\t%d",p->info);
inorder(p->right);
}
}
void preorder(struct tree *p)
{
if(p != NULL)
{
printf("\t%d",p->info);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct tree *p)
{
if(p != NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t%d",p->info);
}
}
----------------------------------------------------------------------------------------------------------
2. Write a program to demonstrate "Bubble Sort".
#include<stdio.h>
#include<conio.h>
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
int Bubblesort(int a[],int n)
{
int i,j,flag,temp;
i=1;flag=1;
while(i<n && flag) //while gives no of passes
{
j=0;
flag=0;
while(j<=n-i-1)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
} //end of if
j++;
}//end of j loop
printf("\nArray after pass %d\t",i);
print(a,n);
i++;
}//end of i loop
return (i-1);
}//function end
void main()
{
int a[50],i,n,m;
clrscr();
printf("\nHow many elements which you want to enter :");
scanf("%d",&n);
printf("\nEnter elements in array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe original array is :-");
print(a,n);
m=Bubblesort(a,n);
printf("\nNo of passes :-%d",m);
getch();
}
-----------------------------------------------------------------------------------------------------------
3.Write a program to INSERT & DELETE an element from a SPECIFIED position.
#include<stdio.h>
#include<conio.h>
void print(int a[],int n);
int insert(int a[],int n,int e,int l);
int del(int a[],int n,int l);
void main()
{
int a[50],i,n,ch,e,l;
clrscr();
printf("\nHow many elements you want to enter in array?");
scanf("%d",&n);
printf("\nEnter %d element in array:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
do
{
printf("\n\t*** Menu ***\n");
printf("\n\t1.\tPrint\n\t2.\tInsert\n\t3.\tDelete\n\t4.\tExit");
printf("\nEnter your choice to perform?");
scanf("%d",&ch);
switch(ch)
{
case 1:
print(a,n);
break;
case 2:
printf("\nEnter element which you want to enter:");
scanf("%d",&e);
printf("\nEnter location of that where you want to place it:");
scanf("%d",&l);
n=insert(a,n,e,l);
break;
case 3:
printf("\nEnter location of the element which you want to delete:");
scanf("%d",&l);
n=del(a,n,l);
break;
case 4:
exit();
default:
printf("\nWARNING:ENTER ORIGINAL CHOICE.");
}
}while(ch != 4);
getch();
}
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
int insert(int a[],int n,int e,int l)
{
int i;
if(n<50)
{
for(i=n-1;i>=0;i--)
{
if(l <= i)
a[i+1]=a[i];
}
a[l]=e;
n++; // This is for traversal
}
return n;
}
int del(int a[],int n,int l)
{
int i;
if(n>0 )
{
if(l >= n)
printf("\nEntered location is not present in array.");
else
{
for(i=0; i<n ;i++)
if(i >= l)
a[i]=a[i+1];
n--;
}
}
return n;
}
----------------------------------------------------------------------------------------------------------
4. Write a program to search an element with the "Linear Search Method".
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2];
int i,j;
int src,flg,x,y;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nEnter element for a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter a no for search :");
scanf("%d",&src);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(a[i][j]==src)
{
flg=1;
x=i;
y=j;
break;
}
else
flg=0;
}
}
if(flg==1)
{
printf("\nEntered no is found.");
printf("\nIt is at position:a[%d][%d]",x,y);
}
else if(flg==0)
printf("\nEntered no not found.");
getch();
}
Data Structure(DS) programs
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct tree
{
int info;
struct tree *left,*right;
};
void create(struct tree **p,int input[],int n);
void inorder(struct tree *p);
void preorder(struct tree *p);
void postorder(struct tree *p);
void main()
{
int i,input[20],ch,n;
struct tree *root;
clrscr();
root=NULL;
printf("\nHow many nodes u want in tree(MAX=20)?\t");
scanf("%d",&n);
printf("\nEnter %d element for creating for B.S.T:",n);
for(i=0;i<n;i++)
scanf("%d",&input[i]);
create(&root,input,n);
printf("\nThe Preorder Traversal Of B.S.T.:");
preorder(root);
printf("\nThe postorder Traversal of B.S.T.:");
postorder(root);
printf("\nThe inorder Traversal of B.S.T.:");
inorder(root);
getch();
}
void create(struct tree **root,int input[],int n)
{
int i;
struct tree *temp,*newnode,*prev;
newnode=(struct tree*)malloc(sizeof(struct tree));
newnode->info=input[0];
newnode->left=NULL;
newnode->right=NULL;
*root=newnode;
for(i=1;i<n;i++)
{
temp=*root;
prev=NULL;
while(temp != NULL)
{
prev = temp;
if(temp->info>input[i])
temp=temp->left;
else
temp=temp->right;
}
newnode=(struct tree*)malloc(sizeof(struct tree));
newnode->info=input[i];
newnode->left=NULL;
newnode->right=NULL;
if(input[i]<prev->info)
prev->left=newnode;
else
prev->right=newnode;
}
}
void inorder(struct tree *p)
{
if(p != NULL)
{
inorder(p->left);
printf("\t%d",p->info);
inorder(p->right);
}
}
void preorder(struct tree *p)
{
if(p != NULL)
{
printf("\t%d",p->info);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct tree *p)
{
if(p != NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t%d",p->info);
}
}
----------------------------------------------------------------------------------------------------------
2. Write a program to demonstrate "Bubble Sort".
#include<stdio.h>
#include<conio.h>
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
int Bubblesort(int a[],int n)
{
int i,j,flag,temp;
i=1;flag=1;
while(i<n && flag) //while gives no of passes
{
j=0;
flag=0;
while(j<=n-i-1)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
} //end of if
j++;
}//end of j loop
printf("\nArray after pass %d\t",i);
print(a,n);
i++;
}//end of i loop
return (i-1);
}//function end
void main()
{
int a[50],i,n,m;
clrscr();
printf("\nHow many elements which you want to enter :");
scanf("%d",&n);
printf("\nEnter elements in array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe original array is :-");
print(a,n);
m=Bubblesort(a,n);
printf("\nNo of passes :-%d",m);
getch();
}
-----------------------------------------------------------------------------------------------------------
3.Write a program to INSERT & DELETE an element from a SPECIFIED position.
#include<stdio.h>
#include<conio.h>
void print(int a[],int n);
int insert(int a[],int n,int e,int l);
int del(int a[],int n,int l);
void main()
{
int a[50],i,n,ch,e,l;
clrscr();
printf("\nHow many elements you want to enter in array?");
scanf("%d",&n);
printf("\nEnter %d element in array:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
do
{
printf("\n\t*** Menu ***\n");
printf("\n\t1.\tPrint\n\t2.\tInsert\n\t3.\tDelete\n\t4.\tExit");
printf("\nEnter your choice to perform?");
scanf("%d",&ch);
switch(ch)
{
case 1:
print(a,n);
break;
case 2:
printf("\nEnter element which you want to enter:");
scanf("%d",&e);
printf("\nEnter location of that where you want to place it:");
scanf("%d",&l);
n=insert(a,n,e,l);
break;
case 3:
printf("\nEnter location of the element which you want to delete:");
scanf("%d",&l);
n=del(a,n,l);
break;
case 4:
exit();
default:
printf("\nWARNING:ENTER ORIGINAL CHOICE.");
}
}while(ch != 4);
getch();
}
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
int insert(int a[],int n,int e,int l)
{
int i;
if(n<50)
{
for(i=n-1;i>=0;i--)
{
if(l <= i)
a[i+1]=a[i];
}
a[l]=e;
n++; // This is for traversal
}
return n;
}
int del(int a[],int n,int l)
{
int i;
if(n>0 )
{
if(l >= n)
printf("\nEntered location is not present in array.");
else
{
for(i=0; i<n ;i++)
if(i >= l)
a[i]=a[i+1];
n--;
}
}
return n;
}
----------------------------------------------------------------------------------------------------------
4. Write a program to search an element with the "Linear Search Method".
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2];
int i,j;
int src,flg,x,y;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\nEnter element for a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter a no for search :");
scanf("%d",&src);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(a[i][j]==src)
{
flg=1;
x=i;
y=j;
break;
}
else
flg=0;
}
}
if(flg==1)
{
printf("\nEntered no is found.");
printf("\nIt is at position:a[%d][%d]",x,y);
}
else if(flg==0)
printf("\nEntered no not found.");
getch();
}
--------------------------------------------------------------------------------------------------------
5. Write a program to demonstrate the use of "LINKED LIST".
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *next;
}*start;
void create();
void insert_after(int ele,int newele);
void insert_before(int ele,int newele);
void traverse();
void deletenode(int ele);
void erase();
void search(int ele);
void main()
{
int ele,newele,ch;
clrscr();
start=NULL;
do
{
printf("\n1>CREATE\n2>INSERT AFTER\n3>INSERT BEFORE\n4>TRAVERSE\n5>DELETE A NODE\n6>ERASE LIST\n7>SEARCH\n8>EXIT\nENTER YOUR CHOICE\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
printf("\nEnter the node after which u want to add a new element\t");
scanf("%d",&newele);
insert_after(ele,newele);
break;
case 3:
printf("\nEnter the node before which u want to add a new element\t");
scanf("%d",&ele);
printf("\nEnter the node to insert\t");
scanf("%d",&newele);
insert_before(ele,newele);
break;
case 4:
traverse();
break;
case 5:
printf("\nEnter the node u want to delete\t");
scanf("%d",&ele);
deletenode(ele);
break;
case 6:
erase();
break;
case 7:
printf("\nEnter the node u want to search \t");
scanf("%d",&ele);
search(ele);
break;
case 8:
printf("\nExisting the program.....");
break;
}
}while(ch != 8);
getch();
}
void create()
{
struct node *newnode,*p,*q;
int k,ch;
do
{
p=start;
printf("\nEnter info\t");
scanf("%d",&k);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->info=k;
newnode->next=NULL;
if(start == NULL)
start=newnode;
else
{
while(p->next!=NULL)
p=p->next;
p->next=newnode;
}
printf("\nWant to continue(YES =1 /NO = 0)?\t");
scanf("%d",&ch);
}while(ch != 0);
}
void insert_after(int ele,int newele)
{
struct node *p,*newnode;
p=start;
if(start == NULL)
{
printf("\n *** The list is empty.");
return;
}
newnode=(struct node*)malloc(sizeof(struct node));
newnode->info=newele;
newnode->next=NULL;
while(p->info != ele && p!=NULL)
p=p->next;
if(p==NULL)
printf("\n*** The node u mentioned is not present in the list.");
else
{
newnode->next=p->next;
p->next=newnode;
}
}
void insert_before(int ele,int newele)
{
struct node *p,*prev,*newnode;
p=start;
if(start==NULL)
{
printf("\nThe list is empty.");
return;
}
newnode=(struct node*)malloc(sizeof(struct node));
newnode->info=newele;
newnode->next=NULL;
if(ele == start->info)
{
newnode->next=start;
start=newnode;
}
else
{
while(p->info!=ele && p!=NULL)
{
prev=p;
p=p->next;
}
if(p==NULL)
printf("\nThe node u mentioned is not present in list.");
else
{
prev->next=newnode;
newnode->next=p;
}
}
}
void traverse()
{
struct node *p;
p=start;
if(start==NULL)
{
printf("\nThe list is empty.");
return;
}
printf("\nThe linked list is as:\n");
while(p!=NULL)
{
printf("%d->",p->info);
p=p->next;
}
printf("NULL");
}
void deletenode(int ele)
{
struct node *p,*prev;
p=start;
if(start==NULL)
{
printf("\nThe list is empty.");
return;
}
if(ele==start->info)
{
start=start->next;
free(p);
}
else
{
while(p->info != ele && p != NULL)
{
prev=p;
p=p->next;
}
if(p==NULL)
printf("\nThe node is mentioned is not present in list.");
else
{
prev->next=p->next;
free(p);
}
}
}
void search(int ele)
{
struct node *p;
int i=1;
p=start;
if(start==NULL)
{
printf("\nThe list is empty.");
return;
}
while(p->info!=ele && p!=NULL)
{
i++;
p=p->next;
}
if(p==NULL)
printf("\nThe node u mentioned is not present.");
else
printf("\nThe node u mentioned is present at location %d in list.",i);
}
void erase()
{
int k;
printf("\nDo u really want to erase list.After it u will lose all data(yes=1/no=0");
scanf("%d",&k);
if(k==1)
start=NULL;
}
----------------------------------------------------------------------------------------------------------
6.Write a program to implement STACK in 'C'.
#define MAX 50
struct stack
{
int top;
int ele[MAX];
}s;
int empty()
{
if(s.top==-1)
return 1;
else
return 0;
}
void display()
{
int i;
if(empty())
printf("\nStack is empty.");
else
{
printf("\nThe element of stack are :\n");
for(i=0;i<=s.top;i++)
printf("\t %d",s.ele[i]);
}
}
int pop()
{
int i;
if(empty())
return -1;
else
{
i=s.ele[s.top];
s.top--;
return i;
}
}
void push(int i)
{
if(s.top == MAX)
printf("\nStack Overflow \n");
else
{
s.top++;
s.ele[s.top] = i;
}
}
void main()
{
int ch,i,j;
s.top=-1;
do
{
printf("\n\t1.\tPush\n\t2.\tPop\n\t3.\tPrint\n\t4.\tExit");
printf("\nEnter your choice :-");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter element to push :");
scanf("%d",&i);
push(i);
break;
case 2:
j=pop();
if(j==-1)
printf("\nStack Underflow");
else
printf("\n%d element is deleted.",j);
break;
case 3:
display();
break;
case 4:
exit();
default:
printf("\nWARNING : INVALID CHOICE");
break;
}
}while(ch != 4);
getch();
}
------------------------------------------------------------------------------------------------------------
7. Write a program to sort elements by using SELECTION SORT method.
#include<stdio.h>
#include<conio.h>
int comparison =0;
int exchange=0;
int pass =0;
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%D\t",a[i]);
}
int selection_sort(int a[],int n)
{
int i,k,temp,m,miniindex;
i=0;
while(i<n-1)
{
k=i+1;
miniindex=i;
while(k<=n-1)
{
comparison++;
if(a[k]<a[miniindex])
{
miniindex=k;
k++;
}
if(miniindex!=i)
{
temp=a[i];
a[i]=a[miniindex] ;
a[miniindex]=temp;
exchange++;
}
printf("\n\n Array after pass%d\n\n",i);
print(a,n);
i++;
}
return(i);
}
void main()
{
int a[50],n,i,m;
clrscr();
printf("\n How many elements u want to enter? ");
scanf("%d",&n);
printf("\n The orignal array is \n");
print(a,n) ;
m=selection_sort (a,n);
printf("\n Nio of passes are%d",m);
printf("\n no of comparisons are%d",comparison);
printf("\n Noof exchanges are %d",exchange);
}
-----------------------------------------------------------------------------------------------------------
8.Write a program to calculate factorial of a number using Recursion.
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}
int mul(int a,int b)
{
if(b == 1)
return a;
if(b > 1)
return (mul(a,b-1)+a);
}
int fibo(int n)
{
int x,y;
if(n == 0 || n== 1)
return n;
x=fibo(n-1);
y=fibo(n-2);
return (x+y);
}
void main()
{
int a,b,i,ch;
clrscr();
do
{
printf("\n\n\n\n\t1.\tFactorial\n\t2.\tMultiplication of two nos.\n\t3.\tFibonacci\n\t4.\tExit");
printf("\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nEnter a no to calculate factorial :");
scanf("%d",&a);
printf("\n\nFactorial of %d no. = %d",a,fact(a));
break;
case 2:
printf("\n\nEnter two natural nos for multiplication :");
scanf("%d%d",&a,&b);
printf("\n\nMultiplication:\n%d * %d=%d",a,b,mul(a,b));
break;
case 3:
printf("\n\nEnter a no to calculate its fibonacci series :");
scanf("%d",&a);
printf("\n\nFibonacci series of %d number:\n",a);
for(i=0;i<=a;i++)
printf("\t%d",fibo(i));
break;
case 4:
exit();
default:
printf("\n\nWARNING : INVALID CHOICE");
break;
}
}while(ch != 4);
getch();
}
---------------------------------------------------------------------------------------------------------
9.Write a program to implement a QUEUE in 'C' using static allocation method.
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct Queue
{
int ele[MAX];
int front,rear;
}q;
int full()
{
if(q.rear >= MAX - 1)
return 1;
else
return 0;
}
int empty()
{
if(q.rear < q.front)
return 1;
else
return 0;
}
void insert(int k)
{
if(full())
printf("\nQueue Overflow");
else
{
q.rear++;
q.ele[q.rear]=k;
}
}
int del()
{
int k;
if(empty())
return -1;
else
{
k=q.ele[q.front];
q.front++;
}
return k;
}
void display()
{
int i;
for(i=q.front;i<=q.rear;i++)
printf("\t %d",q.ele[i]);
}
void main()
{
int ch,k;
clrscr();
q.front=0;
q.rear =-1;
do
{
printf("\n\n\n\t~ MENU ~");
printf("\n\n1.\tInsert\n\n2.\tDelete\n\n3.\tDisplay\n\n4.\tExit");
printf("\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter a element to add:");
scanf("%d",&k);
insert(k);
break;
case 2:
k=del();
if(k == -1)
printf("\nQueue Underflow");
else
printf("\nThe deleted element is:%d",k);
break;
case 3:
printf("\nPresent Queue Is:");
display();
break;
case 4:
exit();
default:
printf("\nWARNING : INVALID CHOICE");
break;
}
}while(ch != 4);
getch();
}
0 comments:
Post a Comment