You are on page 1of 20

/* Porgram to create and traverse a binary search tree */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct tnode
{
int info;
struct tnode *l;
struct tnode *r;
}node;
node *root;
void create()
{
int n,i;
node *new,*ptr,*pptr;
printf("\n enter no of nodes:");
scanf("%d",&n);
printf("\n enter %d values\n",n);
root=(node *)malloc(sizeof(node));
root->l=root->r=NULL;
scanf("%d",&root->info);
for(i=2;i<=n;i++)
{
new=(node *)malloc(sizeof(node));
new->l=new->r=NULL;
scanf("%d",&new->info);
ptr=root;
while(ptr!=NULL)
{
pptr=ptr;
if(new->info<ptr->info)
{
ptr=ptr->l;
if(ptr==NULL)
pptr->l=new;
}
else
{
ptr=ptr->r;
if(ptr==NULL)
pptr->r=new;
}
}
}
}
void preorder(node *ptr)
{
if(ptr!=NULL)
{
printf("%d\t",ptr->info);
preorder(ptr->l);
preorder(ptr->r);
}
}
void inorder(node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->l);
printf("%d\t",ptr->info);
inorder(ptr->r);
}
}
void postorder(node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->l);
postorder(ptr->r);
printf("%d\t",ptr->info);
}
}
void main()
{
clrscr();
create();
printf("\n preorder traversal\n");
preorder(root);
printf("\n inorder traversal\n");
inorder(root);
printf("\n postorder traversal\n");
postorder(root);
getch();
}
/* Recursive program to generate Fibonacci series */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("enter a number\n");
scanf("%d",&n);
printf("the fibonacci series of number is\n");
for(i=0;i<n;i++)
printf("\n%d",fib(i));
getch();
}
fib(int n)
{
if(n==0)
return(0);
if(n==1)
return(1);
else
return(fib(n-1)+fib(n-2));
}

* Program to insert an element into an existing array */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[20],p,m;
clrscr();
printf("enter number of elements\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter an element to be inserted\n");
scanf("%d",&m);
printf("at what position?\n");
scanf("%d",&p);
for(i=n;i>=p;i--)
a[i]=a[i-1];
a[p-1]=m;
printf("after insertion srray is:\n\n");
for(i=0;i<=n;i++)
printf("%d\t",a[i]);
getch();
}

