Sie sind auf Seite 1von 9

//C PROGRAM TO REVERSE AN INPUT STRING USING STACKS

#include<stdio.h>
#include<string.h>
#define STACK_SIZE 20
void push(char item,int *top,char s[])
{
if (*top==STACK_SIZE-1)
{
printf("\n stack overflow\n");
return;
}
s[++(*top)]=item;
}
char pop(int *top,char s[])
{
char item_deleted;
if (*top==-1)
{
return 0;
}
item_deleted=s[(*top)--];
return item_deleted;
}
int is_rev(char str[])
{
int i;
int top=-1;
char s[30] ;
char stk_item=0;
for(i=0;i<strlen(str);i++)
{
push (str[i],&top,s);
}
printf("\n The reversed string is:");
for(i=0;i<strlen(str);i++)
{
stk_item= pop (&top,s);
printf("%c",stk_item);
}
getch();
}
void main()
{
char str[20];
clrscr();
printf("\n Enter the string to be reversed\n");
scanf("%s",str);
is_rev(str);
}

//C PROGRAM TO CONVERT A PREFIX EXPRESSION TO A POST FIX USING POINTERS


#include<stdio.h>
#include<string.h>
voidpush(char item[],int *top,char s[][20])
{
*top=*top+1;
strcpy(s[*top],item);
}
void *pop(int *top,char s[][20])
{
char *item;
item=s[*top];
*top=*top-1;
return item;
}
void pre_post(char prefix[],char postfix[])
{
char s[20][20];
int top,i;
char symbol,temp[2];
char *op1,*op2;
top=-1;
strrev(prefix);
for(i=0;i<strlen(prefix);i++)
{
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
switch (symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:
push(temp,&top,s);
}
}
}

void main()
{
char prefix[20];
char postfix[20];
printf("\n\n Enter the prefix expression \n\n");
scanf("%s",prefix);
pre_post(prefix,postfix);
printf("\n\n The postfix expression is %s \n\n",postfix);
}
//C PROGRAM TO IMPLEMENT BUBBLE SORT PROGRAM USING ARRAYS
BUBBLE SORT PROGRAM:
#include<stdio.h>
#include<conio.h>
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}//end for 1.
}//end function.
void main()
{
int a[100],n,i;
clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
} //end program

// C PROGRAM TO IMPLEMENTS INSERTION SORT PROGRAM USING ARRAYS


#include<stdio.h>
#include<conio.h>
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++) {
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}//end for 1.
}//end function.
void main()
{
int a[100],n,i;
clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
} //end program
//C PROGRAM TO IMPLEMENT HEAP SORT PROGRAM USING POINTERS
#include<stdio.h>
int *x[100],no,i;
void buildheap();
void sort();
void main()
{
clrscr();
printf("\n Enter the number of elements\n");
scanf("%d",&no);
printf("\n Enter %d numbers\n",no);
for(i=1;i<=no;++i)
scanf("%d",&x[i]);
buildheap();

sort();
printf("\n Sorted elements are:\n");
for(i=1;i<=no;++i)
printf("%5d",x[i]);
getch();
}
void buildheap()
{
int j,k,*temp;
for(k=2;k<no;++k)
{
i=k;
temp=x[k];
j=i/2;
while((i>1)&&(temp>x[j]))
{
x[i]=x[j];
i=j;
j=i/2;
if(j<1)j=1;
}
x[i]=temp;
}
}
void sort()
{
int *temp,*value,j,k;
for(k=no;k>=2;--k)
{
temp=x[1];
x[1]=x[k];
x[k]=temp;
i=1;
value=x[1];
j=2;
if ((j+1)<k)
if(x[j+1]>x[j])
j++; while((j<=(k-1))&&(x[j]>value))
{
x[i]=x[j];
i=j;
j=2*i;
if ((j+1)<k)
if(x[j+1]>x[j])
j++;
else
if(j>no)
j=no;
x[i]=value; } }
//C PROGRAM TO IMPLEMENT BUBBLE SORT USING POINTERS

#include<stdio.h>
int *a[100],i,j,item;
void main()
{
void sort(),display();
int i;
clrscr();
printf("\n Enter the number of elements in the first array\n");
scanf("%d",&item);
printf("\n Enter %d numbers\n",item);
for(i=0;i<item;++i)
scanf("%d",&a[i]);
sort();
display();
}
void sort()
{
int swap=1,*temp;
for(i=0;i<item && swap==1;++i)
{
swap=0;
for(j=0;j<item-(i+1);++j)
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap=1;
}
}
} void display()
{
printf("\n Sorted elements are:\n");
for(i=0;i<item;++i)
printf("%d\n",a[i]);
getch();
}
//C PROGRAM TO IMPLEMENT LINEAR SEARCH USING POINTERS
#include<stdio.h>
void main()
{
int *a[100],i,no,*srchno;
clrscr();
printf("\n Enter the number of elements\n");
scanf("%d",&no);
printf("\n Enter %d numbers\n",no);
for(i=0;i<no;++i)

scanf("%d",&a[i]);
printf("Enter the search number\n");
scanf("%d",&srchno);
for(i=0;i<no;++i)
if(srchno==a[i])
{
printf("\n search number is present");
exit(0);
}
printf("\n Search number is not present");
}
//C PROGRAM TO IMPLEMENT BINARY SEARCH USING POINTERS
#include<stdio.h>
void main()
{
int *a[100],i,no,*srchno,top,bottom,mid,j,*temp;
clrscr();
printf("\n Enter the number of elements\n");
scanf("%d",&no);
printf("\n Enter %d numbers\n",no);
for(i=0;i<no;++i)
scanf("%d",&a[i]);
printf("Enter the search number\n");
scanf("%d",&srchno);
for(i=0;i<no-1;++i)
for(j=i+1;j<no;++j)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("\n Sorted array in ascending order\n");
for(i=0;i<no;++i)
printf("%5d",a[i]);
bottom=0;
top=no-1;
while(top!=bottom+1)
{
mid=(bottom+top)/2;
if (a[mid]<=srchno)
bottom=mid;
else
top=mid;
}
if(a[bottom]==srchno)
printf("\n search number is present");
else

printf("\n Search number is not present");


}
WRITE A C PROGRAM TO IMPLEMENT BINARY SEARCH USING ARRAYS
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],n,i,j,item,temp;
printf("Enter the no.of elements in array ");
scanf(" ");
printf( "\n Enter the elements \n");
for(i=1;i<=n;i++)
{
scanf('%d",a[i]);
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;}
}
}
printf("\nSorted array: ");
for(i=1;i<=n;i++)
{
printf("%d"a[i]);
}
printf(" \nEnter the element to be searched \n");
scanf("%d", item);
int beg,end,mid,loc=-1;
beg=1;
end=n;
mid=int(beg+end)/2;
while((beg<=end) && (loc==-1))
{
if (a[mid]==item)
{
loc=mid;
}
else if (item<a[mid])
{
end=mid-1;
}

else{ beg=mid+1;
}
mid=int(beg+end)/2;
}
if (loc==-1)
printf("search unsuccessfull");
else
printf('search successfull");
printf("search location =%d", loc);
}

Das könnte Ihnen auch gefallen