SUBMITTED TO SUBMITTED BY
MOHIT SIR PARNEET KAUR
MCA 2nd
YEAR
ROLL NO: 201880032
DEPARTMENT OF COMPUTER SCIENCE
THAPAR INSTITUTE
OF ENGINEERING TECHNOLOGY
PATIALA
LIST OF PROGRAMS
Program of Insertion sort
Program of Selection Sort
Program of Bubble Sort
Program of Merging two arrays
Program of Multiplication of 2d
Program of Linear Search
Program of Binary Search
Program of Push and Pop operations in stack
using the application of string
Program of Quick Sort
Program of Infix to Postfix Evaluation
Program of Postfix Evaluation
Program of Tower of Hanoi
Program of Circular Queue
Program of Insertion and Deletion operation
in Queue
Program of insertion sort
#include<stdio.h>
int main()
{
int i, k, count, temp, number[100];
printf("Enter the number of the terms you want to insert: ");
scanf("%d",&count);
printf("Enter %d elements: n", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=1;i<count;i++){
temp=number[i];
k=i-1;
while((temp<number[k])&&(k>=0)){
number[k+1]=number[k];
k=k-1;
}
number[k+1]=temp;
}
printf("Number in ascending order");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Program of Selection Sort
#include<stdio.h>
int main(){
int i, k, count, temp, number[40];
printf("Enter the number of terms you want to insert: ");
scanf("%d",&count);
printf("Enter %d elements:n", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=0;i<count;i++){
for(k=i+1;k<count;k++){
if(number[i]>number[k]){
temp=number[i];
number[i]=number[k];
number[k]=temp;
}
}
}
printf("Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Program of Bubble Sort
#include<stdio.h>
int main(){
int count, temp, i, k, number[100];
printf("Enter the number of the terms you want to insert: ");
scanf("%d",&count);
printf("Enter %d numbers: n",count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=count-2;i>=0;i--){
for(k=0;k<=i;k++){
if(number[k]>number[k+1]){
temp=number[k];
number[k]=number[k+1];
number[k+1]=temp;
}
}
}
printf("Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Program of Merging two arrays
#include <stdio.h>
int main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
printf("n Enter size of array Array 1: ");
scanf("%d", &m);
printf("n Enter sorted elements of array 1: n");
for (i = 0; i < m; i++)
{
scanf("%d", &array1[i]);
}
printf("n Enter size of array 2: ");
scanf("%d", &n);
printf("n Enter sorted elements of array 2: n");
for (i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("n After merging: n");
for (i = 0; i < m + n; i++)
{
printf("n%d", array3[i]);
}
}
Program of Multiplication of 2d
# include <stdio.h>
# include <conio.h>
int main()
{
int mata[10][10], matb[10][10], matc[10][10] ;
int i, j, k, row1, col1, row2, col2 ;
printf("Enter the order of first matrix : n") ;
scanf("%d %d", &row1, &col1) ;
printf("nEnter the order of second matrix : n") ;
scanf("%d %d", &row2, &col2) ;
if(col1 == row2)
{
printf("nEnter the elements of first matrix : nn") ;
for(i = 0 ; i < row1 ; i++)
for(j = 0 ; j < col1 ; j++)
scanf("%d", &mata[i][j]) ;
printf("nEnter the elements of second matrix : nn") ;
for(i = 0 ; i < row2 ; i++)
for(j = 0 ; j < col2 ; j++)
scanf("%d", &matb[i][j]) ;
for(i = 0 ; i < row1 ; i++)
{
for(j = 0 ; j < col2 ; j++)
{
matc[i][j] = 0 ;
for(k = 0 ; k < col1 ; k++)
matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] ;
}
}
printf("nThe resultant matrix is : nn") ;
for(i = 0 ; i < row1 ; i++)
{
for(j = 0 ; j < col2 ; j++) {
printf("%d t", matc[i][j]) ;
}
printf("n") ;
}
}
else
printf("nMatrix Multiplication is not possible ...") ;
getch() ;
}
Program of Linear Search
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("Enter the number of element should be available in array: ");
scanf("%d",&n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("nEnter element to search: ");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
Program of Binary Search
#include<stdio.h>
int main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array: ");
scanf("%d",&n);
printf("nEnter array element(ascending order)n");
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
printf("nEnter the element to search: ");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("nElement found at position %d",mid+1);
else
printf("nElement not found");
return 0;
}
Program of Push and Pop operations in stack using the application of string
#include<stdio.h>
typedef struct stack
{
char data[100]; int top;
}stack;
int empty(stack *p)
{
return (p->top==-1);
}
int top(stack *p)
{
return p->data[p->top];
}
void push(stack *p,char x)
{
p->data[++(p->top)]= x;
}
void pop(stack *p)
{
if(!empty(p))
{
p->top = p->top - 1 ;
}
}
int main()
{
stack s; s.top = -1; char str[100];
printf("Enter the string = "); fgets(str,sizeof(str),stdin); int i, len = sizeof(str);
for(i=0;i<len;i++)
{
push(&s,str[i]);
}
printf("nReverse string is n");
while(!empty(&s))
{
printf("%c",top(&s));
pop(&s);
}
}
Program of Quick Sort
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("Enter the number of term you want to insert: ");
scanf("%d",&count);
printf("Enter %d elements: n", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Program for Infix to Postfix Evaluation
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char item)
{
if(top<SIZE-1)
{
top=top+1;
stack[top]=item;
}
else
{
printf("Stack Overflow");
return;
}
}
char pop()
{
char item;
if(top<0)
{
printf("Stack Underflow");
}
else
{
item=stack[top];
top=top-1;
return item;
}
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("nInvalid infix Expression.n");
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("nInvalid infix Expression.n");
}
postfix_exp[j] = '0';
}
int main()
{
char infix[SIZE], postfix[SIZE];
printf("nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix, postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
Program for Postfix Evaluation
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
void push(long int character);
long int postfix_evaluation();
int pop();
int isEmpty();
int top;
long int stack[50];
char postfix_expression[50];
int main()
{
long int evaluated_value;
top = -1;
printf("nEnter an Expression in Postfix format:t");
scanf("%[^n]s", postfix_expression);
printf("nExpression in Postfix Format: t%sn", postfix_expression);
evaluated_value = postfix_evaluation();
printf("nEvaluation of Postfix Expression: t%ldn", evaluated_value);
return 0;
}
long int postfix_evaluation()
{
long int x, y, temp, value;
int count;
for(count = 0; count < strlen(postfix_expression); count++)
{
if(postfix_expression[count] <= '9' && postfix_expression[count] >= '0')
{
push(postfix_expression[count] - '0');
}
else
{
x = pop();
y = pop();
switch(postfix_expression[count])
{
case '+': temp = y + x;
break;
case '-': temp = y - x;
break;
case '*': temp = y * x;
break;
case '/': temp = y / x;
break;
case '%': temp = y % x;
break;
case '^': temp = pow(y, x);
break;
default: printf("Invalid");
}
push(temp);
}
}
value = pop();
return value;
}
void push(long int character)
{
if(top > 50)
{
printf("Stack Overflown");
exit(1);
}
top = top + 1;
stack[top] = character;
}
int pop()
{
if(isEmpty())
{
printf("Stack is Emptyn");
exit(1);
}
return(stack[top--]);
}
int isEmpty()
{
if(top == -1)
{
return 1;
}
else
{
return 0;
}
}
Program for Tower of Hanoi
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
Program for Circular Queue
#include <stdio.h>
#define size 5
void insertq(int[], int);
void deleteq(int[]);
void display(int[]);
int front = - 1;
int rear = - 1;
int main()
{
int n, ch;
int queue[size];
do
{
printf("nn Circular Queue:n1. Insert n2. Deleten3. Displayn0. Exit");
printf("nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("nEnter number: ");
scanf("%d", &n);
insertq(queue, n);
break;
case 2:
deleteq(queue);
break;
case 3:
display(queue);
break;
}
}while (ch != 0);
}
void insertq(int queue[], int item)
{
if ((front == 0 && rear == size - 1) || (front == rear + 1))
{
printf("queue is full");
return;
}
else if (rear == - 1)
{
rear++;
front++;
}
else if (rear == size - 1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}
void display(int queue[])
{
int i;
printf("n");
if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%d ", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
}
void deleteq(int queue[])
{
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
printf("n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
else
{
printf("n %d deleted", queue[front]);
front++;
}
}
Program of Insertion and Deletion operation in Queue
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow n");
else
{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void del()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow n");
return ;
}
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
}
}
}

Pnno

  • 1.
    SUBMITTED TO SUBMITTEDBY MOHIT SIR PARNEET KAUR MCA 2nd YEAR ROLL NO: 201880032 DEPARTMENT OF COMPUTER SCIENCE THAPAR INSTITUTE OF ENGINEERING TECHNOLOGY PATIALA
  • 2.
    LIST OF PROGRAMS Programof Insertion sort Program of Selection Sort Program of Bubble Sort Program of Merging two arrays Program of Multiplication of 2d Program of Linear Search Program of Binary Search Program of Push and Pop operations in stack using the application of string Program of Quick Sort Program of Infix to Postfix Evaluation Program of Postfix Evaluation Program of Tower of Hanoi Program of Circular Queue Program of Insertion and Deletion operation in Queue
  • 3.
    Program of insertionsort #include<stdio.h> int main() { int i, k, count, temp, number[100]; printf("Enter the number of the terms you want to insert: "); scanf("%d",&count); printf("Enter %d elements: n", count); for(i=0;i<count;i++) scanf("%d",&number[i]); for(i=1;i<count;i++){ temp=number[i]; k=i-1; while((temp<number[k])&&(k>=0)){ number[k+1]=number[k]; k=k-1; } number[k+1]=temp; } printf("Number in ascending order"); for(i=0;i<count;i++) printf(" %d",number[i]); return 0; }
  • 4.
    Program of SelectionSort #include<stdio.h> int main(){ int i, k, count, temp, number[40]; printf("Enter the number of terms you want to insert: "); scanf("%d",&count); printf("Enter %d elements:n", count); for(i=0;i<count;i++) scanf("%d",&number[i]); for(i=0;i<count;i++){ for(k=i+1;k<count;k++){ if(number[i]>number[k]){ temp=number[i]; number[i]=number[k]; number[k]=temp; } } } printf("Sorted elements: "); for(i=0;i<count;i++) printf(" %d",number[i]); return 0; }
  • 5.
    Program of BubbleSort #include<stdio.h> int main(){ int count, temp, i, k, number[100]; printf("Enter the number of the terms you want to insert: "); scanf("%d",&count); printf("Enter %d numbers: n",count); for(i=0;i<count;i++) scanf("%d",&number[i]); for(i=count-2;i>=0;i--){ for(k=0;k<=i;k++){ if(number[k]>number[k+1]){ temp=number[k]; number[k]=number[k+1]; number[k+1]=temp; } } } printf("Sorted elements: "); for(i=0;i<count;i++) printf(" %d",number[i]); return 0; }
  • 6.
    Program of Mergingtwo arrays #include <stdio.h> int main() { int array1[50], array2[50], array3[100], m, n, i, j, k = 0; printf("n Enter size of array Array 1: "); scanf("%d", &m); printf("n Enter sorted elements of array 1: n"); for (i = 0; i < m; i++) { scanf("%d", &array1[i]); } printf("n Enter size of array 2: "); scanf("%d", &n); printf("n Enter sorted elements of array 2: n"); for (i = 0; i < n; i++) { scanf("%d", &array2[i]); } i = 0; j = 0; while (i < m && j < n) { if (array1[i] < array2[j]) { array3[k] = array1[i]; i++; } else {
  • 7.
    array3[k] = array2[j]; j++; } k++; } if(i >= m) { while (j < n) { array3[k] = array2[j]; j++; k++; } } if (j >= n) { while (i < m) { array3[k] = array1[i]; i++; k++; } } printf("n After merging: n"); for (i = 0; i < m + n; i++) { printf("n%d", array3[i]); } }
  • 8.
    Program of Multiplicationof 2d # include <stdio.h> # include <conio.h> int main() { int mata[10][10], matb[10][10], matc[10][10] ; int i, j, k, row1, col1, row2, col2 ; printf("Enter the order of first matrix : n") ; scanf("%d %d", &row1, &col1) ; printf("nEnter the order of second matrix : n") ; scanf("%d %d", &row2, &col2) ; if(col1 == row2) { printf("nEnter the elements of first matrix : nn") ; for(i = 0 ; i < row1 ; i++) for(j = 0 ; j < col1 ; j++) scanf("%d", &mata[i][j]) ; printf("nEnter the elements of second matrix : nn") ; for(i = 0 ; i < row2 ; i++) for(j = 0 ; j < col2 ; j++) scanf("%d", &matb[i][j]) ; for(i = 0 ; i < row1 ; i++) { for(j = 0 ; j < col2 ; j++) { matc[i][j] = 0 ; for(k = 0 ; k < col1 ; k++) matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] ; } } printf("nThe resultant matrix is : nn") ; for(i = 0 ; i < row1 ; i++) { for(j = 0 ; j < col2 ; j++) { printf("%d t", matc[i][j]) ; } printf("n") ; } } else printf("nMatrix Multiplication is not possible ...") ; getch() ; }
  • 9.
    Program of LinearSearch #include<stdio.h> int main() { int a[20],i,x,n; printf("Enter the number of element should be available in array: "); scanf("%d",&n); printf("Enter array elements:n"); for(i=0;i<n;++i) scanf("%d",&a[i]); printf("nEnter element to search: "); scanf("%d",&x); for(i=0;i<n;++i) if(a[i]==x) break; if(i<n) printf("Element found at index %d",i); else printf("Element not found"); return 0; }
  • 10.
    Program of BinarySearch #include<stdio.h> int main() { int arr[50],i,n,x,flag=0,first,last,mid; printf("Enter size of array: "); scanf("%d",&n); printf("nEnter array element(ascending order)n"); for(i=0;i<n;++i) scanf("%d",&arr[i]); printf("nEnter the element to search: "); scanf("%d",&x); first=0; last=n-1; while(first<=last) { mid=(first+last)/2; if(x==arr[mid]){ flag=1; break; } else if(x>arr[mid]) first=mid+1; else last=mid-1; } if(flag==1) printf("nElement found at position %d",mid+1); else printf("nElement not found"); return 0; }
  • 11.
    Program of Pushand Pop operations in stack using the application of string #include<stdio.h> typedef struct stack { char data[100]; int top; }stack; int empty(stack *p) { return (p->top==-1); } int top(stack *p) { return p->data[p->top]; } void push(stack *p,char x) { p->data[++(p->top)]= x; } void pop(stack *p) { if(!empty(p)) { p->top = p->top - 1 ; } }
  • 12.
    int main() { stack s;s.top = -1; char str[100]; printf("Enter the string = "); fgets(str,sizeof(str),stdin); int i, len = sizeof(str); for(i=0;i<len;i++) { push(&s,str[i]); } printf("nReverse string is n"); while(!empty(&s)) { printf("%c",top(&s)); pop(&s); } }
  • 13.
    Program of QuickSort #include<stdio.h> void quicksort(int number[25],int first,int last){ int i, j, pivot, temp; if(first<last){ pivot=first; i=first; j=last; while(i<j){ while(number[i]<=number[pivot]&&i<last) i++; while(number[j]>number[pivot]) j--; if(i<j){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } temp=number[pivot]; number[pivot]=number[j]; number[j]=temp; quicksort(number,first,j-1); quicksort(number,j+1,last); } } int main(){ int i, count, number[25]; printf("Enter the number of term you want to insert: "); scanf("%d",&count); printf("Enter %d elements: n", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1);
  • 14.
    printf("Order of Sortedelements: "); for(i=0;i<count;i++) printf(" %d",number[i]); return 0; }
  • 15.
    Program for Infixto Postfix Evaluation #include<stdio.h> #include<ctype.h> #include<string.h> #define SIZE 100 char stack[SIZE]; int top = -1; void push(char item) { if(top<SIZE-1) { top=top+1; stack[top]=item; } else { printf("Stack Overflow"); return; } } char pop() { char item; if(top<0) { printf("Stack Underflow"); } else { item=stack[top]; top=top-1; return item; } } int is_operator(char symbol) { if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') { return 1; }
  • 16.
    else { return 0; } } int precedence(charsymbol) { if(symbol == '^') { return(3); } else if(symbol == '*' || symbol == '/') { return(2); } else if(symbol == '+' || symbol == '-') { return(1); } else { return(0); } } void InfixToPostfix(char infix_exp[], char postfix_exp[]) { int i, j; char item; char x; push('('); strcat(infix_exp,")"); i=0; j=0; item=infix_exp[i]; while(item != '0') { if(item == '(') { push(item); }
  • 17.
    else if( isdigit(item)|| isalpha(item)) { postfix_exp[j] = item; j++; } else if(is_operator(item) == 1) { x=pop(); while(is_operator(x) == 1 && precedence(x)>= precedence(item)) { postfix_exp[j] = x; j++; x = pop(); } push(x); push(item); } else if(item == ')') { x = pop(); while(x != '(') { postfix_exp[j] = x; j++; x = pop(); } } else { printf("nInvalid infix Expression.n"); } i++; item = infix_exp[i]; } if(top>0) { printf("nInvalid infix Expression.n"); } postfix_exp[j] = '0'; }
  • 18.
    int main() { char infix[SIZE],postfix[SIZE]; printf("nEnter Infix expression : "); gets(infix); InfixToPostfix(infix, postfix); printf("Postfix Expression: "); puts(postfix); return 0; }
  • 19.
    Program for PostfixEvaluation #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> void push(long int character); long int postfix_evaluation(); int pop(); int isEmpty(); int top; long int stack[50]; char postfix_expression[50]; int main() { long int evaluated_value; top = -1; printf("nEnter an Expression in Postfix format:t"); scanf("%[^n]s", postfix_expression); printf("nExpression in Postfix Format: t%sn", postfix_expression); evaluated_value = postfix_evaluation(); printf("nEvaluation of Postfix Expression: t%ldn", evaluated_value); return 0; } long int postfix_evaluation() { long int x, y, temp, value; int count; for(count = 0; count < strlen(postfix_expression); count++) { if(postfix_expression[count] <= '9' && postfix_expression[count] >= '0') { push(postfix_expression[count] - '0'); } else { x = pop(); y = pop(); switch(postfix_expression[count]) { case '+': temp = y + x;
  • 20.
    break; case '-': temp= y - x; break; case '*': temp = y * x; break; case '/': temp = y / x; break; case '%': temp = y % x; break; case '^': temp = pow(y, x); break; default: printf("Invalid"); } push(temp); } } value = pop(); return value; } void push(long int character) { if(top > 50) { printf("Stack Overflown"); exit(1); } top = top + 1; stack[top] = character; } int pop() { if(isEmpty()) { printf("Stack is Emptyn"); exit(1); } return(stack[top--]); } int isEmpty() { if(top == -1)
  • 21.
  • 22.
    Program for Towerof Hanoi #include <stdio.h> void towers(int, char, char, char); int main() { int num; printf("Enter the number of disks : "); scanf("%d", &num); printf("The sequence of moves involved in the Tower of Hanoi are :n"); towers(num, 'A', 'C', 'B'); return 0; } void towers(int num, char frompeg, char topeg, char auxpeg) { if (num == 1) { printf("n Move disk 1 from peg %c to peg %c", frompeg, topeg); return; } towers(num - 1, frompeg, auxpeg, topeg); printf("n Move disk %d from peg %c to peg %c", num, frompeg, topeg); towers(num - 1, auxpeg, topeg, frompeg); }
  • 23.
    Program for CircularQueue #include <stdio.h> #define size 5 void insertq(int[], int); void deleteq(int[]); void display(int[]); int front = - 1; int rear = - 1; int main() { int n, ch; int queue[size]; do { printf("nn Circular Queue:n1. Insert n2. Deleten3. Displayn0. Exit"); printf("nEnter Choice 0-3? : "); scanf("%d", &ch); switch (ch) { case 1: printf("nEnter number: "); scanf("%d", &n); insertq(queue, n); break; case 2: deleteq(queue); break; case 3: display(queue); break; } }while (ch != 0); } void insertq(int queue[], int item) { if ((front == 0 && rear == size - 1) || (front == rear + 1)) { printf("queue is full"); return;
  • 24.
    } else if (rear== - 1) { rear++; front++; } else if (rear == size - 1 && front > 0) { rear = 0; } else { rear++; } queue[rear] = item; } void display(int queue[]) { int i; printf("n"); if (front > rear) { for (i = front; i < size; i++) { printf("%d ", queue[i]); } for (i = 0; i <= rear; i++) printf("%d ", queue[i]); } else { for (i = front; i <= rear; i++) printf("%d ", queue[i]); } } void deleteq(int queue[]) { if (front == - 1) { printf("Queue is empty "); } else if (front == rear)
  • 25.
    { printf("n %d deleted",queue[front]); front = - 1; rear = - 1; } else { printf("n %d deleted", queue[front]); front++; } }
  • 26.
    Program of Insertionand Deletion operation in Queue #include <stdio.h> #include<stdlib.h> #define MAX 50 int queue_array[MAX]; int rear = - 1; int front = - 1; void insert() { int add_item; if (rear == MAX - 1) printf("Queue Overflow n"); else { if (front == - 1) front = 0; printf("Inset the element in queue : "); scanf("%d", &add_item); rear = rear + 1; queue_array[rear] = add_item; } } void del() { if (front == - 1 || front > rear) { printf("Queue Underflow n"); return ; } else { printf("Element deleted from queue is : %dn", queue_array[front]); front = front + 1; } } void display() { int i; if (front == - 1) printf("Queue is empty n"); else {
  • 27.
    printf("Queue is :n"); for (i = front; i <= rear; i++) printf("%d ", queue_array[i]); printf("n"); } } int main() { int choice; while (1) { printf("1.Insert element to queue n"); printf("2.Delete element from queue n"); printf("3.Display all elements of queue n"); printf("4.Quit n"); printf("Enter your choice : "); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice n"); } } }