A C Program on Lexical Analysis

Leave a Comment



#include

#include

#include

#include

#define MAX 30

 

 

void main()

{

char str[MAX];

int state=0;

int i=0, j, startid=0, endid, startcon, endcon;

 

 

clrscr();

 

 

for(j=0; j') state=21;   

 //relational '>' or '>='

 

 else if(str[i]=='=') state=23;   

 //relational '==' or assignment '='

 

 else if(isdigit(str[i])) 

 { 

state=25; startcon=i; 

 }

//constant

 

 else if(str[i]=='(') state=26;   

 //special characters '('

 

 else if(str[i]==')') state=27;   

 //special characters ')'

 

 else if(str[i]==';') state=28;   

 //special characters ';'

 

 else if(str[i]=='+') state=29;   

 //operator '+'

 

 else if(str[i]=='-') state=30;   

 //operator '-'

 

 break;

 

 

//States for 'if'

case 1: if(str[i]=='f') state=2;

 else { state=17; startid=i-1; i--; }

 break;

case 2: if(str[i]=='(' || str[i]==NULL)

 {

 printf("if : Keyword");

 state=0;

 i--;

 }

 else { state=17; startid=i-2; i--; }

 break;

 

 

//States for 'while'

case 3: if(str[i]=='h') state=4;

 else { state=17; startid=i-1; i--; }

 break;

case 4: if(str[i]=='i') state=5;

 else { state=17; startid=i-2; i--; }

 break;

case 5: if(str[i]=='l') state=6;

 else { state=17; startid=i-3; i--; }

 break;

case 6: if(str[i]=='e') state=7;

 else { state=17; startid=i-4; i--; }

 break;

case 7: if(str[i]=='(' || str[i]==NULL)

 {

 printf("while : Keyword");

 state=0;

 i--;

 }

 else { state=17; startid=i-5; i--; }

 break;

 

 

//States for 'do'

case 8: if(str[i]=='o') state=9;

 else { state=17; startid=i-1; i--; }

 break;

case 9: if(str[i]=='{' || str[i]==' ' || str[i]==NULL || str[i]=='(')

 {

 printf("do : Keyword");

 state=0;

 i--;

 }

 break;

 

 

//States for 'else'

case 10: if(str[i]=='l') state=11;

else { state=17; startid=i-1; i--; }

break;

case 11: if(str[i]=='s') state=12;

else { state=17; startid=i-2; i--; }

break;

case 12: if(str[i]=='e') state=13;

else { state=17; startid=i-3; i--; }

break;

case 13: if(str[i]=='{' || str[i]==NULL)

{

printf("else : Keyword");

state=0;

i--;

}

else { state=17; startid=i-4; i--; }

break;

 

 

//States for 'for'

case 14: if(str[i]=='o') state=15;

else { state=17; startid=i-1; i--; }

break;

case 15: if(str[i]=='r') state=16;

else { state=17; startid=i-2; i--; }

break;

case 16: if(str[i]=='(' || str[i]==NULL)

{

printf("for : Keyword");

state=0;

i--;

}

else { state=17; startid=i-3; i--; }

break;

 

 

//States for identifiers

case 17: 

 

if(isalnum(str[i]) || str[i]=='_') 

{ 

state=18; i++; 

}

else if(str[i]==NULL||str[i]=='<'||str[i]=='>'||str[i]=='('||str[i]==')'||str[i]==';'||str[i]=='='||str[i]=='+'||str[i]=='-') state=18;

i--;

break;

 

 

case 18: 

 

if(str[i]==NULL || str[i]=='<' || str[i]=='>' || str[i]=='(' || str[i]==')' || str[i]==';' || str[i]=='=' || str[i]=='+' ||str[i]=='-')

{

endid=i-1;

printf("");

for(j=startid; j<=endid; j++)

printf("%c", str[j]);

printf(" : Identifier");

state=0;

i--;

}

break;

 

 

//States for relational operator '<' & '<='

case 19: if(str[i]=='=') state=20;

else if(isalnum(str[i]) || str[i]=='_')

{

printf("< : Relational operator");

i--;

state=0;

}

break;

case 20: if(isalnum(str[i]) || str[i]=='_')

{

printf("<= : Relational operator");

i--;

state=0;

}

break;

 

 

//States for relational operator '>' & '>='

case 21: if(str[i]=='=') state=22;

else if(isalnum(str[i]) || str[i]=='_')

{

printf("> : Relational operator");

i--;

state=0;

}

break;

case 22: if(isalnum(str[i]) || str[i]=='_')

{

printf(">= : Relational operator");

i--;

state=0;

}

break;

 

 

//States for relational operator '==' & assignment operator '='

case 23: if(str[i]=='=') state=24;

else

{

printf("= : Assignment operator");

i--;

state=0;

}

break;

case 24: if(isalnum(str[i]))

{

printf("== : Relational operator");

state=0;

i--;

}

break;

 

 

//States for constants

case 25: if(isalpha(str[i]))

{

printf("*** ERROR ***");

puts(str);

for(j=0; j' || str[i]==NULL || str[i]==';' || str[i]=='=')

{

endcon=i-1;

printf("");

for(j=startcon; j<=endcon; j++)

printf("%c", str[j]);

printf(" : Constant");

state=0;

i--;

}

break;

 

 

//State for special character '('

case 26: printf("( : Special character");

startid=i;

state=0;

i--;

break;

 

 

//State for special character ')'

case 27: printf(") : Special character");

state=0;

i--;

break;

 

 

//State for special character ';'

case 28: printf("; : Special character");

state=0;

i--;

break;

 

 

//State for operator '+'

case 29: printf("+ : Operator");

state=0;

i--;

break;

 

 

//State for operator '-'

case 30: printf("+ : Operator");

state=0;

i--;

break;

 

 

//Error State

case 99: break;

}

i++;

}

printf("End of program");

getch();

}


Output of the program should be like this:

Correct input

-------------
*** Program on Lexical Analysis *** 

Enter the string: for(x1=0; x1<=10; x1++);


Analysis:
 

for     : Keyword

(       : Special character

x1      : Identifier

=       : Assignment operator

0       : Constant

;       : Special character

x1      : Identifier

<=      : Relational operator

10      : Constant

;       : Special character

x1      : Identifier

+       : Operator

+       : Operator

)       : Special character

;       : Special character

End of program

Wrong input

-----------

*** Program on Lexical Analysis ***

Enter the string: for(x1=0; x1<=19x; x++);

Analysis:

for     : Keyword

(       : Special character

x1      : Identifier

=       : Assignment operator

0       : Constant

;       : Special character

x1      : Identifier

<=      : Relational operator

Token cannot be generated

Read More...

A Calender Program in C

Leave a Comment

#include
#include
#include
int getNumberOfDays(int month,int year)
{
   switch(month)
   {
      case 1 : return(31);
      case 2 : if(year%4==0)
   return(29);
        else
   return(28);
      case 3 : return(31);
      case 4 : return(30);
      case 5 : return(31);
      case 6 : return(30);
      case 7 : return(31);
      case 8 : return(31);
      case 9 : return(30);
      case 10: return(31);
      case 11: return(30);
      case 12: return(31);
      default: return(-1);
   }
}
char *getName(int odd)
{
   switch(odd)
   {
      case 0 :return("Sunday");
      case 1 :return("Monday");
      case 2 :return("Tuesday");
      case 3 :return("Wednesday");
      case 4 :return("Thursday");
      case 5 :return("Friday");
      case 6 :return("Saturday");
      default:return("Error in getName() module.Invalid argument
passed");
   }
}
int getOddNumber(int day,int mon,int year)
{
   int res=0,t1,t2,y=year;
   year = year-1600;
   while(year>=100)
   {
       res=res+5;
       year=year-100;
   }
   res=(res%7);
   t1=((year-1)/4);
   t2=(year-1)-t1;
   t1=(t1*2)+t2;
   t1=(t1%7);
   res = res+t1;
   res=res%7;
   t2=0;
   for(t1=1;t12000)
     res=res+1;
   res = res%7;
   return res;
}
char *getWeek(int dd,int mm,int yy)
{
   int odd;
   if(!(mm>=1 && mm<=12))
   {
      return("Invalid month value");
   }
   if(!(dd>=1 && dd<=getNumberOfDays(mm,yy)))
   {
      return("Invalid date");
   }
   if(yy>=1600)
   {
     odd = getOddNumber(dd,mm,yy);
     odd=odd%7;
     return(getName(odd));
   }
   else
   {
      return("
Please give year more than 1600");
   }
}
void printMonth(int mon,int year,int x,int y)
{
   int nod,odd,cnt,d=1,x1=x,y1=y;
   if(!(mon>=1 && mon<=12))
   {
       printf("
INVALID MONTH");
       getch();
       return;
   }
   if(!(year>=1600))
   {
      printf("
INVALID YEAR");
      getch();
      return;
   }
   if(x<=0)
     x=wherex();
   if(y<=0)
     y=wherey();
   gotoxy(x,y);
   textcolor(RED);
   cprintf("S");
   textcolor(YELLOW);
   cprintf("   M   T   W   T   F   S");
   /*       1234567891234567891234567 */
   textcolor(7);
   cprintf("");
   y++;
   nod=getNumberOfDays(mon,year);
   odd=getOddNumber(d,mon,year);
   switch(odd)
   {
     case 0 : x=x;
       cnt=1;
       break;
     case 1 : x=x+4;
       cnt=2;
       break;
     case 2 : x=x+8;
       cnt=3;
       break;
     case 3 : x=x+12;
       cnt=4;
       break;
     case 4 : x=x+16;
       cnt=5;
       break;
     case 5 : x=x+20;
       cnt=6;
       break;
     case 6 : x=x+24;
       cnt=7;
       break;
     default : printf("

INVALID DATA FROM THE getOddNumber() 
MODULE");
        return;
   }
   gotoxy(25,25);
   gotoxy(x,y);
   printf("%02d",d);
   for(d=2;d<=nod;d++)
   {
      if(cnt%7==0)
      {
 y++;
 cnt=0;
 x=x1-4;
      }
      x = x+4;
      cnt++;
      gotoxy(x,y);
      printf("%02d",d);
   }
}
main()
{
   char ch='k';
   int dd,mm,yy;
   while(ch!='0')
   {
      clrscr();
      printf("




    1.Know the day");
      printf("
    2.Print the month");
      printf("
    0.EXIT");
      printf("

    ENTER YOUR CHOICE : ");
      flushall();
      fflush(stdin);
      ch=getche();
      clrscr();
      switch(ch)
      {
 case '1': printf("Enter date (DD MM YYYY) : ");
    scanf("%d %d %d",&dd,&mm,&yy);
    printf("Day is : %s",getWeek(dd,mm,yy));
    flushall();
    getch();
    break;
 case '2' : printf("Enter month and year (MM YYYY) : ");
     scanf("%d %d",&mm,&yy);
     printf("

");
     printMonth(mm,yy,-1,-1);
     flushall();
     getch();
     break;
 case '0' : exit(0);
      }
   }
}
Read More...

C program for TRAVERSING A TWO DIMENSIONAL ARRAY

Leave a Comment

# include

int i, j;
float mat[10][10];

void Traverse ( int, int);
void input( int, int);

void Traverse (int row, int col)
{
	printf("\n Traversing in row major order\n");

	for( i = 0; i < row; i++)
	{
		for( j = 0; j < col; j++)
		{
			printf("\n 0x%x", &mat[i][j]);
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}

	printf("\n Traversing in column major order\n");

	for(j = 0; j < col; j++)
	{
		for(i = 0; i < row; i++)
		{
			printf("\n  0x%x", &mat[i][j]);
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}
	printf("\n Traversing in row major order\n");

	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  mat[%d][%d] = ", i, j);
			printf("%f", mat[i][j]);
		}
		printf("\n");
	}

	printf("\n Traversing in column major order\n");

	for(j = 0; j < col; j++)
	{
		for(i = 0; i < row; i++)
		{
			printf("  mat[%d][%d] = ", i, j);
			printf("%f", mat[i][j]);
		}
		printf("\n");
	}
}

void input(int row, int col)
{
	for(i = 0 ; i< row; i++)
	{
		for(j = 0 ;  j
Read More...

C Program to Add Tow Matrices

Leave a Comment

# include
# include

# define row 10
# define col 10

int i, j;
int row1, col1;
int row2, col2;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_add(  float mat1[row][col], int, int,
float mat2[row][col], int, int,
float mat_res[row][col]);

void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);

void mat_add(float mat1[row][col], int row1, int col1,
float mat2[row][col], int row2, int col2,
float mat_res[row][col])
{
	int i, j;
	if((row1 == row2) && (col1 == col2))
	{
		printf("\n Addition is possible and Result is as follows\n");

		for(i = 0; i1:");
	scanf("%d", &row1);
	printf(" Input the col  of the matrix->1:");
	scanf("%d", &col1);
	printf("\n Input data for matrix-> 1\n");

	input(mat1, row1, col1);
	printf("\n Input the row  of the matrix ->2:");
	scanf("%d", &row2);
	printf("\n Input the col  of the matrix->2:");
	scanf("%d", &col2);

	printf("\n Input data for matrix-> 2\n");
	input(mat2, row2, col2);

	printf("\n Entered Matrix First is as follows:\n");
	display(mat1,row1,col1);

	printf("\n Entered Matrix Two is as follows:\n");
	display(mat2,row2,col2);
	mat_add(mat1, row1, col1, mat2, row2, col2, mat_res);
}

Read More...

C Program to Find norm of matrix: Norm a matrix is defined as squareroot of the sum of the squares of the elements of a matrix

Leave a Comment


# include
# include

# define row 10
# define col 10

int i, j;
float summation;

void display( float mat[row][col], int, int);
void input(float mat[row][col] ,int, int);
float norm_mat(float mat[row][col], int, int);

void display(float mat[row][col] ,int row1, int col1)
{
	for(i = 0; i < row1; i++)
	{
		for(j = 0; j < col1; j++)
		{
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}
}

/* Input function */

void input(float mat[row][col] ,int row1, int col1)
{
	for(i = 0 ; i< row1; i++)
	{
		for(j = 0 ;  j
Read More...

C Program to Find upper and lower half triangle of a matrix

Leave a Comment

# include

int i, j;
float mat[10][10];

void display( int, int);
void input( int, int);
void Triangle_Matrix(int, int);

void display(int row, int col)
{
	for( i = 0; i < row; i++)
	{
		for( j = 0; j < col; j++)
		{
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}
}

void input(int row, int col)
{
	for(i = 0 ; i< row; i++)
	{
		for(j = 0 ;  j= j )
			{
				printf(" %f", mat[i][j]);
			}
		}
		printf("\n");
	}
	printf("\n Upper Half is as follows:\n");
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col ; j++)
		{
			if( i <= j )
				printf("  %f", mat[i][j]);
			if(i>j)
				printf("        ");
		}
		printf("\n");
	}
}
void main()
{
	int r,c;
	printf("\n Input number of rows:");
	scanf("%d", &r);
	printf(" Input number of cols:");
	scanf("%d", &c);
	input(r, c);
	printf("\n Entered Matrix is as follows:\n");
	display(r, c);
	printf("\n Triangle Matrix is as follows:\n");
	Triangle_Matrix(r,c);
}

Read More...

C Program for Sorting two dimensional arrays row wise

Leave a Comment


# include

int i, j;
float mat[10][10];

void display( int, int);
void input( int, int);
void Two_Sort_Matrix(int, int);

void display(int row, int col)
{
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}
}

void input(int row, int col)
{
	for(i = 0 ; i< row; i++)
	{
		for(j = 0 ;  j
Read More...

C Program to Find Transpose of a matrix

Leave a Comment

# include

int i, j;
int value;
int mat[10][10];
void display(int, int);
void display_o( int transp[10][10],int, int);
void input( int transp[10][10],int, int);
void transpose( int transp[10][10],int, int);

void transpose(int transp[10][10], int row, int col)
{
	for(i = 0; i< row; i++)
	{
		for(j = 0; j < col; j++)
		{
			mat[i][j] = transp[j][i] ;
		}
	}
}

void display(int row, int col)
{
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  %d", mat[i][j]);
		}
		printf("\n");
	}
}

void display_o(int transp[10][10], int row, int col)
{
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  %d", transp[i][j]);
		}
		printf("\n");
	}
}

void input(int transp[10][10], int row, int col)
{
	for(i = 0 ; i< row; i++)
	{
		for(j = 0 ;  j
Read More...

C Program to Find trace of a Matrix

Leave a Comment


# include

int i, j;
float mat[10][10];

void display( int, int);
void input( int, int);
float trace_mat(int, int);

/* Display function */

void display(int row, int col)
{
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}
}

/* Input function */

void input(int row, int col)
{
	for(i = 0 ; i < row; i++)
	{
		for(j = 0 ;  j < col; j++)
		{
			printf("\nInput Value for : %d: %d: ", i+1, j+1);
			scanf("%f", &mat[i][j]);
		}
	}
}

/* Finding trace of a matrix */

float trace_mat(int row, int col)
{
	float trace = 0;
	for(i = 0; i < row; i++)
		for(j = 0; j< col; j++)
			if(i == j)
				trace += mat[i][j];
	return(trace);
}

/* main function */

void main()
{
	float trace;
	int r,c;
	printf("\n Input the number of rows:");
	scanf("%d", &r);
	printf(" Input number of cols:");
	scanf("%d", &c);
	input(r,c);
	printf("\n Entered Matrix is as follows:\n");
	display(r,c);
	printf("\n Trace of above matrix is :");
	trace = trace_mat(r,c);
	printf("%f", trace);
}
Read More...

C Program to SEARCHING FOR LARGEST AND SMALLEST ELEMENTS IN A LINEAR ARRAY

Leave a Comment
# include

int s;
int small, big, temp;

int search_array(int *, int);
void input(int *, int );
void display(int *, int );

/* Definition of the function */

int search_array(int array[], int number)
{
	big = small = array[0];
	temp = 0;

	while(temp < number)
	{
		if(array[temp] > big)
		{
			big = array[temp] ;
		}
		else if(array[temp] < small)
		{
			small = array[temp];
		}

		temp ++;
	}
	s = small;
	return(big);
}

/* Input function */

void input(int array[], int number)
{
	int i;
	for(i = 0; i< number ; i++)
	{
		printf("Input value for : %d: ",i+1);
		scanf("%d", &array[i]);
	}
}

/* Output function */

void display(int array[], int number)
{ 
	int i;
	for(i = 0; i < number; i++)
	{
		printf("\n Value at the position: %d: %d", i+1, array[i]);
	}
}

/* main function */

void main()
{
	int number,big;
	int array[100];

	printf("\n Input the number of elements in the list:");
	scanf("%d", &number);
	input(array, number);
	printf("\n Entered list is as follows:\n");

	display(array,number);

	big = search_array(array,number);
	printf("\nLargest number in the array is : %d", big);
	printf("\nSmallest number in the array is : %d", s);
}
Read More...

C Program to TEST SINGULARITY OF A GIVEN MATRIX

Leave a Comment
# include




int i, j;
float mat[10][10];
float mat1[10][10];




void display( int, int);
void input( int, int);
float Singular_Matrix(int, int);


void display( int row, int col)
{
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  %f", mat[i][j]);
		}
		printf("\n");
	}
}




/* Input function */
void input( int row, int col)
{
	for(i = 0 ; i< row; i++)
	{
		for(j = 0 ;  j
Read More...

C Program to Find the Rank of a Matrix

Leave a Comment
# include




int R,C;
int i, j;
int mat[10][10];




void display( int, int);
void input( int, int);
int Rank_Mat(int , int);
void swap(int, int, int);
void swap( int row1,int row2, int col)
{
	for( i = 0; i < col; i++)
	{
		int temp = mat[row1][i];
		mat[row1][i] = mat[row2][i];
		mat[row2][i] = temp;
	}
}

int Rank_Mat(int row1, int col1)
{
	int r, c;
	for(r = 0; r< col1; r++)
	{
		display(R,C);
		if( mat[r][r] )  // Diagonal element is not zero
		for(c = 0; c < row1; c++)
			if(c != r)
			{
				float ratio = mat[c][r]/ mat[r][r];
				for( i = 0; i < col1; i++)
					mat[c][i] -= ratio * mat[r][i];
			}




			else
				printf("\n");


		else
		{
			for(c =  r+1 ; c < row1;  c++)
				if (mat[c][r])
				{
					/*  Find non zero elements in the same column */
					swap(r,c,col1);
					break ;
				}




			if(c == row1)
			{
				-- col1;




				for(c = 0; c < row1; c ++)
					mat[c][r] = mat[c][col1];
			}
			--r;
		}
	}
	return col1;
}

void display( int row, int col)
{
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			printf("  %d", mat[i][j]);
		}
		printf("\n");
	}
}
void input( int row, int col)
{
	int value;
	for(i = 0 ; i< row; i++)
	{
		for(j = 0 ;  j
Read More...

C program to Find Orthogonality of Matrix

Leave a Comment

# include"stdio.h"
# include"stdlib.h"

# define row 10
# define col 10

int i, j, k;
int row1, col1;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_mult( float mat1[row][col], int, int,
float mat_res[row][col]);

void transpose( float transp[row][col], int, int);
void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);

void mat_mult( float mat1[row][col], int row1, int col1,
float mat_res[row][col])
{
	int flag ;
	if(col1 == row1)
	{
		printf("\n Multiplication is possible and Result is as follows\n");

		for(i = 0; i
Read More...

C Program to Multiply Two Matrices

Leave a Comment


# include
# include

# define row 10
# define col 10

int i, j;
int row1, col1;
int row2, col2;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_mult(  float mat1[row][col], int, int,
float mat2[row][col], int, int,
float mat_res[row][col]);

void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);


void mat_mult( float mat1[row][col], int row1, int col1,
float mat2[row][col], int row2, int col2,
float mat_res[row][col])
{
	int i, j, k;
	if(col1 == row2)
	{
		printf("\n Multiplication is possible and Result is as follows\n");

		for(i =0; i1:");
	scanf("%d", &row1);

	printf("\n Input the col  of the matrix->1:");
	scanf("%d", &col1);

	printf("\n Input data for matrix-> 1\n");
	input(mat1, row1, col1);

	printf("\n Input the row  of the matrix ->2:");
	scanf("%d", &row2);

	printf("\n Input the col  of the matrix->2:");
	scanf("%d", &col2);

	printf("\n Input data for matrix-> 2\n");
	input(mat2, row2, col2);

	printf("\n Entered Matrix First is as follows:\n");
	display(mat1,row1,col1);

	printf("\n Entered Matrix Two is as follows:\n");
	display(mat2,row2,col2);

	mat_mult(mat1 ,row1 ,col1, mat2, row2, col2, mat_res);
}

Read More...

C Program to Find The Inverse of a Matrix

Leave a Comment

# include

int i, j;

void display( int, int,  float mat[10][10],float mat1[10][10]);
void input( int, int,  float mat[10][10],float mat1[10][10]);
Inverse_Mat(int , int,  float mat[10][10],float mat1[10][10]);
void swap(int, int, int,  float mat[10][10],float mat1[10][10]);

/* This function exchange two rows of a matrix */

void swap( int row1,int row2, int col, float mat[10][10],float mat1[10][10])
{
	for( i = 0; i < col; i++)
	{
		float   temp = mat[row1][i];
		mat[row1][i] = mat[row2][i];
		mat[row2][i] = temp;

		temp = mat1[row1][i];
		mat1[row1][i] = mat1[row2][i];
		mat1[row2][i] = temp;

	}
}

/* This function find inverse of matrix */

int Inverse_Mat(int row1, int col1, float mat[10][10],float mat1[10][10])
{
	int singular = 0;
	int r, c;
	for(r = 0;( r < row1)&& !singular;  r++)
	{

		if( mat[r][r] )  /* Diagonal element is not zero */
			for(c = 0; c < col1; c++)

				if( c == r)
				{

					/* Make all the elements above and below the current principal
					 diagonal element zero */

					float ratio =  mat[r][r];
					for( i = 0; i < col1; i++)
					{
						mat[r][i] /= ratio ;
						mat1[r][i] /= ratio;
					}
				}
				else
				{
					float ratio = mat[c][r] / mat[r][r];
					for( i = 0; i < col1;  i++)
					{
						mat[c][i] -= ratio * mat[r][i];
						mat1[c][i] -= ratio * mat1[r][i];
					}
				}

		else
		{
			/* If principal diagonal element is zero */
			singular = 1;

			for(c = (r+1); (c < col1) && singular; ++c)

				if(mat[c][r])
				{
					singular = 0;
					/* Find non zero elements in the same column */
					swap(r,c,col1, mat, mat1);
					--r;
				}

		}
	}
	return(!singular);
}

/* To print output this is used */

void display( int row, int col, float mat[10][10],float mat1[10][10])
{
	printf("\n");
	/* Output of inverse Matrix */
	for( i = 0; i < row; i++)
	{
		for( j = 0; j < col; j++)
		{
			printf("   %f", mat1[i][j]);
		}
		printf("\n");
	}

}

/* input function */

void input( int row, int col, float mat[10][10],float mat1[10][10])
{
	for( i = 0 ; i< row; i++)
	{
		for( j = 0 ;  j
Read More...

C program for Traversing a linear array

Leave a Comment
Here's the code for C program for Traversing a linear array

# include


void memory(int *, int, int);
void memory1(char *, int, int);

/* Definition of the function memory */

void memory(int a[], int l_b, int u_b)
{
 int counter;

 for(counter = l_b; counter<=u_b; counter++)
 {
  printf("\n Element at location: 0x%x is %d", &a[counter], a[counter]);
 }
 printf("\n Array size = %d ", &a[counter-1] - &a[0] + 1);
}

void memory1(char b[], int l_b, int u_b)
{
 char *pointer;
 int counter;
 pointer =&b[0];

 for(counter = l_b; counter<=u_b; counter++)
 {
  printf("\n Element at location: 0x%x is %d", &b[counter], b[counter]);
 }
 printf("\n Array size = %d", &b[counter-1] - &b[0] + 1);
}

/*Function main */

void main()
{
 int a[12] = {
  99,88,77,66,55,44,33,22,11,100,200,300 };
 char b[] = {
  'A','B','C','D','E','F','G','H','I','J','K',
       'L','M','N','O','P','Q','R','S','T','U','V',
       'W','X','Y','Z' };
 int lb=0, ub=11;
 memory(a,lb,ub);
 lb = 0;
 ub = 25;
 memory1(b,lb,ub);
}
Read More...

Source Code: Data Structure Questions

Leave a Comment

Q.1) WRITE A PROGRAM IN C TO SORT AN ARRAY USING INSERTION SORT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void insort(int a[], int size)
{
 int i, j, t;
 for (i=1; i<size; i++)
  {
                                t = a[i];
                                j = i;
                                while ((j > 0) && (a[j-1] > t))
                                 {
                                  a[j] = a[j-1];
                                  j = j - 1;
                                 }
                                a[j] = t;
  }
}


void main()
{
 int *a,i,n;
 printf("\nEnter the length of the array :");
 scanf("%d",&n);
 a=(int*)malloc(n * sizeof(int));
 printf("\nEnter the elements of the array :\n");
 for(i=0;i<n;i++)
  {
                printf("Enter %d element : ",i+1);
                scanf("%d",&a[i]);
  }
 insort(a,n);
 printf("\n\nSorted array : ");
 for(i=0;i<n;i++)
 printf("  %d",a[i]);
 getch();
}



Q.2) WRITE A PROGRAM IN C TO SORT AN ARRAY USING SELECTION SORT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


void selsort(int a[], int size)
{
 int i, j, min, temp;
 for (i=0; i<(size-1); i++)
  {
                min = i;
                for (j=i+1; j<size; j++)
                 {
                  if (a[j] < a[min])
                  min = j;
                 }
                temp = a[i];
                a[i] = a[min];
                a[min] = temp;
  }
}

void main()
{
 int *a,i,n;
 printf("\nEnter the length of the array :");
 scanf("%d",&n);
 a=(int*)malloc(n * sizeof(int));
 printf("\nEnter the elements of the array :\n");
 for(i=0;i<n;i++)
  {
                printf("Enter %d element : ",i+1);
                scanf("%d",&a[i]);
  }
 selsort(a,n);
 printf("\n\nSorted array : ");
 for(i=0;i<n;i++)
 printf("  %d",a[i]);
 getch();
}

Q.3) WRITE A PROGRAM IN C TO SORT AN ARRAY USING QUICK  SORT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void quicksort(int *,int,int);
int split(int *,int,int);

void main()
{
                int *a,i,n;
                clrscr();
                printf("Enter the no. of  elements in the array: ");
                scanf("%d",&n);
                a = (int *)malloc(n*sizeof(int));
                printf("\nEnter the elements of the array : \n");
                for(i=0;i<n;i++)
                 scanf("%d",&a[i]);

                quicksort(a,0,n-1);
                printf("\n\nThe array after sorting is :");
                for(i=0;i<n;i++)
                   printf("  %d",a[i]);

                getch();
}

void quicksort(int a[],int lower,int upper)
{
                int i;
                if(upper>lower)
                {
                                i=split(a,lower,upper);
                                quicksort(a,lower,i-1);
                                quicksort(a,i+1,upper);
                }
}

int split(int a[],int lower,int upper)
{
                int i,p,q,t;
                p=lower+1;
                q=upper;
                i=a[lower];

                while(q>=p)
                {
                                while(a[p]<i)
                                p++;

                                while(a[q]>i)
                                q--;

                                if(q>p)
                                {
                                                t=a[p];
                                                a[p]=a[q];
                                                a[q]=t;
                                }
                }
                t=a[lower];
                a[lower]=a[q];
                a[q]=t;
                return q;
}

Q.4) WRITE A PROGRAM IN C TO IMPLEMENT BINARY SEARCH IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
 int *a, i, n, x, low, mid, high;
 clrscr();

 printf("Enter the length of the array : ");
 scanf("%d",&n);
 a = (int *)malloc(n * sizeof(int));

 printf("\n\nEnter the Array elements in ASCENDING ORDER : \n");
 for(i=0; i<n;i++)
                scanf("%d",&a[i]);

 printf("\n\nEnter the element to be searched\n");
 scanf("%d", &x);

 low=1;
 high=n;

 do
  {
                mid= (low + high) / 2;
                if ( x < a[mid] )
                                high = mid - 1;
                else if ( x > a[mid])
                                low = mid + 1;
  } while( x!=a[mid] && low <= high);

 if( x == a[mid] )
                printf("\n\n....SUCCESSFUL SEARCH....\nIt is at position %d",(mid+1));

 else
                printf("\n\nSearch is FAILED");
}

Q.5) WRITE A PROGRAM IN C TO IMPLEMENT STACK IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void stackpush (int *a,int &n)
{
 int i,x;
 if(n>10)
  {
  printf("Cannot be inserted");
  return;
  }
 n++;

  printf("\nEnter the element to be inserted : ");
  scanf("%d",&x);
  a[n-1]=x;
  printf("The array now is : ");
  for(i=0;i<n;i++)
  printf("%d ",a[i]);
  getch();
}

int stackpop (int a[],int &n)
{
 int x;
 if(n==0)
  printf("Stack Underflow ");

 else
  {
  x=a[n-1];
  n--;
  }
 return(x);
}



void main()
{
 int *a,n,c,i,x;
 a = (int *)malloc(10 * sizeof(int));
 clrscr();
 printf("Enter no. of elements of array : ");
 scanf("%d",&n);

 printf("\nEnter the array elements:\n");
 for (i=0;i<n;i++)
  scanf("%d",&a[i]);

 st:

 clrscr();
 printf("\n\t1.Stack Push");
 printf("\n\t2.Stack Pop");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);
 switch (c)
 {
  case 1 : stackpush(a,n);
                   break;

  case 2 : x=stackpop(a,n);
                   printf("\nThe element popped out is %d",x);
                   getch();
                   break;

  case 3 : printf("\n\nThe elements in the array : ");
                   for(i=0;i<n;i++)
                   printf("%d ",a[i]);
                   getch();
                   break;

   case 4 : exit(0);

   default : printf("\n\n....Wrong Choice ....");
                     getch();
                     break;
  }
  goto st;
}


Q.6)  WRITE A PROGRAM IN C TO IMPLEMENT STACK IN A LINKED LIST.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct linkedlist
{
 int data;
 struct linkedlist *link;
}node;

node *top=NULL;

void stackpush()
{
 node *temp;
 temp=(node *)malloc(sizeof(node));
 printf("\nEnter the node data : ");
 scanf("%d",&temp->data);
 temp->link=top;
 top=temp;
}


int stackpop()
{
 node *p,*pre;
 int temp;
 p=top;
 if(p->link==NULL)
  {
                free(p);
                top=NULL;
                return 0;
  }
 else
  {
                pre=p;
                p=p->link;
                top=p;
                temp=pre->data;
                free(pre);
                return(temp);
  }

}

void display()
{
 node *ptr;
 int i=0;
 if(top==NULL)
                printf("\n....Empty list....");
 else
  {
                ptr=top;
                while(ptr)
                 {
                  printf("\nNode %d data : %d",i+1,ptr->data);
                  ptr=ptr->link;
                  i++;
                 }
  }
 getch();
}


void main()
{
 int c,x;
 clrscr();

 st:

 clrscr();
 printf("\n\t1.Stack Push");
 printf("\n\t2.Stack Pop");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);

 switch (c)
 {
  case 1 : stackpush();
                                                  break;

  case 2 : x=stackpop();
                                                  printf("\nThe element popped out is %d",x);
                                                  getch();
                                                  break;

  case 3 : display();
                                                  break;

  case 4 : exit(0);

  default : printf("\n\n....Wrong Choice ....");
                                                                getch();
 }

 goto st;
}




Q.7) WRITE PROGRAM IN C TO IMPLEMENT QUEUE IN AN ARRAY.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void queueins (int *a,int &n)
{
 int i,x;
 if(n>10)
  {
  printf("Cannot be inserted");
  return;
  }
  n++;

  printf("\nEnter the element to be inserted : ");
  scanf("%d",&x);
  a[n-1]=x;
  printf("The array now is : ");
  for(i=0;i<n;i++)
  printf("%d ",a[i]);
  getch();
}

int queuedel (int a[],int &n)
{
 int i,x;
 if(n==0)
  printf("Queue Underflow ");

 else
  {
   x=a[0];
   for(i=0;i<n;i++)
   a[i]=a[i+1];
   n--;
   }
  return(x);
}



void main()
{
 int *a,n,c,i,x;
 a = (int *)malloc(10 * sizeof(int));
 clrscr();
 printf("Enter no. of elements of array : ");
 scanf("%d",&n);

 printf("\nEnter the array elements:\n");
 for (i=0;i<n;i++)
  scanf("%d",&a[i]);

 st:

 clrscr();
 printf("\n\t1.Queue Insert");
 printf("\n\t2.Queue Delete");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);

 switch (c)
 {
  case 1 : queueins(a,n);
                   break;

  case 2 : x=queuedel(a,n);
                   printf("\nThe element deleted is %d",x);
                   getch();
                   break;

  case 3 : printf("\n\nThe elements in the array : ");
                   for(i=0;i<n;i++)
                   printf("%d ",a[i]);
                   getch();
                   break;

   case 4 : exit(0);

   default : printf("\n\n....Wrong Choice ....");
                     getch();
                     break;
  }

  goto st;
}





Q.8) WRITE PROGRAM IN C TO IMPLEMENT QUEUE IN A LINKED LIST.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct linkedlist
{
 int data;
 struct linkedlist *link;
}node;

node *top=NULL;

void qins()
{
 node *temp,*ptr;
 temp=(node *)malloc(sizeof(node));
 printf("Enter the node data : ");
 scanf("%d",&temp->data);
 if(top==NULL)
                {
                temp->link=NULL;
                top=temp;
                }
 else
                {
                 ptr=top;
                 while(ptr->link)
                                ptr=ptr->link;
                 temp->link=NULL;
                 ptr->link=temp;

                }
}


int qdel()
{
 node *p,*pre;
 int temp;
 p=top;
 if(p->link==NULL)
  {
                free(p);
                top=NULL;
                return 0;
  }
 else
  {
                pre=p;
                p=p->link;
                top=p;
                temp=pre->data;
                free(pre);
                return(temp);
  }

}

void display()
{
 node *ptr;
 int i=0;
 if(top==NULL)
                printf("\n....Empty list....");
 else
  {
                ptr=top;
                while(ptr)
                 {
                  printf("\nNode %d data : %d",i+1,ptr->data);
                  ptr=ptr->link;
                  i++;
                 }
  }
 getch();
}


void main()
{
 int c,x;
 clrscr();

 st:

 clrscr();
 printf("\n\t1.Queue Insert");
 printf("\n\t2.Queue Delete");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);
 switch (c)
 {
  case 1 : qins();
                                                  break;

  case 2 : x=qdel();
                                                  printf("\nThe element deleted is %d",x);
                                                  getch();
                                                  break;

  case 3 : display();
                                                  break;

  case 4 : exit(0);

  default : printf("\n\n....Wrong Choice ....");
                                                                getch();
 }
 goto st;
}


Q.9) WRITE PROGRAM IN C TO IMPLEMENT DOUBLE-ENDED QUEUE IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10


int count(int *arr)
{
 int c=0,i;

 for(i=0;i<max;i++)
 {
  if(arr[i]!=0)
    c++;
 }

 return c;
}


void addatbeg(int *arr,int item,int *pfront,int *prear)
{
 int i,k,c;
 if(*pfront==0 && *prear==max-1)
 {
  printf("\nDeque is Full...\n");
  return;
 }

 if(*pfront==-1)
 {
  *pfront=*prear=0;
  arr[*pfront]=item;
  return;
 }

 if(*prear!=max-1)
 {
    c=count(arr);
    k=*prear+1;

    for(i=1;i<=c;i++)
    {
     arr[k]=arr[k-1];
     k--;
    }
    arr[k]=item;
    *pfront=k;
    (*prear)++;
 }

 else
  {
    (*pfront)--;
    arr[*pfront]+item;
  }
}

void addatend (int *arr,int item,int *pfront,int *prear)
{
 int i,k;

 if (*pfront==0 && *prear==max-1)
 {
  printf("\nDeque s full..");
  return;
 }

 if(*pfront == -1)
 {
  *prear = *pfront=0;
  arr[*prear]= item;
  return;
 }

 if(*prear==max-1)
 {
   k=*pfront-1;

   for(i=*pfront-1;i<*prear;i++)
    {
     k=i;
     if(k==max-1)
      arr[k]=0;
     else
      arr[k]=arr[i+1];
     }

   (*prear)--;
   (*pfront)--;
 }

 (*prear)++;
 arr[*prear]=item;
}

int delatbeg (int *arr,int *pfront,int *prear)
{
 int item;
 if(*pfront==-1)
  {
   printf("\nDeque is Empty...");
   return (0);
  }

 item = arr[*pfront];
 arr[*pfront]=0;

 if(*pfront==*prear)
   *pfront=*prear=-1;

 else
  ( *pfront)++;

 return item;
}


int delatend (int *arr,int *pfront,int *prear)
{
 int item;
 if(*pfront==-1)
  {
   printf("\nDeque is Empty...");
   return (0);
  }

 item = arr[*prear];
 arr[*prear]=0;
 (*prear)--;

 if(*prear==-1)
   *pfront=-1;

 return item;
}

void display(int *arr,int *pfront,int *prear)
{
 int i;

 printf("\nfront->  ");
 for(i=(*pfront);i<=(*prear);i++)
  printf("  %d",arr[i]);
 printf("  <-rear");
}




void main()
{
 int a[max];
 int front=-1,rear=-1,i,x,aa,bb;
 for(i=0;i<max;i++)
    a[i]= 0;
 top:
 clrscr();
 printf("\n\t1.Add at Beg");
 printf("\n\t2.Add at End");
 printf("\n\t3.Delete from Beg");
 printf("\n\t4.Delete from End");
 printf("\n\t5.Display");
 printf("\n\t...Press other key for exit...");
 printf("\n\nEnter your choice : ");
 scanf("%d",&x);
 switch (x)
 {
  case 1 : printf("\nEnter element : ");
                   scanf("%d",&aa);
                   addatbeg(a,aa,&front,&rear) ;
                   printf("\n....Item Inserted at Beg....");
                   getch();
                   break;

  case 2:  printf("\nEnter element : ");
                   scanf("%d",&bb);
                   addatend(a,bb,&front,&rear) ;
                   printf("\n....Item Inserted at End....");
                   getch();
                   break;

  case 5:   printf("\nElements in a Deque : ");
                    display(a,&front,&rear);
                    getch();
                    break;

  case 3:   i=delatbeg(a,&front,&rear);
                    printf("\nItem Extracted : %d",i);
                    getch();
                    break;

  case 4:   i=delatend(a,&front,&rear);
                    printf("\nItem Extracted : %d",i);
                    getch();
                    break;

  default : exit(0);
 }
goto top;
}


Q.10) WRITE PROGRAM IN C TO IMPLEMENT PRIORITY QUEUE IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert();
void del();
void display();

struct node
 {
  int priority;
  int info;
  struct node *next;
 }*start,*q,*temp,*a;

void main()
{
 int ch;
 top :
 clrscr();
 printf("\n\n\t[1] INSERTION\n\t[2] DELETION\n\t[3] DISPLAY\n\t[4] EXIT\n");
 printf("\nEnter your Choice : ");
 scanf("%d",&ch);
 switch(ch)
  {
   case 1 : insert();
                    break;

   case 2 : del();
                    break;

   case 3 : display();
                    break;

   case 4 : exit(0);

   default: printf("\n\n....Wrong Choice....");
                    getch();
  }
  goto top;
}

void insert()
{
 int item,pr;
 a=(struct node *)malloc(sizeof(struct node));
 printf("\n\nEnter the element to be inserted : ");
 scanf("%d",&item);
 printf("\nEnter its Priority : ");
 scanf("%d",&pr);
 a->info=item;
 a->priority=pr;

 if(start==NULL || pr<(start->priority))
  {
   a->next=start;
   start=a;
  }

 else
  {
   q=start;

   while(q->next != NULL && (q->next->priority) <= pr)
   q=q->next;

   a->next=q->next;
   q->next=a;
  }
}

void del()
{
 if(start==NULL)
    printf("\n....UnderFlow\n");

 else
 {
  a=start;
  printf("\nDeleted item is %d",a->info);
  start=start->next;
  free(start);
 }
}

void display()
{
 temp=start;
 if(start==NULL)
   printf("\n....Queue is Empty....");

 else
  {
   printf("\n\n\nQueue is as follows :\n");
   printf("\n\n\t\t  ITEM  || PRIORITY\n");
   printf("\t\t---------------------");
   while(temp!=NULL)
    {
     printf("\n\t\t   %2d   ||   %2d",temp->info,temp->priority);
     temp=temp->next;
    }
  }
  getch();
}

Q.11)WRITE A PROGRAM IN C TO CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION.



#include<stdio.h>
#include<conio.h>
#include<ctype.h>

char in[20],post[20],stack[20];
int top=-1,p=-1;

void push(char c)
                {
                stack[++top]=c;
                }


char pop()
                {
                return stack[top--];
                }

int pred(char c)
                {
                int p;
                switch(c)
                                {
                                case '^':p=3;
                                break;
                                case '%':
                                case '/':
                                case '*':p=2;
                                break;
                                case '+':
                                case '-':p=1;
                                break;
                                default :p=0;
                                break;

                                }
                return p;
                }


void main()
                {
                int i;
                char c;
                clrscr();

                printf("\n Enter the Infix Expression : ");
                gets(in);
                for(i=0;in[i];i++)
                                {
                                if(isalpha(in[i]))
                                                post[++p]=in[i];
                                else
                                                {
                                                if(in[i]=='(')
                                                                push(in[i]);
                                                else if(in[i]==')')
                                                                {
                                                                while(stack[top]!='(')
                                                                                post[++p]=pop();
                                                                top--;
                                                                }
                                                else
                                                                {
                                                                                while((pred(in[i])<=pred(stack[top]))&&(top>-1))
                                                                                {
                                                                                post[++p]=pop();
                                                                                }
                                                                                push(in[i]);

                                                                }
                                                }
                                }

                while(top>=0)
                                                post[++p]=pop();


                post[++p]=NULL;
                printf("\n\nThe Postfix Expression is : ");
                for(i=0;post[i];i++)
                                printf("%c",post[i]);
                getch();
                }





Q.12)WRITE A PROGRAM IN C TO CONVERT INFIX EXPRESSION TO PREFIX EXPRESSION.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define max 30
#define operand 10
#define OPERATOR 20
#define leftpara 30
#define rightpara 40
typedef struct prestk
{
int top;
char stack[max];
}stack;
void init(stack *st)
{
                st->top=-1;
}
void push(stack *st,char c)
{
st->top++;
st->stack[st->top]=c;
}
char pop(stack *st)
{
                char c;
                c=st->stack[st->top];
                st->top--;
                return c;
}
int getprec(char c)
{
                switch(c)
                {
                                case ')' : return 0;
                                case '+' :
                                case '-' : return 1;
                                case '*' :
                                case '/' :
                                case '%' : return 2;
                                case '^' : return 3;
                }
}
int gettype(char c)
{
                switch(c)
                {
                                case '+':
                                case '-':
                                case '*':
                                case '/':
                                case '^':
                                case '%': return OPERATOR;
                                case '(': return leftpara;
                                case ')': return rightpara;
                                default : return operand;
                }
}
void main()
{
stack stk;
char inf[max],ch,pre[max];
int l,i,k=0,pr;
clrscr();
init(&stk);
printf("\nEnter an infix expression : ");
gets(inf);
l=strlen(inf);
for(i=l-1;i>=0;i--)
{
                switch(gettype(inf[i]))
                {
                                case operand : pre[k++]=inf[i];
                                break;
                                case OPERATOR : pr=getprec(inf[i]);
                                while(pr<getprec(stk.stack[stk.top]) && stk.top!=-1)
                                                pre[k++]=pop(&stk);
                                push(&stk,inf[i]);
                                break;
                                case rightpara : push(&stk,inf[i]);
                                break;
                                case leftpara : while((ch=pop(&stk))!=')')
                                                                                pre[k++]=ch;
                }
}
printf("\n\nThe corresponing Prefix Expression is : ");
while(stk.top!=-1)
                pre[k++]=pop(&stk);
pre[k]='\0';
strrev(pre);
puts(pre);
getch();
}



Q.13)WRITE A PROGRAM IN C TO EVALUATE A POSTFIX EXPRESSION.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define MAX 50

struct postfix
{
 int stack[MAX];
 int top,nn;
 char *s;
};
void initpostfix(struct postfix*);
void setexpr(struct postfix*,char*);
void push(struct postfix*,int);
int pop(struct postfix*);
void calculate(struct postfix*);
void show(struct postfix);
void main()
{
 struct postfix q;
 char expr[MAX];
 clrscr();
 initpostfix(&q);
 printf ("\nEnter the Postfix Expression to be evaluated : ");
 gets(expr);
 setexpr(&q,expr);
 calculate(&q);
 show(q);
 getch();
}
void initpostfix(struct postfix*p)
{
p->top=-1;
}

void setexpr(struct postfix*p,char *str)
{
p->s=str;
}

void push(struct postfix*p, int item)
{
 if(p->top==MAX-1)
 printf("stack is full\n");

 else
 {
 p->top++;
 p->stack[p->top]=item;
 }
}
 int pop(struct postfix*p)
{
 int data;
 if(p->top==-1)
 {
 printf("stack is empty");
 return NULL;
 }
 data=p->stack[p->top];
 p->top--;
 return data;
}
void calculate(struct postfix*p)
{
                int n1,n2,n3;
                while(*(p->s))
                 {
                  if(*p->s==' '||*(p->s)=='\t')
                  {  p->s++;
                  continue;
                  }
                 if(isdigit(*(p->s)))
                 {
                 p->nn=*(p->s)-'0';
                 push(p,p->nn);
                 }
                 else
                 {
                  n1=pop(p);
                  n2=pop(p);
                  switch(*(p->s))
                     {
                                case '+':   n3=n2+n1;
                                                 break;
                                case '-':   n3=n2-n1;
                                                break;
                                case '/': n3=n2/n1;
                                                break;
                                case '*':  n3=n2*n1;
                                                break;
                                case '%':  n3=n2%n1;
                                                 break;
                                case '^':   n3=pow(n2,n1);
                                                break;
                                default:                   printf(" unknown operator");
                                                 exit(1);
                     }
                  push(p,n3);
                  }
                  p->s++;
                  }
                  }

 void show(struct postfix p)
  {
                                p.nn=pop(&p);
                                printf("\n\nResult = %d",p.nn);
 }








 Q.1) WRITE A PROGRAM IN C TO SORT AN ARRAY USING INSERTION SORT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void insort(int a[], int size)
{
 int i, j, t;
 for (i=1; i<size; i++)
  {
                                t = a[i];
                                j = i;
                                while ((j > 0) && (a[j-1] > t))
                                 {
                                  a[j] = a[j-1];
                                  j = j - 1;
                                 }
                                a[j] = t;
  }
}


void main()
{
 int *a,i,n;
 printf("\nEnter the length of the array :");
 scanf("%d",&n);
 a=(int*)malloc(n * sizeof(int));
 printf("\nEnter the elements of the array :\n");
 for(i=0;i<n;i++)
  {
                printf("Enter %d element : ",i+1);
                scanf("%d",&a[i]);
  }
 insort(a,n);
 printf("\n\nSorted array : ");
 for(i=0;i<n;i++)
 printf("  %d",a[i]);
 getch();
}



Q.2) WRITE A PROGRAM IN C TO SORT AN ARRAY USING SELECTION SORT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


void selsort(int a[], int size)
{
 int i, j, min, temp;
 for (i=0; i<(size-1); i++)
  {
                min = i;
                for (j=i+1; j<size; j++)
                 {
                  if (a[j] < a[min])
                  min = j;
                 }
                temp = a[i];
                a[i] = a[min];
                a[min] = temp;
  }
}

void main()
{
 int *a,i,n;
 printf("\nEnter the length of the array :");
 scanf("%d",&n);
 a=(int*)malloc(n * sizeof(int));
 printf("\nEnter the elements of the array :\n");
 for(i=0;i<n;i++)
  {
                printf("Enter %d element : ",i+1);
                scanf("%d",&a[i]);
  }
 selsort(a,n);
 printf("\n\nSorted array : ");
 for(i=0;i<n;i++)
 printf("  %d",a[i]);
 getch();
}

Q.3) WRITE A PROGRAM IN C TO SORT AN ARRAY USING QUICK  SORT.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void quicksort(int *,int,int);
int split(int *,int,int);

void main()
{
                int *a,i,n;
                clrscr();
                printf("Enter the no. of  elements in the array: ");
                scanf("%d",&n);
                a = (int *)malloc(n*sizeof(int));
                printf("\nEnter the elements of the array : \n");
                for(i=0;i<n;i++)
                 scanf("%d",&a[i]);

                quicksort(a,0,n-1);
                printf("\n\nThe array after sorting is :");
                for(i=0;i<n;i++)
                   printf("  %d",a[i]);

                getch();
}

void quicksort(int a[],int lower,int upper)
{
                int i;
                if(upper>lower)
                {
                                i=split(a,lower,upper);
                                quicksort(a,lower,i-1);
                                quicksort(a,i+1,upper);
                }
}

int split(int a[],int lower,int upper)
{
                int i,p,q,t;
                p=lower+1;
                q=upper;
                i=a[lower];

                while(q>=p)
                {
                                while(a[p]<i)
                                p++;

                                while(a[q]>i)
                                q--;

                                if(q>p)
                                {
                                                t=a[p];
                                                a[p]=a[q];
                                                a[q]=t;
                                }
                }
                t=a[lower];
                a[lower]=a[q];
                a[q]=t;
                return q;
}

Q.4) WRITE A PROGRAM IN C TO IMPLEMENT BINARY SEARCH IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
 int *a, i, n, x, low, mid, high;
 clrscr();

 printf("Enter the length of the array : ");
 scanf("%d",&n);
 a = (int *)malloc(n * sizeof(int));

 printf("\n\nEnter the Array elements in ASCENDING ORDER : \n");
 for(i=0; i<n;i++)
                scanf("%d",&a[i]);

 printf("\n\nEnter the element to be searched\n");
 scanf("%d", &x);

 low=1;
 high=n;

 do
  {
                mid= (low + high) / 2;
                if ( x < a[mid] )
                                high = mid - 1;
                else if ( x > a[mid])
                                low = mid + 1;
  } while( x!=a[mid] && low <= high);

 if( x == a[mid] )
                printf("\n\n....SUCCESSFUL SEARCH....\nIt is at position %d",(mid+1));

 else
                printf("\n\nSearch is FAILED");
}

Q.5) WRITE A PROGRAM IN C TO IMPLEMENT STACK IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void stackpush (int *a,int &n)
{
 int i,x;
 if(n>10)
  {
  printf("Cannot be inserted");
  return;
  }
 n++;

  printf("\nEnter the element to be inserted : ");
  scanf("%d",&x);
  a[n-1]=x;
  printf("The array now is : ");
  for(i=0;i<n;i++)
  printf("%d ",a[i]);
  getch();
}

int stackpop (int a[],int &n)
{
 int x;
 if(n==0)
  printf("Stack Underflow ");

 else
  {
  x=a[n-1];
  n--;
  }
 return(x);
}



void main()
{
 int *a,n,c,i,x;
 a = (int *)malloc(10 * sizeof(int));
 clrscr();
 printf("Enter no. of elements of array : ");
 scanf("%d",&n);

 printf("\nEnter the array elements:\n");
 for (i=0;i<n;i++)
  scanf("%d",&a[i]);

 st:

 clrscr();
 printf("\n\t1.Stack Push");
 printf("\n\t2.Stack Pop");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);
 switch (c)
 {
  case 1 : stackpush(a,n);
                   break;

  case 2 : x=stackpop(a,n);
                   printf("\nThe element popped out is %d",x);
                   getch();
                   break;

  case 3 : printf("\n\nThe elements in the array : ");
                   for(i=0;i<n;i++)
                   printf("%d ",a[i]);
                   getch();
                   break;

   case 4 : exit(0);

   default : printf("\n\n....Wrong Choice ....");
                     getch();
                     break;
  }
  goto st;
}


Q.6)  WRITE A PROGRAM IN C TO IMPLEMENT STACK IN A LINKED LIST.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct linkedlist
{
 int data;
 struct linkedlist *link;
}node;

