0% found this document useful (0 votes)
10 views8 pages

Programs

The document contains three C programs: the first implements selection sort to sort an array of integers, the second manages a dynamic stack with push and pop operations, and the third converts an infix expression to postfix and evaluates it. Each program includes user interaction for input and displays results accordingly. The programs demonstrate fundamental data structures and algorithms in C.

Uploaded by

sahiltamseyrr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Programs

The document contains three C programs: the first implements selection sort to sort an array of integers, the second manages a dynamic stack with push and pop operations, and the third converts an infix expression to postfix and evaluates it. Each program includes user interaction for input and displays results accordingly. The programs demonstrate fundamental data structures and algorithms in C.

Uploaded by

sahiltamseyrr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Program 1:

#include<stdio.h>

void selectionsort(int a[], int n);

void selectionsort(int a[],int n)

int i,pos,j,temp;

for (i=0;i<n-1;i++)

{
pos = i;

for(j=i+1;j<n;j++)

if(a[j]<a[pos])

pos =j;

if (i!=pos)

temp =a[i];

a[i]=a[pos];

a[pos]=temp;

}
int main()

int a[100],n,i;

printf("enter the number of elements\n");

scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)

scanf("%d",&a[i]);

selectionsort(a,n);

printf("after sorting\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

return 0;

Program 2:

#include<stdio.h>

#include<stdlib.h>

int *a;

int max,top=-1;

void push(int ele)

if(top==max-1)

max=2*max;

printf("stack overflow so stack is resized and element is pushed\n");

a=realloc(a,max*sizeof(int));

}
a[++top]=ele;

int pop()

if(top==-1)
{
printf("stack underflow");

return -999;

else

return(a[top--]);

}
void display()

int i;

if(top==-1)

printf("stack is empty");

else

printf("stack elements are");

for(i=top;i>=0;i--)

printf("%d\t",a[i]);

int main()

int choice,ele;
printf("enter the value of max");

scanf("%d",&max);

a=(int *)malloc(max*sizeof(int));

while(1)

{
printf("\nenter your choice\n ");
printf("1 for push\n 2 for pop\n 3 for display\n 4 for exit\n");

scanf("%d",&choice);

switch(choice)

case 1: printf("enter the element to be pushed");

scanf("%d",&ele);

push(ele);

break;
case 2: ele=pop();

if(ele!=-999)

printf("deleted element is %d\n",ele);

break;

case 3: display();

break;

case 4: free(a);

exit(0);

default:printf("invalid choice");

Program 3:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<math.h>

char istack[20];
int tos=-1;
float stack[20];

int top=-1;

void ipush(char s)

istack[++tos]=s;

char ipop()

{
return istack[tos--];

int precd(char s)

switch(s)

case '^': return 4;

case '*':

case '%':

case '/': return 3;

case '+':

case '-': return 2;

case '(':

case ')':

case '#':return 1;
}

return 0;

void convertip(char infix[20], char postfix[20])

{
int i,j=0;
char symbol;

ipush('#');

for(i=0;i<strlen(infix);i++)

symbol=infix[i];

switch(symbol)

case '(': ipush(symbol);


break;

case ')': while(istack[tos]!='(')

postfix[j++]=ipop();

ipop();//pop ( bracket

break;

case '^':

case '*':

case '/':

case '%':

case '+':

case '-': while(precd(symbol)<=precd(istack[tos]))

postfix[j++]=ipop();

ipush(symbol);

break;

default:postfix[j++]=symbol;
}

while(istack[tos]!='#')

postfix[j++]=ipop();

postfix[j]='\0';
}
void push(float ele)

stack[++top]=ele;

float pop()

return(stack[top--]);

}
void evaluate(char postfix[50])

int i;

char sym;

float op1,op2,result,x;

for(i=0;i<strlen(postfix);i++)

sym=postfix[i];

if(isalpha(sym))

printf("enter the value for %c\t",sym);

scanf("%f",&x);

push(x);

else
{

op2=pop();

op1=pop();

switch(sym)

{
case '+':push(op1+op2);
break;

case '-':push(op1-op2);

break;

case '*':push(op1*op2);

break;

case '/':push(op1/op2);

break;

case '%':push((int)op1%(int)op2);
break;

case '^':push(pow(op1,op2));

break;

default:printf("invalid postfix expression");

exit(0);

result=pop();

printf("the result is %f\n",result);

int main()

char infix[20],postfix[20];

printf("\n Enter infix expression");


gets(infix);

convertip(infix,postfix);

printf("postfix expr is\n %s",postfix);

evaluate(postfix);

return 0;
}

You might also like