University Experiment #1
AIM- WAP to perform linear search & binary search on array.
#include<stdio.h>
#include<conio.h>
int linear_search(int [], int, int);
int binary_search(int [],int,int,int);
void main()
{
int a[20],search,i,n,pos,x;
int item,f=0,n,l,b;
clrscr();
printf(“search\n”);
printf(“1.linear search.\n”);
printf(“2.binary search”);
printf(“enter type of search u want to perform”);
scanf(“%d”,&x);
switch(x)
{case 1:
printf("Enter the size of the array(max. 20):");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the number to search:");
scanf("%d",&search);
pos = linear_search(a,n,search);
if(pos==-1)
printf("\n The element %d is not present in array.", search);
else
printf("\n The element %d is present at location %d.", search, pos+1);
break;
case 2:
printf("\n ENTER THE ELEMENT FOR SEARCH:");
scanf("%d",&item);
l=n-1;
b=binary_search(a,f,l,item);
if(b==-1)
printf("\n ELEMENT IS NOT FOUND.");
else
printf("\n ELEMENT %d IS FOUND AT LOCATION =%d",item,b+1);
break;
default: printf(“u enter a wrong choice”);
}
getch();
}
int linear_search(int arr[], int n, int find)
{
int i;
for (i=0;i<n;i++)
{
if (arr[i]==find)
return i;
}
return -1;
}
Output Experiment #1
University Experiment #2
AIM- WAP to create a stack and Perform Push, Pop and Traverse operations on the
stack using Linear Linked List.
#include<stdio.h>
#include<conio.h>
#include<process.h>
typedef struct node
{ int data;
struct node *link;
}NODE;
void Push(int);
int pop();
void Display();
NODE *top=NULL;
void main()
{ int opn,elem;
clrscr();
printf("\n ### Linked List Implementation of STACK Operations ### \n\n");
printf("\n Press 1-Push, 2-Pop, 3-Display,4-Exit");
do { printf("\n Your option- ");
scanf("%d",&opn);
switch(opn)
{ case 1: printf("\n\nRead the Element to be pushed-");
scanf("%d",&elem);
Push(elem);
break;
case 2: elem=Pop();
if(elem != -1)
printf(" Deleted Node(From Top)with the Data: %d\n",elem);
break;
case 3: printf("Linked List Implementation of Stack: Status:\n");
Display();
break;
case 4: exit(0);
default: printf("\nInvalid Option !!! Try Again !! ");
break;
}
}while(opn != 4);
getch();
}
Output Experiment #2
University Experiment #3
AIM- WAP to create a Linear Queue using Linked List and Implement different
operations such as Insert, Delete and Display the queue elements.
#include <stdlib.h>
#include<conio.h>
typedef struct node
{ int data;
struct node *link;
} NODE;
NODE *front, *rear;
void Insert(int);
int Delete();
void Display();
void main()
{ int opn, elem;
clrscr();
printf("\n ### Linked List Implementation of QUEUE Operations ### ");
printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit");
front = rear = NULL;
do{ printf("\n Your option- ");
scanf("%d", &opn);
switch (opn)
{ case 1: printf("\nRead the Employee no. to be Inserted-");
scanf("%d", &elem);
Insert(elem);
break;
case 2: elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %d\n", elem);
break;
case 3: printf("Linked List Implementation of Queue: Status:\n");
Display();
break;
case 4: exit(0);
break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while (opn != 4);
getch(); }
Output Experiment #3
University Experiment #4
AIM- WAP to Create a Binary Tree and perform Tree traversals (Preorder, Postorder,
Inorder) using the concept of Recursion.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct bint
{
int data,flag;
struct bint *left,*right;
}node;
Output Experiment #4
University Experiment #5
AIM- WAP to Implement insertion, deletion and display (Preorder, Postorder, Inorder)
on binary search tree with the information in the tree about the details of an automobile
(type, company, year of make).
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct btnode
{
char value1[30];
char value2[30];
int value3;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
clrscr();
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
scanf("%s",&type);
printf("company :");
scanf("%s",&company);
printf("year :");
scanf("%d", &year);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
strcpy(temp->value1,type);
strcpy(temp->value2,company);
temp->value3=year;
temp->l = temp->r = NULL;
}
void search(struct btnode *t)
{
if ((temp->value3 > t->value3) && (t->r != NULL)) /* value more than root node value
insert at right */
search(t->r);
else if ((temp->value3 > t->value3) && (t->r == NULL))
t->r = temp;
else if ((temp->value3 < t->value3) && (t->l != NULL)) /* value less than root node
value insert at left */
search(t->l);
else if ((temp->value3 < t->value3) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->r != NULL)
inorder(t->r);
}
void delete()
{
int year;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the year of data to be deleted : ");
scanf("%d", &year);
t1 = root;
t2 = root;
search1(root, year);
}
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
}
void search1(struct btnode *t, int year)
{
if ((year>t->value3))
{
t1 = t;
search1(t->r, year);
}
else if ((year < t->value3))
{
t1 = t;
search1(t->l, year);
}
else if ((year==t->value3))
{
delete1(t);
}
}
void delete1(struct btnode *t)
{
int k;
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t = NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value3 = k;
}
}
int smallest(struct btnode *t)
{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value3);
}
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value3);
}
Output Experiment #5
University Experiment #6
AIM: WAP to create a stack and Perform Push, Pop and Traverse operations on the
stack using Linear Linked List.
#include<stdio.h>
#include<conio.h>
#include<process.h>
typedef struct node
{ int data;
struct node *link;
}NODE;
void Push(int);
int pop();
void Display();
NODE *top=NULL;
void main()
{ int opn,elem;
clrscr();
printf("\n ### Linked List Implementation of STACK Operations ### \n\n");
printf("\n Press 1-Push, 2-Pop, 3-Display,4-Exit");
do { printf("\n Your option- ");
scanf("%d",&opn);
switch(opn)
{ case 1: printf("\n\nRead the Element to be pushed-");
scanf("%d",&elem);
Push(elem);
break;
case 2: elem=Pop();
if(elem != -1)
printf(" Deleted Node(From Top)with the Data: %d\n",elem);
break;
case 3: printf("Linked List Implementation of Stack: Status:\n");
Display();
break;
case 4: exit(0);
default: printf("\nInvalid Option !!! Try Again !! ");
break;
}
}while(opn != 4);
getch();
}
Output Experiment #6
University Experiment #7
AIM- WAP to create a Linear Queue using Linked List and Implement different
operations such as Insert, Delete and Display the queue elements.
#include <stdlib.h>
#include<conio.h>
typedef struct node
{ int data;
struct node *link;
} NODE;
NODE *front, *rear;
void Insert(int);
int Delete();
void Display();
void main()
{ int opn, elem;
clrscr();
printf("\n ### Linked List Implementation of QUEUE Operations ### ");
printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit");
front = rear = NULL;
do{ printf("\n Your option- ");
scanf("%d", &opn);
switch (opn)
{ case 1: printf("\nRead the Employee no. to be Inserted-");
scanf("%d", &elem);
Insert(elem);
break;
case 2: elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %d\n", elem);
break;
case 3: printf("Linked List Implementation of Queue: Status:\n");
Display();
break;
case 4: exit(0);
break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while (opn != 4);
getch();
}
void Insert(int info)
{ NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else
{ temp->data = info;
temp->link = NULL;
if (front == NULL) {
front = rear = temp; }
else
{ rear->link = temp;
rear = temp; }
printf(" Node has been inserted at End Successfully !!"); } }
int Delete() {
int info;
NODE *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1; }
Else
{ t = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info); } }
void Display() {
NODE *t;
if (front == NULL)
printf("Empty Queue\n");
else
{ t = front;
printf("Front->");
while (t) {
printf("[%d]->", t->data);
t = t->link; }
printf("Rear\n");
}
}
Output Experiment #7
University Experiment #8
AIM- WAP to Create a Binary Tree and perform Tree traversals (Preorder, Postorder,
Inorder) using the concept of Recursion.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct bint
{
int data,flag;
struct bint *left,*right;
}node;
Output Experiment #8
University Experiment #9
AIM- WAP to Implement insertion, deletion and display (Preorder, Postorder, Inorder)
on binary search tree with the information in the tree about the details of a automobile
(type, company, year of make).
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct btnode
{
char value1[30];
char value2[30];
int value3;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
clrscr();
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
scanf("%s",&type);
printf("company :");
scanf("%s",&company);
printf("year :");
scanf("%d", &year);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
strcpy(temp->value1,type);
strcpy(temp->value2,company);
temp->value3=year;
temp->l = temp->r = NULL;
}
void search(struct btnode *t)
{
if ((temp->value3 > t->value3) && (t->r != NULL)) /* value more than root node value
insert at right */
search(t->r);
else if ((temp->value3 > t->value3) && (t->r == NULL))
t->r = temp;
else if ((temp->value3 < t->value3) && (t->l != NULL)) /* value less than root node
value insert at left */
search(t->l);
else if ((temp->value3 < t->value3) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->r != NULL)
inorder(t->r);
}
void delete()
{
int year;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the year of data to be deleted : ");
scanf("%d", &year);
t1 = root;
t2 = root;
search1(root, year);
}
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
puts(t->value1);
puts(t->value2);
printf("%d -> ", t->value3);
}
void search1(struct btnode *t, int year)
{
if ((year>t->value3))
{
t1 = t;
search1(t->r, year);
}
else if ((year < t->value3))
{
t1 = t;
search1(t->l, year);
}
else if ((year==t->value3))
{
delete1(t);
}
}
void delete1(struct btnode *t)
{
int k;
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t = NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value3 = k;
}
}
int smallest(struct btnode *t)
{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value3);
}
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value3);
}
Output Experiment #9
Insertion sort
#include<stdio.h>
#include<conio.h>
void InsertionSort(int *array , int number_of_elements)
{ int iter,jter;
for(iter=1;iter<number_of_elements;iter++)
{ int current_element = array[iter];
jter = iter-1;
while(jter>=0 && array[jter] > current_element)
{ array[jter+1] = array[jter];
jter--;
}
array[jter+1] = current_element;
}
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to add") ;
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
{ scanf("%d",&array[iter]);
}
InsertionSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
getch();
}
Merge Sort
#include<stdio.h>
#include<conio.h>
void Merge(int * , int , int , int );
void MergeSort(int *array, int left, int right)
{ int mid = (left+right)/2;
if(left<right)
{ MergeSort(array,left,mid);
MergeSort(array,mid+1,right);
Merge(array,left,mid,right);
}
}
void Merge(int *array, int left, int mid, int right)
{ int tempArray[60];
int pos=0,lpos = left,rpos = mid + 1;
int iter;
while(lpos <= mid && rpos <= right)
{ if(array[lpos] < array[rpos])
tempArray[pos++] = array[lpos++];
else
tempArray[pos++] = array[rpos++];
}
while(lpos <= mid) tempArray[pos++] = array[lpos++];
while(rpos <= right)tempArray[pos++] = array[rpos++];
for(iter = 0;iter < pos; iter++)
array[iter+left] = tempArray[iter];
return;
}
int main()
{ int number_of_elements;
int iter;
int array[30];
clrscr();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
MergeSort(array,0,number_of_elements-1);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0; }
Quick Sort
#include<stdio.h>
#include<conio.h>
void QuickSort(int *array, int from, int to)
{ int i = from, j, pivot,temp;
if(from>=to)return;
pivot = array[from];
for(j = from + 1;j <= to;j++)
{ if(array[j] < pivot)
{ i = i + 1;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
temp = array[i];
array[i] = array[from];
array[from] = temp;
QuickSort(array,from,i-1);
QuickSort(array,i+1,to);
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to sort");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
QuickSort(array,0,number_of_elements-1);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
}
Bubble sort
#include<stdio.h>
#include<conio.h>
void BubbleSort(int *array,int number_of_elements)
Bucket sort
#include<stdio.h>
#include<conio.h>
void Bucket_Sort(int array[], int n)
{ int i, j;
int count[30];
for(i=0; i < n; i++)
count[i] = 0;
for(i=0; i < n; i++)
(count[array[i]])++;
}
int main()
{ int array[100];
int num;
int i;
clrscr();
printf("Enter How many Numbers : ");
scanf("%d",&num);
printf("Enter the %d elements to be sorted:\n",num);
for(i = 0; i < num; i++ )
scanf("%d",&array[i]);
printf("\nThe array of elements before sorting : \n");
for (i = 0;i < num;i++)
printf("%d ", array[i]);
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, num);
for (i = 0;i < num;i++)
printf("%d ", array[i]);
printf("\n");
return 0;
getch();
}
Radix sort
#include <stdio.h>
#include<conio.h>
#define MAX 20
#define SHOWPASS
#define BASE 10
void print(int *a, int n)
{ int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radixsort(int *a, int n)
{ int i, b[MAX], m = a[0], exp = 1;
for (i = 1; i < n; i++)
{ if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{ int bucket[BASE] = { 0 };
Shell sort
#include<stdio.h>
#include<conio.h>
void ShellSort(int *array, int number_of_elements)
{ int iter,i,j,jter, increment, temp;
for(increment = number_of_elements/2;increment > 0; increment /= 2)
{ for(i = increment; i<number_of_elements; i++)
{ temp = array[i];
for(j = i; j >= increment ;j-=increment)
{ if(temp < array[j-increment])
{ array[j] = array[j-increment];
}
else
break;
}
array[j] = temp;
}
}
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
ShellSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
getch();
}
Selection sort
#include<stdio.h>
#include<conio.h>
void SelectionSort(int *array,int number_of_elements)
{ int iter,jter,minIndex,temp;
for(iter = 0;iter<number_of_elements;iter++)
{ minIndex = iter;
for(jter = iter+1; jter<number_of_elements;jter++)
{ if(array[jter] < array[minIndex])
minIndex = jter;
}
temp = array[iter];
array[iter] = array[minIndex];
array[minIndex] = temp;
}
}
int main()
{ int number_of_elements;
int array[30];
int iter;
clrscr();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
scanf("%d",&array[iter]);
SelectionSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
printf("%d ",array[iter]);
printf("\n");
return 0;
}
Heap sort
#include <stdio.h>
#include<conio.h>
void main()
{ int heap[10], no, i, j, c, root, temp;
clrscr();
printf("\n Enter no of elements :");
scanf("%d", &no);
printf("\n Enter the nos : ");
for (i = 0; i < no; i++)
scanf("%d", &heap[i]);
for (i = 1; i < no; i++)
{ c = i;
do
{ root = (c - 1) / 2;
if (heap[root] < heap[c])
{ temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while (c != 0);
}
printf("Heap array : ");
Exchange sort
#include<stdio.h>
#include <conio.h>
int main(void)
{ clrscr();
int array[5];
int length = 5;
int i, j;
int temp;
printf("Enter a four numbers: ");
for (i = 0; i <5; i++)
scanf(“%d”, array[i]);
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Printf("exchange sort\n");
for (i = 0; i < 5; i++)
{
Printf(“%d\n”,array[i] );
}
getch();
}
Merge Sort
Quick Sort
Bubble sort
Bucket sort
Radix sort
Shell sort
Selection sort
Heap sort
Exchange sort
Practice Experiment #1
AIM: WAP to insert an element at user defined position in an array of type float.
(Unsorted)
#include<stdio.h>
#include<conio.h>
void main()
{
float a[20];
int n,i,j,k,m;
clrscr();
printf("\n Enter the array length(max. 20):");
scanf("%d",&n);
printf("\n Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("\n Enter the position: ");
scanf("%d", &k);
printf("\n Enter the element you want to insert..");
scanf("%d",&m);
n=n+1;
for(j=n;j>=k;j--)
{
a[j]=a[j-1];
}
a[j]=m;
printf(" \n The new list is :");
for(i=0;i<n;i++)
{
printf( "\n %.2f ",a[i]);
}
getch();
}
Output Experiment #1
Practice Experiment #2
AIM: WAP to insert an element in an array of type float. (Sorted)
#include<stdio.h>
#include<conio.h>
void main( )
{
float a[20];
int n,item,i;
clrscr();
printf("Enter the size of the array(max. 20):");
scanf("%d",&n);
printf("Enter elements of the array in the sorted order:");
for(i=0; i<n; i++)
{
scanf("%f", &a[i]);
}
printf("\nEnter ITEM to be inserted : ");
scanf("%d", &item);
i=n-1;
while(item<a[i] && i>=0)
{
a[i+1]=a[i];
i--;
}
a[i+1] = item;
n=n+1;
printf("\n\nAfter inserting element in array:\n");
for(i=0;i<n;i++)
{
printf("\n%.2f", a[i]);
}
getch();
}
Output Experiment #2
Practice Experiment #3
AIM: WAP to delete an element from user defined position in an array of type float.
#include<stdio.h>
#include<conio.h>
void main()
{
float a[20];
int n,i,k,m;
clrscr();
printf("\n Enter the array length(max. 20):");
scanf("%d",&n);
printf("\n Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("\n Enter the element you want to delete..");
scanf("%d",&m);
for(i=0;i<n;i++)
if(a[i]==m)
{
k=i;
break;
}
for(i=k;i<n;i++)
{
a[i]=a[i+1];
}
a[n-1]=0;
printf(" \n The new list is :");
for(i=0;i<n;i++)
{
printf( "\n %.2f ",a[i]);
}
getch();
}
Output Experiment #3
Practice Experiment #4
AIM: WAP to add two polynomials using array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n");
printf("\t= = = = = = = = = = = = = = = = = = =\n");
printf("\n\tEnter the no. of terms of 1st polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees and coefficients:");
for (i=0;i<2*m;i++)
scanf("%d", &a[i]);
printf("\n\tFirst polynomial is:");
k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;
while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}
i=0;
j=0;
k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
}
k+=2;
}
while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;
}
while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
getch();
}
Output Experiment #4
Practice Experiment #5
AIM: WAP to create a Matrix. Perform Addition, Transpose and Multiplication using
Switch-Case statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,k,j,c1,c2,r1,r2;
int m1[3][3],m2[3][3],m3[3][3];
clrscr();
while(1)
{
printf("\n 1. Transpose of Matrix:-\n");
printf("\n 2. Addition of Matrix:-\n");
printf("\n 3. Multiplication of Matrix:-\n");
printf("\n 4. Exit\n");
printf("\n Enter your choice:-");
scanf("%d",&a);
switch(a)
{
case 1 : printf("\n Enter the element of array :-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%d",&m1[i][j]);
m2[j][i]=m1[i][j];
}
printf("\n Transpose of Matrix is:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d",m2[i][j]);
printf("\n");
}
break;
case 2: printf("\n Input Matrix one:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
printf("\n Input Matrix two:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m3[i][j]=m1[i][j]+ m2[i][j];
printf("\n The sum is:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d",m3[i][j]);
printf("\n");
}
break;
case 3: printf("\n Input value of Matrix one:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
printf("\n Input value of Matrix two:-");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
m3[i][j]=0;
for(k=0;k<3;k++)
m3[i][j]=m3[i][j]+(m1[i][k]*m2[k][j]);
}
printf("\n Multiplication of Matrix:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d",m3[i][j]);
printf("\n");
}
break;
case 4: exit(0);
break;
}
}
getch();
}
Output Experiment #5
Practice Experiment #6
AIM: WAP to perform addition of two Polynomials using Circular Linked List.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<math.h>
typedef struct node
{ int expo,coef;
struct node *next;
}node;
node * insert(node *,int,int);
node * create();
node * add(node *p1,node *p2);
void display(node *head);
node *insert(node*head,int expo1,int coef1)
{ node *p,*q;
p=(node *)malloc(sizeof(node));
p->expo=expo1;
p->coef=coef1;
p->next=NULL;
if(head==NULL)
{ head=p;
head->next=head;
return(head);
}
if(expo1>head->expo)
{ p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(expo1==head->expo)
{
head->coef=head->coef+coef1;
return(head);
}
q=head;
while(q->next!=head&&expo1>=q->next->expo)
q=q->next;
if(p->expo==q->expo)
q->coef=q->coef+coef1;
else
{ p->next=q->next;
q->next=p;
}
return(head);
}
node *create()
{ int n,i,expo1,coef1;
node *head=NULL;
printf("\nEnter no of terms of polynomial==>");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\nEnter coef & expo==>");
scanf("%d%d",&coef1,&expo1);
head=insert(head,expo1,coef1);
}
return(head);
}
node *add(node *p1,node *p2)
{ node *p;
node *head=NULL;
printf("\nAddition of polynomial==>");
p=p1->next;
do { head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p1->next);
p=p2->next;
do { head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p2->next);
return(head);
}
void display(node *head)
{ node *p,*q;
int n=0;
q=head->next;
p=head->next;
do { n++;
q=q->next;
}while(q!=head->next);
printf("\n\tThe polynomial is==>");
do { if(n-1)
{ printf("%dx^(%d) + ",p->coef,p->expo);
p=p->next;
}
else
{ printf(" %dx^(%d)",p->coef,p->expo);
p=p->next; }
n--;
} while(p!=head->next);
}
void main()
{ node *p1,*p2,*p3;
p1=p2=p3=NULL;
clrscr();
p1=create();
display(p1);
p2=create();
display(p2);
p3=add(p1,p2);
display(p3);
getch();
}
Output Experiment #6
Practice Experiment #7
AIM: WAP to implement push and pop operations in a stack using an array. The array
should be storing the roll numbers of the students in the integer form. Separate
functions for display, push and pop should be designed with appropriate arguments.
The pop function should return the element which is poped out.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int item;
int s[10];
int top;
void push()
{ if(top==size-1)
{ printf("Stack is full\n");
return;
}
printf("\nEnter item:");
scanf("%d",&item);
s[++top]=item;
}
void pop()
{ if(top==-1)
{ printf("\nstack is empty");
return;
}
printf("\nDeleted item is: %d",s[top]);
top--;
}
void display()
{ int i;
if(top==-1)
{ printf("\nstack is empty");
return;
}
printf("\nContent of stack is:");
for(i=0;i<=top;i++)
printf("%d\t",s[i]);
}
void main()
{ int ch;
top=-1;
clrscr();
for(;;)
{ printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{case 1:push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong entry ! try again");
break;
}
}
getch();
}
Output Experiment #7
Practice Experiment #8
AIM: WAP to convert Infix Expression to Postfix Form using stack.
#define SIZE 50
#include<conio.h>
#include <ctype.h>
char s[SIZE];
int top=-1;
void push(char elem)
{ s[++top]=elem;
}
char pop()
{ return(s[top--]);
}
int pr(char elem)
{ switch(elem)
{ case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void main()
{ char infx[50],pofx[50],ch,elem;
int i=0,k=0;
clrscr();
printf("\n\nRead the Infix Expression - ");
scanf("%s",infx);
push('#');
while( (ch=infx[i++]) != '\0')
{ if( ch == '(')
push(ch);
else if(isalnum(ch))
pofx[k++]=ch;
else if( ch == ')')
{ while( s[top] != '(')
pofx[k++]=pop();
elem=pop();
}
else
{ while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
pofx[k++]=pop();
pofx[k]='\0';
printf("\n\nGiven Infix Expn: %s and its Postfix Expn: %s\n",infx,pofx);
getch();
}
Output Experiment #8
Practice Experiment #9
AIM: WAP to convert Infix Expression to Prefix Form using stack.
#define SIZE 50
#include<conio.h>
#include<string.h>
#include <ctype.h>
char s[SIZE];
int top=-1;
void push(char elem)
{ s[++top]=elem;
}
char pop()
{ return(s[top--]);
}
int pr(char elem)
{ switch(elem)
{ case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void main()
{ char infx[50],prfx[50],ch,elem;
int i=0,k=0;
clrscr();
printf("\n\nRead the Infix Expression - ");
scanf("%s",infx);
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{ if( ch == ')') push(ch);
else if(isalnum(ch)) prfx[k++]=ch;
else if( ch == '(')
{ while( s[top] != ')')
prfx[k++]=pop();
elem=pop();
}
else
{ while( pr(s[top]) >= pr(ch) )
prfx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
prfx[k++]=pop();
prfx[k]='\0';
strrev(prfx);
strrev(infx);
printf("\n\nGiven Infix Expn: %s and Prefix Expn: %s\n",infx,prfx);
getch();
}
Output Experiment #9
#include<stdio.h>
#include<conio.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();
void main()
{ int option;
clrscr();
printf(">>> c program to implement queue operations <<<");
printf("\n\n 1.Insert an element");
printf("\n 2.Delete an element");
printf("\n 3.Display queue");
printf("\n 4.Exit");
do { printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{ case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: exit(0);
default:printf("Wrong choice");
break;
}
}while(option!=4);
getch();
}
void insert_element()
{ int num;
printf("\n Enter the number to be inserted: ");
scanf("%d",&num);
#include<stdio.h>
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct que
{ int info;
char name[30];
struct que *next; };
void inqueue(struct que**,struct que**,int,char []);
void delqueue(struct que**,struct que**);
void display(struct que*,struct que*);
void main()
{
struct que *front, *rear;
int choice, item;
char labname[30];
clrscr();
front = NULL;
rear = NULL;
printf("\n\n\n\t MAIN MENU FOR CIRCULAR QUEUE OPERATIONS");
printf("\n\t 1. Insert in Queue ");
printf("\n\t 2. Delete from Queue ");
printf("\n\t 3. Display the Queue ");
printf("\n\t 4. Exit ");
do
{
printf("\n\t Enter choice : ");
scanf(" %d",&choice);
switch(choice)
{
case 1: printf("\n Enter lab name and no. of computers: ");
scanf("%s %d",&labname,&item);
inqueue(&front,&rear,item,labname);
display(front,rear);
break;
case 2: delqueue(&front,&rear);
display(front,rear);
break;
case 3: display(front,rear);
break;
default:printf("\n End of program ");
}
}while(choice !=4);
getch();
}
}
}
void display(struct que *front,struct que *rear)
{
printf ("\n Now queue is : ");
if (front==NULL)
printf(" Queue is empty, nothing to display");
else
{
if(front==rear)
printf(" ->%s %d",front->name,front->info);
else
{
while(front != rear)
{
printf(" ->%s %d",front->name,front->info);
front=front->next;
}
printf(" ->%s %d",rear->name,rear->info);
}
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
}
pt = s[top--];
while(pt != NULL)
{
printf("%d\t",pt->data);
if(pt->right != NULL)
{
pt = pt->right;
while(pt != NULL)
{
s[++top] = pt;
pt = pt->left;
}
}
pt = s[top--];
}
}
head = create(head,d);
printf("\n Do you want to continue(y/n)");
fflush(stdin);
ch = getchar();
}
printf("\ninorder non recursive\n");
non_in(head);
printf("\npostorder non recursive\n");
non_post(head);
printf("\npreorder non recursive\n");
non_pre(head);
getch();
}
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *left, *right;
};
typedef struct btnode node;
void insert(node *, node *);
void bfs_traverse(node *);
node *root = NULL;
int val, front = 0, rear = -1, i;
int queue[20];
void main()
{
node *new = NULL ;
int num = 1;
printf("Enter the elements of the tree(enter 0 to exit)\n");
while (1)
{
scanf("%d", &num);
if (num == 0)
break;
new = malloc(sizeof(node));
new->left = new->right = NULL;
new->value = num;
if (root == NULL)
root = new;
else
{
insert(new, root);
}
}
printf("elements in a tree in inorder are\n");
queue[++rear] = root->value;
bfs_traverse(root);
#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
struct node *left;
struct node *right;
};
void generate(struct node **, int);
void DFS(struct node *);
void delete(struct node **);
int main()
{
struct node *head = NULL;
int choice = 0, num, flag = 0, key;
do
{ printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice:
");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter element to insert: ");
scanf("%d", &num);
generate(&head, num);
break;
case 2: DFS(head);
break;
case 3: delete(&head);
printf("Memory Cleared\nPROGRAM TERMINATED\n");
break;
default: printf("Not a valid input, try again\n");
}
} while (choice != 3);
return 0;
}
void generate(struct node **head, int num)
{
#include "stdio.h"
#include "conio.h"
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[])
{ int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{ min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w]; } }
void main()
{ int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{ scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity; }
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch(); }
#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void main()
{
int p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of input data: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
warshal(p,n);
printf("\n Transitive closure: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}