node *top=NULL;

void stackpush()
{
 node *temp;
 temp=(node *)malloc(sizeof(node));
 printf("\nEnter the node data : ");
 scanf("%d",&temp->data);
 temp->link=top;
 top=temp;
}


int stackpop()
{
 node *p,*pre;
 int temp;
 p=top;
 if(p->link==NULL)
  {
                free(p);
                top=NULL;
                return 0;
  }
 else
  {
                pre=p;
                p=p->link;
                top=p;
                temp=pre->data;
                free(pre);
                return(temp);
  }

}

void display()
{
 node *ptr;
 int i=0;
 if(top==NULL)
                printf("\n....Empty list....");
 else
  {
                ptr=top;
                while(ptr)
                 {
                  printf("\nNode %d data : %d",i+1,ptr->data);
                  ptr=ptr->link;
                  i++;
                 }
  }
 getch();
}


void main()
{
 int c,x;
 clrscr();

 st:

 clrscr();
 printf("\n\t1.Stack Push");
 printf("\n\t2.Stack Pop");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);

 switch (c)
 {
  case 1 : stackpush();
                                                  break;

  case 2 : x=stackpop();
                                                  printf("\nThe element popped out is %d",x);
                                                  getch();
                                                  break;

  case 3 : display();
                                                  break;

  case 4 : exit(0);

  default : printf("\n\n....Wrong Choice ....");
                                                                getch();
 }

 goto st;
}




