Web
Web
Web
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100];
int element,i,loc,size,n=0,j=0;
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("List before insertion: ");
for(i=0;i<size;i++)
{
printf("%d",a[i]);
}
printf("\nEnter an element to insert\n");
scanf("%d",&element);
printf("Enter a position to insert an element %d\n",element);
scanf("%d",&loc);
loc--;
for(i=size-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=element;
printf("\nList after Insertion: ");
for(i=0;i<size+1;i++)
{
printf("%d",a[i]);
}
printf("\nEnter an element to delete\n");
scanf("%d",&n);
i=0;
for(i=0;i<size;i++)
{
if(a[i]==n)
{
for(j=1;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("List after deletion\n");
for(i=0;i<(size-1);i++)
{
printf("%d",a[i]);
}
return 0;
OUTPUT:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n enter the size of stack[max=100]:");
scanf("%d", &n);
printf("\n \t stack operations using array");
printf("\n \t 1.push\n\t 2.pop\n\t 3.display\n\t 4.exit");
do
{
printf("\n enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t exit point");
break;
}
default:
{
printf("\n\t please enter a valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n enter the size of stack[max=100]:");
scanf("%d", &n);
printf("\n \t stack operations using array");
printf("\n \t 1.push\n\t 2.pop\n\t 3.display\n\t 4.exit");
do
{
printf("\n enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t exit point");
break;
}
default:
{
printf("\n\t please enter a valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\t stack is over flow");
}
else
{
printf("enter the value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t stack is under flow");
}
else
{
printf("\n\t the poped elements is %d", stack [top]);
top --;
}
}
void display()
{
if(top>=0)
{
printf("\n the elements in stack\n");
for(i=top;i>=0;i--)
printf("\n %d",stack[i]);
printf("\n press next choice");
}
else
{
printf("\n the stack is empty");
}
}if(top>=n-1)
{
printf("\n\t stack is over flow");
}
else
{
printf("enter the value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t stack is under flow");
}
else
{
printf("\n\t the poped elements is %d", stack [top]);
top --;
}
}
void display()
{
if(top>=0)
{
printf("\n the elements in stack\n");
for(i=top;i>=0;i--)
printf("\n %d",stack[i]);
printf("\n press next choice");
}
else
{
printf("\n the stack is empty");
}
}
OUTPUT:
exit point
3. Convert infix expression into postfix expression using Stack.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack
= (struct Stack*)malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array
= (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int infixToPostfix(char* exp)
{
int i, k;
struct Stack* stack = createStack(strlen(exp));
if (!stack)
return -1;
if (isOperand(exp[i]))
exp[++k] = exp[i];.
else if (exp[i] == '(')
push(stack, exp[i]);
else if (exp[i] == ')') {
while (!isEmpty(stack) && peek(stack) != '(')
exp[++k] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1
else
pop(stack);
}
else
{
while (!isEmpty(stack)
&& Prec(exp[i]) <= Prec(peek(stack)))
exp[++k] = pop(stack);
push(stack, exp[i]);
}
}
while (!isEmpty(stack))
exp[++k] = pop(stack);
exp[++k] = '\0';
printf("%s", exp);
}
int main()
{
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
OUTPUT:
abcd^e-fgh*+^*+i-
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top=-1;
void push(int item)
{
if(top>=MAX_SIZE-1)
{
printf("Stack Overflow\n");
return;
}
top++;
stack[top]=item;
}
int pop()
{
if(top<0)
{
printf("Stack Underflow\n");
return -1;
}
int item = stack[top];
top--;
return item;
}
int is_operator(char symbol)
{
if(symbol == '+' || symbol == '-' || symbol =='*' || symbol == '/')
{
return 1;
}
return 0;
}
int evaluate(char*expression)
{
int i = 0;
char symbol = expression[i];
int operand1, operand2, result;
while(symbol !='\0')
{
if(symbol>= '0' && symbol<= '9')
{
int num = symbol - '0';
push(num);
}
else if(is_operator(symbol))
{
operand2 = pop();
operand1 = pop();
switch(symbol)
{
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
}
push(result);
}
i++;
symbol = expression[i];
}
result = pop();
return result;
}
int main()
{
char expression[] = "5 6 7 + *8-";
int result = evaluate(expression);
printf("Result= %d\n", result);
return 0;
}
OUTPUT:
Result= 57
OUTPUT:
1.INSERTION
2.DELETION
3.DISPLAY
4.EXITenter the choice
1
enter the number to be inserted
22
no inserted is 22
enter the choice
2
deleted element is 22
enter the choice
3
content of queue
enter the choice
4
#include<stdio.h>
#include<stdlib.h>
struct node
{
int val;
struct node * next;
};
struct node *head;
void push();
void pop();
void display();
int main()
{
int choice=0;
printf("******* stack operation using linked list*****\n");
printf("--------------------------------------------\n");
while(choice !=4)
{
printf("\n\n choose one from the below options.....\n");
printf("\n1.push\n2.pop\n3.show\n4.exit");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exiting....");
break;
}
default:
{
printf("please enter a valid choice");
}
}
}
return 0;
}
void push()
{
int val;
struct node * ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("not able to push the elements");
}
else
{
printf("enter the value:");
scanf("%d",&val);
ptr->val=val;
ptr->next=head;
head=ptr;
printf("item, pushed\n");
}
}
void pop()
{
int item;
struct node *ptr;
if(head == NULL)
{
printf("underflow\n");
}
else
{
item=head-> val;
ptr=head;
head= head-> next;
free(ptr);
printf("item poped: %d\n",item);
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
printf("stack is empty\n");
}
else
{
printf("printing stack elements\n");
while(ptr !=NULL)
{
printf("%d\n",ptr->val);
ptr= ptr-> next;
}
}
}
OUTPUT:
******* stack operation using linked list*****
--------------------------------------------
1.push
2.pop
3.show
4.exit
enter your choice
1
enter the value:
11
item, pushed
1.push
2.pop
3.show
4.exit
enter your choice
2
item poped: 11
1.push
2.pop
3.show
4.exit
enter your choice
3
stack is empty
1.push
2.pop
3.show
4.exit
enter your choice
4
exiting....
7.Preorder, Inorder and Postorder Traversal of a Binary Tree
#include <stdio.h>
#include<malloc.h>
struct node{
struct node * left ;
char data;
struct node * right;
}; Preorder, Inorder and Postorder Traversal of a Binary
Tree
struct node * constructTree(int);
void inorder(struct node *);
char array[]={'A','B','C','D','E','F','G','\0','\0','H'};
int leftcount[]={1,3,5,-1,9,-1,-1,-1,-1,-1};
int rightcount[]={2,4,6,-1,-1,-1,-1,-1,-1,-1};
void main()
{
struct node * root;
root = constructTree(0);
printf("In-Order Traversal : \n");
inorder(root);
}
OUTPUT:
In-Order Traversal :
D B H E A F C G
8.Dijkstra’s Algorithm to find Shortest Path
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 9
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printSolution(int dist[])
{
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t\t\t %d\n", i, dist[i]);
}
void dijkstra2(int graph[V][V], int src)
{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v]
&& dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
int main()
{
int graph[V][V] = {
{ 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 }
};
dijkstra2(graph, 0);
return 0;
}
#include<stdio.h>
#define MAX 7
#define INF 1000
int selected[MAX];
int G[MAX][MAX] = {{INF,9,INF,INF,INF,12,INF},
{9,INF,5,INF,INF,INF,4},
{INF,5,INF,4,INF,INF,INF},
{INF,INF,4,INF,7,INF,6},
{INF,INF,INF,7,INF,8,8},
{12,INF,INF,INF,8,INF,INF},
{INF,4,INF,6,8,INF,INF}};
void main()
{
int r,c,nE,min,sum=0,n=MAX;
printf("Minimum Cost Spanning Tree:\n");
selected[0]=1;
nE=1;
while(nE<n)
{
for(int i=0;i<n;i++)
if(selected[i]==1)
for(int j=0;j<n;j++)
if (selected[j]==0)
if (min > G[i][j])
{
min = G[i][j];
r=i;c=j;
}
selected[c]=1;
nE++;
sum = sum + G[r][c];
printf("#include<stdio.h>
#define MAX 7
#define INF 1000
int selected[MAX];
int G[MAX][MAX] = {{INF,9,INF,INF,INF,12,INF},
{9,INF,5,INF,INF,INF,4},
{INF,5,INF,4,INF,INF,INF},
{INF,INF,4,INF,7,INF,6},
{INF,INF,INF,7,INF,8,8},
{12,INF,INF,INF,8,INF,INF},
{INF,4,INF,6,8,INF,INF}};
void main()
{
int r,c,nE,min,sum=0,n=MAX;
printf("Minimum Cost Spanning Tree:\n");
selected[0]=1;
nE=1;
while(nE<n)
{
for(int i=0;i<n;i++)
if(selected[i]==1)
for(int j=0;j<n;j++)
if (selected[j]==0)
if (min > G[i][j])
{
min = G[i][j];
r=i;c=j;
}
selected[c]=1;
nE++;
sum = sum + G[r][c];
printf("\nedge:(%d, %d) cost:%d",r,c,G[r][c]);
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10],i,n,j,sn;
printf("enter how many numbers\n");
scanf("%d",&n);
printf("\n enter the number one by one:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element of search:\n");
scanf("%d:",&sn);
for(j=0;j<n;j++)
if(a[j]==sn)
{
printf("the element found at %d is %d",j,a[j]);
exit(0);
}
printf("\n search unsuccessfull");
}
ii) Binary Search
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10],mid,n,beg,last,i,se;
printf("]n enter how many numbers as\n");
scanf("%d",&n);
printf("enter the number one by one:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
beg=0;
last=n;
printf("\n enter element to search\n");
scanf("%d",&se);
while(beg<=last)
{
mid=(beg+last/2);
if(a[mid]==se)
{
printf("element found of the position %d",mid+1);
exit(0);
}
if(a[mid]<se)
beg=mid+1;
if(a[mid]>se)
last=mid-1;
}
if(beg!=last)
printf("element not found");
}
#include<stdio.h>
int main()
{
int n,i,j,a[10],t;
printf("enter the number");
scanf("%d",&n);
printf("enter the number one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<(n-1)-i;j++)
{
if(a[j]>=a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("sorting list");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
Ii)Insertion Sort
#include<stdio.h>
int main()
{
int i,j,s,temp,a[20];
printf(" enter the total elements:");
scanf("%d",&s);
printf(" enter %d elements:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while(temp<a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting:\n");
for(i=0;i<s;i++)
printf("%d\n",a[i]);
return 0;
}
12)i)Selection Sort
#include<stdio.h>
int main()
{
int n,i,j,a[10],small,t;
printf("enter the number");
scanf("%d",&n);
printf("enter the number one by one:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
small=i;
for(j=i+1;j<n;j++)
{
if(a[j]<=a[small])
{
small=j;
}
}
t=a[i];
a[i]=a[small];
a[small]=t;
}
printf("sorted list");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Ii)Quick Sort
#include<stdio.h>
void qsort(int arr[20],int fst,int last)
{
int i,j,pivot,tmp;
if(fst<last)
{
pivot=fst;
i=fst;
j=last;
while(i<j)
{
while(arr[i]<=arr[pivot]&&i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
tmp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=tmp;
qsort(arr,fst,j-i);
qsort(arr,j+1,last);
}
}
int main()
{
int arr[30],i,size;
printf("Enter the no:of the elements:");
scanf("%d",&size);
printf(" Enter %d element :\n",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
qsort(arr,0,size-1);
printf("quick sorted elements are an:\n");
for(i=0;i<size;i++)
printf("%d\n",arr[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}