/* Program to delete a node from the Linked List */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct list
{
int info;
struct list *next;
}node;
node *start;
void main()
{
int item;
clrscr();
start=(node *)malloc(sizeof(node));
create(start);
printf("\n elements in linked list before deletion are\n");
display(start);
printf("\n enter an item to be deleted:");
scanf("%d",&item);
elete(item);
printf("\n linked after deletion\n");
display(start);
getch();
}
create(node *l)
{
printf("enter a number:");
scanf("%d",&l->info);
if(l->info==0)
l->next=NULL;
else
{
l->next=(node *)malloc(sizeof(node));
create(l->next);
}
return;
}
display(node *l)
{
while(l->info!=0)
{
printf("%d\t",l->info);
l=l->next;
}
return;
}
elete(int item)
{
int found=0;
node *ptr,*ptr_del;
ptr=start;
if(ptr->info==item)
{
found=1;
start=ptr->next;
free(ptr);
}
else
{
if(ptr->next->info==item)
{
ptr_del=ptr->next;
ptr->next=ptr_del->next;
free(ptr_del);
found=1;
}
else
ptr=ptr->next;
}
if(!found)
printf("\nitem to be deleted is not fund in the linked list");
return;
}
/* Program to sort an array using Selection Sort */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[' '],i,n;
void selection_sort(int[],int);
clrscr();
printf("enter the total no of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection_sort(a,n);
printf("the sored array in ascending order is\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void selection_sort(int a[],int n)
{
int i,j,temp,loc;
for(i=0;i<n-1;i++)
{
loc=i;
for(j=i+1;j<n;j++)
if(a[j]<a[loc])
loc=j;
if(i!=loc)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
}

/* Porgram to copy a string and to extract a substring from a given string */

#include<stdio.h>
#include<conio.h>
void main()
{
int sl,p;
void copy(char *,char *);
void extract(char *,int,int,char *);
char st1[20],st2[20],s[20];
clrscr();
printf("\n enter string 1\n");
scanf("%s",st1);
copy(st1,st2);
printf("\n enter length of substring:");
scanf("%d",&sl);
printf("enter position of first character of substring:");
scanf("%d",&p);
printf("\n after copying\nstring1=%s\nstring2=%s",st1,st2);
extract(st1,sl,p,s);
printf("\n after extracting,substring is : %s",s);
getch();
}
void copy(char *st1,char *st2)
{
while(*st1!='\0')
{
*st2=*st1;
st1++;
st2++;
}
*st2='\0';
}
void extract(char *st1,int sl,int p,char *s)
{
int i=0,j;
//for(i=0;*(st1+i)!='\0';i++)
while(*(st1+i)!='\0')
i++;
if(i<sl)
{
printf("\n length of substring exceeds main string");
getch();
exit();
}
else
{
for(j=0;j<sl;j++)
{
*s= *(st1+p-1);
st1++;
s++;
}
*s='\0';
}
}

/* Program to create a linked list and to display the same */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct list
{
int info;
struct list *next;
}node;
node *create()
{
node *start,*prev,*new;
int num;
printf("\nENTER ZERO TO STOP\n");
scanf("%d",&num);
prev=NULL;
start=NULL;
while(num!=0)
{
new=(node *)malloc(sizeof(node));
new->info=num;
new->next=NULL;
if(start==NULL)
start=new;
else
prev->next=new;
prev=new;
scanf("%d",&num);
}
return(start);
}
void display(node *l)
{
{
while(l!=NULL)
{
printf("\n%d",l->info);
l=l->next;
}
}
}
void main()
{
node *start;
clrscr();
start=create();
printf("\n LINKED LIST IS\n");
display(start);
getch();
}

/* Recursive program to find the GCD of two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int gcd(int m,int n);
int m,n;
clrscr();
printf("enter values\n");
scanf("%d %d",&m,&n);
printf("gcd of values is %d",gcd(m,n));
getch();
}
int gcd(int m,int n)
{
if(n==0)
return(m);
else
if(n>m)
return(gcd(n,m));
else
return(gcd(n,m%n));
}
/* Program to sort an array using Insertion Sort */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[' '],i,n;
void insert_sort(int[],int);
clrscr();
printf("enter the total no of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insert_sort(a,n);
printf("the sotred array in ascending order is\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void insert_sort(int a[],int n)
{
int i,k,temp;
for(k=1;k<n;k++)
{
temp=a[k];
{
i=k-1;
while(temp<a[i]&&i>=0)
{
a[i+1]=a[i];
i--;
}
a[i+1]=temp;
}
}
}

/* Program to insert a node into the Linked List */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct list
{
int info;
struct list *next;
}node;
node *start;

void create(node *l)


{
scanf("%d",&l->info);
if(l->info==0)
l->next=NULL;
else
{
l->next=(node *)malloc(sizeof(node));
create(l->next);
}
}
void display(node *l)
{
if(l!=NULL)
{
printf("%d\n",l->info);
display(l->next);
}
}
void ins_fi()
{
node *new;
int num;
printf("enter the number to be inserted as the first node :");
scanf("%d",&num);
new=(node*)malloc(sizeof(node));
new->info=num;
new->next=start;
start=new;
}
void ins_end()
{
node *new,*ptr,*prev;
int num;
printf("enter the number to be inserted at the end\n");
scanf("%d",&num);
new=(node *)malloc(sizeof(node));
new->info=num;
new->next=NULL;
ptr=start;
prev=NULL;
while(ptr->next!=NULL)
{
prev=ptr;
ptr=ptr->next;
}
if(prev==NULL)
start=new;
else
prev->next=new;
}
void main()
{
clrscr();
printf("enter 0 to stop\n");
start=(node*)malloc(sizeof(node));
create(start);
printf("\n elements in linked list before insertion are\n");
display(start);
ins_fi();
printf("linked list after insertion at begining\n");
display(start);
ins_end();
printf("linked list after insertion at the end\n");
display (start);
getch();
}

/* Program to implement the functions of Stack */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct list
{
int info;
struct list *next;
}node;
node *top;
void main()
{
void push(int);
void display(node *);
void pop();
int choice,item;
top=NULL;
do
{
clrscr();
printf("\STACK MENU");
Printf("\n1.push from top\n2.pop from top\n3.exit\n");
printf("enter ur choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter an item to be pushed\n");
scanf("%d",&item);
push(item);
printf("\n STACK CONTENTS AFTER PUSH OPERATION");
display(top);
getch();
break;
case 2:pop();
printf("\n stack after pp");
display(top);
getch();
break;
case 3:exit(1);
default:printf("\n invalid selection. please try again!");
break;
}
}
while(choice!=3);
}
void push(int item)
{
node *new_node;
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\n stack overflow");
getch();
exit(1);
}
else
{
new_node->info=item;
new_node->next=top;
top=new_node;
}
}
void pop()
{
node *temp;
if(top==NULL)
{
printf("\n STACK UNDERFLOW.(STACK IS EMPTY)");
return;
}
else
{
temp=top;
top=top->next;
free(temp);
}
return;
}
void display(node *l)
{
while(l!=NULL)
{
printf("\n\t%d",l->info);
l=l->next;
}
}

/* Program to delete an element from a given array */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[' '],n,i,p;
clrscr();
printf("enter the number of items in the array:");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
do
{
printf("\n enter the position whose element u want to delete :");
scanf("%d",&p);
if(p>n)
printf("the position has to be within %d or %d",n,n,"\n enter once more");
}while(p>n);
printf("\nthe item deleted is %d\n",a[p-1]);
for(i=p;i<n;i++)
a[i-1]=a[i];
printf("\nthe new list of item is\n");
for(i=0;i<n-1;i++)
printf("%d\t",a[i]);
getch();
}

/* Recursive program for the Tower of Hanoi with three discs */

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter the number of disks\n");
scanf("%d",&n);
tower(n,'A','B','C');
getch();
}
tower(int n,char frompeg,char topeg,char auxpeg)
{
if(n==1)
{
printf("\n\n move disk1 frompeg%c topeg%c",frompeg,topeg);
return;
}
tower(n-1,frompeg,auxpeg,topeg);
printf("\n\n move disk%d frompeg%c topeg%c",n,frompeg,topeg);
tower(n-1,auxpeg,topeg,frompeg);
return;
}
/* Program to sort an array using Merge Sort */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[' '],i,n;
void mer_sort(int a[' '],int n);
clrscr();
printf("enter the no.elements:");
scanf("%d",&n);
printf("\nenter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mer_sort(a,n);
printf("the array elements in ascending order is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void mer_sort(int a[' '],int n)
{
int l1,u1,l2,u2,i,j,k,size,aux[' '];
size=1;
while(size<n)
{
l1=0;k=0;
while(l1+size<n)
{
l2=l1+size;
u1=l2-1;
if(l2+size-1<n)
u2=l2+size-1;
else
u2=n-1;
i=l1;j=l2;
while(i<=u1&&j<=u2)
{
if(a[i]<=a[j])
{
aux[k]=a[i++];
}
else
{
aux[k]=a[j++];
}
k++;
}
while(i<=u1)
{
aux[k++]=a[i++];
}
while(j<=u2)
{
aux[k++]=a[j++];
}
l1=u2+1;
}
for(i=l1;i<n;i++)
{
aux[k]=a[i];
k++;
}
for(i=0;i<n;i++)
a[i]=aux[i];
size=size*2;
}
}

/* Program to implement the functions of a Queue */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct list
{
int info;
struct list *next;
}node;
node *front=NULL,*rear=NULL;
que_ins()
{
node *new;
int item;
clrscr();
printf("\n enter the item to be inserted\n");
scanf("%d",&item);
new=(node *)malloc(sizeof(node));
new->info=item;
new->next=NULL;
if(front==NULL)
{
front=new;
rear=new;
}
else
rear->next=new;
rear=new;
return;
}
que_del()
{
node *temp;
if(front==NULL)
{
printf("que is empty\n");
return;
}
else
{
printf("deleted item is =%d\n",front->info);
temp=front;
if(rear==front)
rear=front=NULL;
else
front=front->next;
free(temp);
}
return;
}
void display(node *front)
{
if(front==NULL)
printf("que is empty");
else
while(front!=NULL)
{
printf("\t%d",front->info);
front=front->next;
}
}
menu()
{
int choice;
printf("\nQUEUE MENU");
printf("\n1.insert in\n2.delete in\n3.exit\n");
printf("enter ur choice :");
scanf("%d",&choice);
if((choice>0)&&(choice<4))
return(choice);
else
menu();
return(0);
}
void main()
{
int choice;
clrscr();
while(1)
{
choice=menu();
switch(choice)
{
case 1:que_ins();
printf("queue after insertion\n");
display(front);
getch();
break;
case 2:que_del();
{
printf("q after deletion :\n");
display(front);
}
getch();
break;
case 3:exit();
}
}
}

/* Program to concatenate two strings using pointers */

#include<stdio.h>
#include<conio.h>
void main()
{
void con_cat(char *,char *);
int i,j,k;
char str1[30],str2[30];
clrscr();
printf("\n enter 2 strings\n");
gets(str1);
gets(str2);
printf("\n length of %s=%d",str1, len(str1));
printf("\n length of %s=%d",str2, len(str2));
printf("\n after concatenation result is:");
con_cat(str1,str2);
puts(str1);
getch();
}
len(char *ch)
{
int l=0;
while(*ch!='\0')
{
ch++;
l++;
}
return(l);
}
void con_cat(char *st1,char *st2)
{
char *s;
int i,j,k;
i=len(st1);
j=len(st2);
s=(st2);
for(k=0;k<=j;k++)
{
*(st1+i)=*(s+k);
i++;
}
*(st1+i)='\0';
}

You might also like