Q.7) WRITE PROGRAM IN C TO IMPLEMENT QUEUE IN AN ARRAY.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void queueins (int *a,int &n)
{
 int i,x;
 if(n>10)
  {
  printf("Cannot be inserted");
  return;
  }
  n++;

  printf("\nEnter the element to be inserted : ");
  scanf("%d",&x);
  a[n-1]=x;
  printf("The array now is : ");
  for(i=0;i<n;i++)
  printf("%d ",a[i]);
  getch();
}

int queuedel (int a[],int &n)
{
 int i,x;
 if(n==0)
  printf("Queue Underflow ");

 else
  {
   x=a[0];
   for(i=0;i<n;i++)
   a[i]=a[i+1];
   n--;
   }
  return(x);
}



void main()
{
 int *a,n,c,i,x;
 a = (int *)malloc(10 * sizeof(int));
 clrscr();
 printf("Enter no. of elements of array : ");
 scanf("%d",&n);

 printf("\nEnter the array elements:\n");
 for (i=0;i<n;i++)
  scanf("%d",&a[i]);

 st:

 clrscr();
 printf("\n\t1.Queue Insert");
 printf("\n\t2.Queue Delete");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);

 switch (c)
 {
  case 1 : queueins(a,n);
                   break;

  case 2 : x=queuedel(a,n);
                   printf("\nThe element deleted is %d",x);
                   getch();
                   break;

  case 3 : printf("\n\nThe elements in the array : ");
                   for(i=0;i<n;i++)
                   printf("%d ",a[i]);
                   getch();
                   break;

   case 4 : exit(0);

   default : printf("\n\n....Wrong Choice ....");
                     getch();
                     break;
  }

  goto st;
}





Q.8) WRITE PROGRAM IN C TO IMPLEMENT QUEUE IN A LINKED LIST.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct linkedlist
{
 int data;
 struct linkedlist *link;
}node;

node *top=NULL;

void qins()
{
 node *temp,*ptr;
 temp=(node *)malloc(sizeof(node));
 printf("Enter the node data : ");
 scanf("%d",&temp->data);
 if(top==NULL)
                {
                temp->link=NULL;
                top=temp;
                }
 else
                {
                 ptr=top;
                 while(ptr->link)
                                ptr=ptr->link;
                 temp->link=NULL;
                 ptr->link=temp;

                }
}


int qdel()
{
 node *p,*pre;
 int temp;
 p=top;
 if(p->link==NULL)
  {
                free(p);
                top=NULL;
                return 0;
  }
 else
  {
                pre=p;
                p=p->link;
                top=p;
                temp=pre->data;
                free(pre);
                return(temp);
  }

}

void display()
{
 node *ptr;
 int i=0;
 if(top==NULL)
                printf("\n....Empty list....");
 else
  {
                ptr=top;
                while(ptr)
                 {
                  printf("\nNode %d data : %d",i+1,ptr->data);
                  ptr=ptr->link;
                  i++;
                 }
  }
 getch();
}


void main()
{
 int c,x;
 clrscr();

 st:

 clrscr();
 printf("\n\t1.Queue Insert");
 printf("\n\t2.Queue Delete");
 printf("\n\t3.Display");
 printf("\n\t4.Exit");
 printf("\n\nEnter your Choice : ");
 scanf("%d",&c);
 switch (c)
 {
  case 1 : qins();
                                                  break;

  case 2 : x=qdel();
                                                  printf("\nThe element deleted is %d",x);
                                                  getch();
                                                  break;

  case 3 : display();
                                                  break;

  case 4 : exit(0);

  default : printf("\n\n....Wrong Choice ....");
                                                                getch();
 }
 goto st;
}


Q.9) WRITE PROGRAM IN C TO IMPLEMENT DOUBLE-ENDED QUEUE IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10


int count(int *arr)
{
 int c=0,i;

 for(i=0;i<max;i++)
 {
  if(arr[i]!=0)
    c++;
 }

 return c;
}


void addatbeg(int *arr,int item,int *pfront,int *prear)
{
 int i,k,c;
 if(*pfront==0 && *prear==max-1)
 {
  printf("\nDeque is Full...\n");
  return;
 }

 if(*pfront==-1)
 {
  *pfront=*prear=0;
  arr[*pfront]=item;
  return;
 }

 if(*prear!=max-1)
 {
    c=count(arr);
    k=*prear+1;

    for(i=1;i<=c;i++)
    {
     arr[k]=arr[k-1];
     k--;
    }
    arr[k]=item;
    *pfront=k;
    (*prear)++;
 }

 else
  {
    (*pfront)--;
    arr[*pfront]+item;
  }
}

void addatend (int *arr,int item,int *pfront,int *prear)
{
 int i,k;

 if (*pfront==0 && *prear==max-1)
 {
  printf("\nDeque s full..");
  return;
 }

 if(*pfront == -1)
 {
  *prear = *pfront=0;
  arr[*prear]= item;
  return;
 }

 if(*prear==max-1)
 {
   k=*pfront-1;

   for(i=*pfront-1;i<*prear;i++)
    {
     k=i;
     if(k==max-1)
      arr[k]=0;
     else
      arr[k]=arr[i+1];
     }

   (*prear)--;
   (*pfront)--;
 }

 (*prear)++;
 arr[*prear]=item;
}

int delatbeg (int *arr,int *pfront,int *prear)
{
 int item;
 if(*pfront==-1)
  {
   printf("\nDeque is Empty...");
   return (0);
  }

 item = arr[*pfront];
 arr[*pfront]=0;

 if(*pfront==*prear)
   *pfront=*prear=-1;

 else
  ( *pfront)++;

 return item;
}


int delatend (int *arr,int *pfront,int *prear)
{
 int item;
 if(*pfront==-1)
  {
   printf("\nDeque is Empty...");
   return (0);
  }

 item = arr[*prear];
 arr[*prear]=0;
 (*prear)--;

 if(*prear==-1)
   *pfront=-1;

 return item;
}

void display(int *arr,int *pfront,int *prear)
{
 int i;

 printf("\nfront->  ");
 for(i=(*pfront);i<=(*prear);i++)
  printf("  %d",arr[i]);
 printf("  <-rear");
}




void main()
{
 int a[max];
 int front=-1,rear=-1,i,x,aa,bb;
 for(i=0;i<max;i++)
    a[i]= 0;
 top:
 clrscr();
 printf("\n\t1.Add at Beg");
 printf("\n\t2.Add at End");
 printf("\n\t3.Delete from Beg");
 printf("\n\t4.Delete from End");
 printf("\n\t5.Display");
 printf("\n\t...Press other key for exit...");
 printf("\n\nEnter your choice : ");
 scanf("%d",&x);
 switch (x)
 {
  case 1 : printf("\nEnter element : ");
                   scanf("%d",&aa);
                   addatbeg(a,aa,&front,&rear) ;
                   printf("\n....Item Inserted at Beg....");
                   getch();
                   break;

  case 2:  printf("\nEnter element : ");
                   scanf("%d",&bb);
                   addatend(a,bb,&front,&rear) ;
                   printf("\n....Item Inserted at End....");
                   getch();
                   break;

  case 5:   printf("\nElements in a Deque : ");
                    display(a,&front,&rear);
                    getch();
                    break;

  case 3:   i=delatbeg(a,&front,&rear);
                    printf("\nItem Extracted : %d",i);
                    getch();
                    break;

  case 4:   i=delatend(a,&front,&rear);
                    printf("\nItem Extracted : %d",i);
                    getch();
                    break;

  default : exit(0);
 }
goto top;
}


Q.10) WRITE PROGRAM IN C TO IMPLEMENT PRIORITY QUEUE IN AN ARRAY.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert();
void del();
void display();

struct node
 {
  int priority;
  int info;
  struct node *next;
 }*start,*q,*temp,*a;

void main()
{
 int ch;
 top :
 clrscr();
 printf("\n\n\t[1] INSERTION\n\t[2] DELETION\n\t[3] DISPLAY\n\t[4] EXIT\n");
 printf("\nEnter your Choice : ");
 scanf("%d",&ch);
 switch(ch)
  {
   case 1 : insert();
                    break;

   case 2 : del();
                    break;

   case 3 : display();
                    break;

   case 4 : exit(0);

   default: printf("\n\n....Wrong Choice....");
                    getch();
  }
  goto top;
}

void insert()
{
 int item,pr;
 a=(struct node *)malloc(sizeof(struct node));
 printf("\n\nEnter the element to be inserted : ");
 scanf("%d",&item);
 printf("\nEnter its Priority : ");
 scanf("%d",&pr);
 a->info=item;
 a->priority=pr;

 if(start==NULL || pr<(start->priority))
  {
   a->next=start;
   start=a;
  }

 else
  {
   q=start;

   while(q->next != NULL && (q->next->priority) <= pr)
   q=q->next;

   a->next=q->next;
   q->next=a;
  }
}

void del()
{
 if(start==NULL)
    printf("\n....UnderFlow\n");

 else
 {
  a=start;
  printf("\nDeleted item is %d",a->info);
  start=start->next;
  free(start);
 }
}

void display()
{
 temp=start;
 if(start==NULL)
   printf("\n....Queue is Empty....");

 else
  {
   printf("\n\n\nQueue is as follows :\n");
   printf("\n\n\t\t  ITEM  || PRIORITY\n");
   printf("\t\t---------------------");
   while(temp!=NULL)
    {
     printf("\n\t\t   %2d   ||   %2d",temp->info,temp->priority);
     temp=temp->next;
    }
  }
  getch();
}

Q.11)WRITE A PROGRAM IN C TO CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION.



#include<stdio.h>
#include<conio.h>
#include<ctype.h>

char in[20],post[20],stack[20];
int top=-1,p=-1;

void push(char c)
                {
                stack[++top]=c;
                }


char pop()
                {
                return stack[top--];
                }

int pred(char c)
                {
                int p;
                switch(c)
                                {
                                case '^':p=3;
                                break;
                                case '%':
                                case '/':
                                case '*':p=2;
                                break;
                                case '+':
                                case '-':p=1;
                                break;
                                default :p=0;
                                break;

                                }
                return p;
                }


void main()
                {
                int i;
                char c;
                clrscr();

                printf("\n Enter the Infix Expression : ");
                gets(in);
                for(i=0;in[i];i++)
                                {
                                if(isalpha(in[i]))
                                                post[++p]=in[i];
                                else
                                                {
                                                if(in[i]=='(')
                                                                push(in[i]);
                                                else if(in[i]==')')
                                                                {
                                                                while(stack[top]!='(')
                                                                                post[++p]=pop();
                                                                top--;
                                                                }
                                                else
                                                                {
                                                                                while((pred(in[i])<=pred(stack[top]))&&(top>-1))
                                                                                {
                                                                                post[++p]=pop();
                                                                                }
                                                                                push(in[i]);

                                                                }
                                                }
                                }

                while(top>=0)
                                                post[++p]=pop();


                post[++p]=NULL;
                printf("\n\nThe Postfix Expression is : ");
                for(i=0;post[i];i++)
                                printf("%c",post[i]);
                getch();
                }





Q.12)WRITE A PROGRAM IN C TO CONVERT INFIX EXPRESSION TO PREFIX EXPRESSION.


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 30
#define operand 10
#define OPERATOR 20
#define leftpara 30
#define rightpara 40
typedef struct prestk
{
int top;
char stack[max];
}stack;
void init(stack *st)
{
                st->top=-1;
}
void push(stack *st,char c)
{
st->top++;
st->stack[st->top]=c;
}
char pop(stack *st)
{
                char c;
                c=st->stack[st->top];
                st->top--;
                return c;
}
int getprec(char c)
{
                switch(c)
                {
                                case ')' : return 0;
                                case '+' :
                                case '-' : return 1;
                                case '*' :
                                case '/' :
                                case '%' : return 2;
                                case '^' : return 3;
                }
}
int gettype(char c)
{
                switch(c)
                {
                                case '+':
                                case '-':
                                case '*':
                                case '/':
                                case '^':
                                case '%': return OPERATOR;
                                case '(': return leftpara;
                                case ')': return rightpara;
                                default : return operand;
                }
}
void main()
{
stack stk;
char inf[max],ch,pre[max];
int l,i,k=0,pr;
clrscr();
init(&stk);
printf("\nEnter an infix expression : ");
gets(inf);
l=strlen(inf);
for(i=l-1;i>=0;i--)
{
                switch(gettype(inf[i]))
                {
                                case operand : pre[k++]=inf[i];
                                break;
                                case OPERATOR : pr=getprec(inf[i]);
                                while(pr<getprec(stk.stack[stk.top]) && stk.top!=-1)
                                                pre[k++]=pop(&stk);
                                push(&stk,inf[i]);
                                break;
                                case rightpara : push(&stk,inf[i]);
                                break;
                                case leftpara : while((ch=pop(&stk))!=')')
                                                                                pre[k++]=ch;
                }
}
printf("\n\nThe corresponing Prefix Expression is : ");
while(stk.top!=-1)
                pre[k++]=pop(&stk);
pre[k]='\0';
strrev(pre);
puts(pre);
getch();
} 

Q.13) PROGRAM IN C TO EVALUATE A POSTFIX EXPRESSION.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define MAX 50

struct postfix
{
 int stack[MAX];
 int top,nn;
 char *s;
};
void initpostfix(struct postfix*);
void setexpr(struct postfix*,char*);
void push(struct postfix*,int);
int pop(struct postfix*);
void calculate(struct postfix*);
void show(struct postfix);
void main()
{
 struct postfix q;
 char expr[MAX];
 clrscr();
 initpostfix(&q);
 printf ("\nEnter the Postfix Expression to be evaluated : ");
 gets(expr);
 setexpr(&q,expr);
 calculate(&q);
 show(q);
 getch();
}
void initpostfix(struct postfix*p)
{
p->top=-1;
}

void setexpr(struct postfix*p,char *str)
{
p->s=str;
}

void push(struct postfix*p, int item)
{
 if(p->top==MAX-1)
 printf("stack is full\n");

 else
 {
 p->top++;
 p->stack[p->top]=item;
 }
}
 int pop(struct postfix*p)
{
 int data;
 if(p->top==-1)
 {
 printf("stack is empty");
 return NULL;
 }
 data=p->stack[p->top];
 p->top--;
 return data;
}
void calculate(struct postfix*p)
{
                int n1,n2,n3;
                while(*(p->s))
                 {
                  if(*p->s==' '||*(p->s)=='\t')
                  {  p->s++;
                  continue;
                  }
                 if(isdigit(*(p->s)))
                 {
                 p->nn=*(p->s)-'0';
                 push(p,p->nn);
                 }
                 else
                 {
                  n1=pop(p);
                  n2=pop(p);
                  switch(*(p->s))
                     {
                                case '+':   n3=n2+n1;
                                                 break;
                                case '-':   n3=n2-n1;
                                                break;
                                case '/': n3=n2/n1;
                                                break;
                                case '*':  n3=n2*n1;
                                                break;
                                case '%':  n3=n2%n1;
                                                 break;
                                case '^':   n3=pow(n2,n1);
                                                break;
                                default:                   printf(" unknown operator");
                                                 exit(1);
                     }
                  push(p,n3);
                  }
                  p->s++;
                  }
                  }

 void show(struct postfix p)
  {
                                p.nn=pop(&p);
                                printf("\n\nResult = %d",p.nn);
 }
Read More...