Fundamentals of C Lab Record New MANUAL2
Fundamentals of C Lab Record New MANUAL2
BIOMEDICAL ENGINEERING
LAB MANUAL
Aim:
To write a C program using basic while loop.
#include <stdio.h>
int main()
{
int i=3;
while(i<10)
{
printf("%d\n",i);
i++;
}
}
Output:
3456789
Result:
Thus the C program for basic while loop has been verified and executed successfully.
Exp 2 BASIC C PROGRAMS – LOOPING(DO WHILE LOOP)
Aim:
To write a C program using basic do while loop.
Output:
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
Result:
Thus the C program for basic do while loop has been verified and executed successfully.
Exp 3
SUM OF DIGITS OF A NUMBER
Aim:
To write a C program for sum of the digits of a number.
Program :
#include<stdio.h>
int main()
{
int num,n;
int sum=0;
printf("\nEnter an integer value:");
scanf("%d",&num);
n=num;
while(n!=0)
{
sum=sum+n%10;n=n/10;
}
printf("\nThe sum of digitsof%dis%d",num,sum);
}
Output:
Enter an integer value:542
Result:
Thus the C program for sum of the digits of a number has been verified and executed
successfully.
Exp 4 FINDING AVERAGE OF ARRAY ELEMENTS
Aim:
To write a C program for finding average of array elements.
Program :
#include<stdio.h>
int main()
{
int a[10],sum,i;
float avg;
printf("Entertenintegers:");
for(i=0;i<=9;i++)
scanf(“%d",&a[i]);
sum=0;
for(i=0;i<=9;i++)
sum=sum+a[i];
avg=sum/10;
printf("\nAverage=%f",avg);
}
Output:
Enter ten integers:10
20
30
40
50
60
70
80
90
100
Average=55.000000
Result:
Thus the C program for using basic do while loop has been verified and executed successfully
Exp 5 CALCULATE LENGTH OF STRING
Aim:
To write a C program to calculate length of string.
Program :
#include<stdio.h>
int main()
{
char s[1000],i;
printf("Enter a string: ");
scanf("%s", s);
for(i=0; s[i]!='\0';++i);
printf("Length of string: %d",i);
return 0;
}
Output
Enter a string: Programiz
Length of string: 9
Result:
Thus the C program to calculate length of string has been verified and executed successfully
Exp 6 C STRING FUNCTION – STRLEN
Aim:
To write a C program for string function – strlen.
Program :
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20]="BeginnersBook";
printf("Length of string str1: %d",strlen(str1));
return 0;
}
Output:
Length of string str1:13
Result:
Thus the C program to string function – strlen has been verified and executed successfully
Exp 7 C STRING FUNCTION – STRCMP
Aim:
To write a C program for string function – strcmp
Program :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[20]="BeginnersBook";
char s2[20]="BeginnersBook.COM";
if(strcmp(s1, s2)==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:
String 1and 2 are different
Result:
Thus the C program for string function – strcmp has been verified and executed
successfully.
Exp 8 C STRING FUNCTION – STRCAT
Aim:
To write a C program for string function – strcat
Program :
#include<stdio.h>
#include<string.h>
int main()
{
char s1[10]="Hello";
char s2[10]="World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
Output:
Output string after concatenation: HelloWorld
Result:
Thus the C program for string function strcat has been verified and executed successfully.
Exp 9 ONE DIMENSIONAL ARRAY IN C
Aim:
To write a C program for one dimensional array in c
Program:
#include<stdio.h>
int main()
{
int arr[5], i;
for(i = 0; i < 5; i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
printf("\nPrinting elements of the array: \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter a[0]: 10
Enter a[1]: 20
Enter a[2]: 30
Enter a[3]: 40
Enter a[4]: 50
10 20 30 40 50
Result:
Thus the C program for one dimensional array in c has been verified and executed
successfully.
Exp 10 TWO DIMENSIONAL ARRAY IN C
Aim:
To write a C program for two dimensional array in c
Program:
#include<stdio.h>
int main()
{
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
OUTPUT:
Enter value for disp[0][0]:10
Enter value for disp[0][1]:20
Enter value for disp[0][2]:30
Enter value for disp[1][0]:40
Enter value for disp[1][1]:50
Enter value for disp[1][2]:60
Two Dimensional array elements:
10 20 30
40 50 60
Result:
Thus the C program for two dimensional array in c has been verified and executed
successfully.
Exp 11 DYNAMIC MEMORY ALLOCATIONS
Aim:
To write a C program for dynamic memory allocations
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Output:
Enter number of elements: 5
Enter elements of array: 10
20
30
40
50
Sum = 150
Result:
Thus the C program for dynamic memory allocations has been verified and executed
successfully.
Exp 12 STRUCTURES AND POINTERS
Aim:
To write a C program for structures and pointers
Program:
#include<stdio.h>
#include<stdlib.h>
struct person
{
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct person*) malloc(num * sizeof(struct person));
for(i = 0; i < num; ++i)
{
printf("Enter name, age and weight of the person respectively:\n");
scanf("%s%d%f", &(ptr+i)->name, &(ptr+i)->age, &(ptr+i)->weight);
}
printf("Displaying Infromation:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);
return 0;
}
Output:
Enter number of persons: 2
Enter name, age and weight of the person respectively:
Adam
2
3.2
Enter name, age and weight of the person respectively:
Eve
6
2.3
Displaying Information:
Adam 2 3.20
Eve 6 2.30
Result:
Thus the C program for structures and pointers has been verified and executed successfully.
Aim:
To write a C program for implementation of stack adt using array in c
Program:
#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--------------------------------");
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\tSTACK is over flow");
}
else
{
printf(" Enter a 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 popped 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:
Enter the size of STACK[MAX=100]:10
50
40
30
20
10
Press Next Choice
Enter the Choice:2
30
20
10
Press Next Choice
Enter the Choice:4
EXIT POINT
Result:
Thus the C program for implementation of stack adt using array has been verified and
executed successfully.
Aim:
To write a C program for implementation of queue adt using array
Program:
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
}
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3
Queue Elements are:
10
54
98
234
Enter the Choice:2
Deleted Element is 10
Enter the Choice:3
Queue Elements are:
54
98
234
Enter the Choice:4
Result:
Thus the C program for implementation of queue adt using array has been verified and
executed successfully.
Aim:
To write a C program for implementation of stack using linked list
Program:
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *link;
}stk;
int main()
{
int ch,item;
stk *top=NULL,*ptr,*temp;
while(1)
{
printf("\n\t\t Stack Operations");
printf("\n\t\t***************");
printf("\n\t\t 1.Push\n\t\t 2.Pop\n\t\t 3.View\n\t\t 4.Exit");
printf("\n\t\t Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\t\t Push");
printf("\n\t\t*****");
printf("\n\t\t Enter the value : ");
scanf("%d",&item);
temp=(stk*)malloc(sizeof(stk));
temp->data=item;
temp->link=top;
top=temp;
top=temp;
Output:
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 1
Push
*****
Enter the value : 5
Pushed element is 5
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 1
Push
******
Enter the value : 4
Pushed element is 4
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 1
Push
*****
Enter the value : 3
Pushed element is 3
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 3
View
******
3->4->5->
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 2
Pop
*****
Popped element is 3
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 3
View
******
4->5->
Stack Operations
***************
1.Push
2.Pop
3.View
4.Exit
Enter your choice : 4
End of program.
Result:
Thus the C program for implementation of stack using linked list has been verified and
executed successfully.
Aim:
To write a C program for implementation of queue using linked list
Program:
#include<stdio.h>
#include<malloc.h>
struct queue
{
int data;
struct queue *link;
};
int main()
{
int i,ch,x;
struct queue *temp,*ptr,*fr,*p,*re=NULL;
printf("\n\t\t Enter the value : ");
scanf("%d",&x);
temp=(struct queue *)malloc(sizeof(struct queue));
temp->data=x;
temp->link=NULL;
re=temp;
ptr=temp;
fr=temp;
while(1)
{
printf("\n\t\t Queue Operations");
printf("\n\t\t***************");
printf("\n\t\t 1.Enqueue\n\t\t 2.Dequeue\n\t\t 3.View\n\t\t 4.Exit");
printf("\n\t\t Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\t\t Enqueue Operations");
printf("\n\t\t****************");
if(re==NULL)
printf("\n\t\t Empty Queue");
else
{
printf("\n\t\t Enter the value : ");
scanf("%d",&x);
temp=(struct queue*)malloc(sizeof(struct queue));
temp->data=x;
re->link=temp;
temp->link=NULL;
re=temp;
}
break;
case 2:printf("\n\t\t Dequeue Operations");
printf("\n\t\t****************");
if(fr==NULL)
printf("\n\t\t Empty Queue");
else
{
p=fr;
printf("\n\t\t Dequeued element is %d",p->data);
fr=fr->link;
free(p);
}
break;
case 3:printf("\n\t\t Content of Queue");
printf("\n\t\t***************\n\t\t");
if(fr==NULL)
printf("\n\t\t Empty Queue");
else
{
ptr=fr;
while(ptr->link!=NULL)
{
printf("%d->",ptr->data);
ptr=ptr->link;
}
printf("%d",ptr->data);
}
break;
case 4:printf("\n\t\t End of program");
exit(0);
default:printf("\n\t\t Valid choice is 1 to 4");
}
}
}
Output:
Enter the value : 5
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 1
Enqueue Operations
*****************
Enter the value : 4
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 1
Enqueue Operations
*****************
Enter the value : 3
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 3
Content of Queue
***************
5->4->3
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 2
Dequeue Operations
*****************
Dequeued element is 5
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 3
Content of Queue
***************
4->3
Queue Operations
***************
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter your choice : 4
End of program
Result:
Thus the C program for implementation of queue using linked list has been verified and
executed successfully.
Aim:
To write a C program to evaluate postfix expression using stack
Program:
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT:
Enter the expression :: 245+*
Thus the C program to evaluate postfix expression using stack has been verified and
executed successfully.
Aim:
To write a C program for implement priority queue to add and delete elements
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();
int pri_que[MAX];
int front, rear;
int main()
{
int n, ch;
create();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter value to be inserted : ");
scanf("%d",&n);
insert_by_priority(n);
break;
case 2:
printf("\nEnter value to delete : ");
scanf("%d",&n);
delete_by_priority(n);
break;
case 3:
display_pqueue();
break;
case 4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}
/* Function to create an empty priority queue */
void create()
{
front = rear = -1;
}
pri_que[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}
Output:
1 - Insert an element into queue
2 - Delete an element from queue
3 - Display queue elements
4 - Exit
Enter your choice : 1
Result:
Thus the C program for implement priority queue to add and delete elements has been verified and
executed successfully.
Aim:
To write a C program for binary search tree
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
return node;
}
int main()
{
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}
Output:
20
30
40
50
60
70
80
Result:
Thus the C program for binary search tree has been verified and executed successfully.
Aim:
To write a c program for implementation of tree
Program:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
return newNode;
}
int main(){
struct node *root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
Output:
123
Result:
Thus the C program for implementation of tree has been verified and executed
successfully.
Exp 21 TREE TRAVERSALS (INORDER, PREORDER AND POSTORDER)
Aim:
To write a C program tree traversals (inorder, preorder and postorder)
Program:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
return(node);
}
printPostorder(node->left);
printPostorder(node->right);
printInorder(node->left);
printInorder(node->right);
}
printPreorder(node->left);
printPreorder(node->right);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
getchar();
return 0;
}
OUTPUT:
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231
Result:
Thus the C program for tree traversals (inorder, preorder and postorder) has been verified
and executed successfully.
Exp 22 BINARY SEARCH TREE OPERATIONS
Aim:
To write a C program for binary search tree operations
Program:
#include<stdio.h>
#include<stdlib.h>
typedef struct tree *node;
node ins(int,node t);
node findmin(node t);
node del(int,node t);
void disp(node t);
struct tree
{
int data;
struct tree *left,*right;
}*root;
int main()
{
node t=NULL;
int data,i,n;
char c;
printf("\n\n\t\t Binary Search tree operations ");
printf("\n\t\t***************************");
printf("\n\t\t Enter the no. of node : ");
scanf("%d",&n);
printf("\n\t\t Enter the elements to insert :\n");
for(i=0;i<n;i++)
{
printf("\n\t\t\t ");
scanf("%d",&data);
t=ins(data,t);
}
l1:
printf("\n\n\t\t Elements displayed in inorder format\n\n\t\t ");
disp(t);
printf("\n\n\t\t Enter the element to delete : ");
scanf("%d",&data);
t=del(data,t);
printf("\n\t\t Content after deletion\n\n\t\t ");
disp(t);
printf("\n\n\t\t Do you want to continue?(y/n) : ");
//c=getch();
//putch(c);
if(c=='y'|| c=='Y')
goto l1;
else
exit(0);
}
node ins(int x,node t)
{
struct tree *newnode;
newnode=malloc(sizeof(struct tree));
if(newnode==NULL)
printf("\n\t\t Out of space.");
else
{
if(t==NULL)
{
newnode->data=x;
newnode->left=NULL;
newnode->right=NULL;
t=newnode;
}
else
{
if(x<t->data)
t->left=ins(x,t->left);
else
t->right=ins(x,t->right);
}
}
return t;
}
node del(int x,node t)
{
node tmp;
if(t==NULL)
printf("\n\t\t Element not found.\n");
else if(x<t->data)
t->left=del(x,t->left);
else if(x>t->data)
t->right=del(x,t->right);
else if(t->left && t->right)
{
tmp=findmin(t->right);
t->data=tmp->data;
t->right=del(t->data,t->right);
}
else
{
tmp=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tmp);
}
return t;
}
node findmin(node t)
{
if(t!=NULL)
{
if(t->left==NULL)
return t;
else
return findmin(t->left);
}
return(0);
}
void disp(node t)
{
if(t!=NULL)
{
disp(t->left);
printf("%d ",t->data);
disp(t->right);
}
}
Output :
Binary Search tree operations
**************************
Enter the no. of node : 5
Enter the elements to insert :
1
3
2
5
7
Elements displayed in inorder format
1 2 3 5 7
Enter the element to delete : 3
Content after deletion
1 2 5 7
Do you want to continue?(y/n) : y
Elements displayed in inorder format
1 2 5 7
Enter the element to delete : 7
Content after deletion
1 2 5
Do you want to continue?(y/n) : y
Elements displayed in inorder format
1 2 5
Enter the element to delete : 1
Content after deletion
2 5
Do you want to continue?(y/n) : n
Result:
Thus the C program for binary search tree operations has been verified and executed
successfully.
Aim:
To write a C program for linear search
Program:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
Output:
Aim:
To write a C program for binary search
Program:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
Aim:
To write a C program for insertion sort
Program:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d--;
}
}
return 0;
}
Output:
Enter number of elements
5
Enter 5 integers
10
60
90
20
30
Sorted list in ascending order:
10
20
30
60
90
Result:
Thus the C program for insertion sort has been verified and executed successfully.
Aim:
To write a C program for bubble sort
Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
return 0;
}
Output:
Enter number of elements
5
Enter 5 integers
20
60
10
90
80
Sorted list in ascending order:
10
20
60
80
90
Result:
Thus the C program for bubble sort has been verified and executed successfully.
Aim:
To write a C program for quick sort
Program:
#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];
quicksort(number,0,count-1);
return 0;
}
Output:
How many elements are u going to enter?: 5
Enter 5 elements: 20
60
90
10
50
Order of Sorted elements: 10 20 50 60 90
Result:
Thus the C program for quick sort has been verified and executed successfully.
Aim:
To write a C program for merge sort
Program:
#include<stdlib.h>
#include<stdio.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
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++;
}
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);
Output:
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
Result:
Thus the C program for merge sort has been verified and executed successfully.
Aim:
To write a C program for implementation of hashing
Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 20
struct DataItem {
int data;
int key;
};
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
return NULL;
}
hashArray[hashIndex] = item;
}
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
return NULL;
}
void display() {
int i = 0;
if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("\n");
}
int main() {
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = -1;
dummyItem->key = -1;
insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);
display();
item = search(37);
if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
delete(item);
item = search(37);
if(item != NULL) {
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
}
Output:
~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44) (13,78) (14,32) ~~ ~~
(17,11) (37,97) ~~
Element found: 97
Element not found
Result:
Thus the C program for implementation of hashing has been verified and executed
successfully.
Aim:
To write a C program for collision resolution in hash table
Program:
#include<stdio.h>
#include<stdlib.h>
struct hash {
struct hashnode *ptr;
};
struct hashnode {
int val;
struct hashnode *next;
};
struct hash *create_hash(int size)
{
struct hash *htable;
int i;
htable = (struct hash *) malloc(sizeof(struct hash) * size);
if (htable == NULL) {
printf("\n Out of Memory !!");
return NULL;
}
for (i = 0; i < size; i++)
htable[i].ptr = NULL;
return htable;
}
struct hashnode *create_hashnode(int val)
{
struct hashnode *tmp;
tmp = (struct hashnode *) malloc(sizeof(struct hashnode));
if (tmp == NULL)
printf("\n System out of Memory");
tmp->next = NULL;
tmp->val = val;
}
int insert_value(struct hash **htable, int val, int hsize)
{
int slot;
struct hash *htptr = (*htable);
struct hashnode *tmp;
if ((*htable) == NULL) {
printf("\n Hash Table is not created");
return 0;
}
slot = (val) % hsize;
if (htptr[slot].ptr == NULL) {
tmp = create_hashnode(val);
htptr[slot].ptr = tmp;
}
else {
tmp = create_hashnode(val);
tmp->next = htptr[slot].ptr;
htptr[slot].ptr = tmp;
}
return 1;
}
void print_hashtable(struct hash **htable, int size)
{
int i;
struct hashnode *tmp;
struct hash *htmp = (*htable);
for (i = 0; i < size; i++) {
tmp = htmp[i].ptr;
while (tmp != NULL) {
printf(" %d ", tmp->val);
tmp = tmp->next;
}
printf("\n");
}
}
int seach_hashtable(struct hash **htable, int val, int size)
{
int slot, found = 0;
slot = val % size;
struct hashnode *tmp;
tmp = (*htable)[slot].ptr;
while (tmp != NULL) {
if (tmp->val == val) {
found = 1;
break;
}
tmp = tmp->next;
}
return found;
}
int main()
{
struct hash *hashtable1;
int hashtable1size;
printf("\n Enter the size of HashTable : ");
scanf("%d", &hashtable1size);
hashtable1 = create_hash(hashtable1size);
if (hashtable1 != NULL)
printf("\nTable Created Successfully !!");
int key, val, found;
do {
printf("\n Hash Table Collision Resolution by Codingstreet.com");
printf("\n 1. Insert value ");
printf("\n 2. Search Value");
printf("\n 3. Print Table");
printf("\n 4. Exit");
printf("\n Enter the key [1-4] :");
scanf("%d", &key);
switch (key) {
case 1:
printf("\n Enter Val : ");
scanf("%d", &val);
insert_value(&hashtable1, val, hashtable1size);
break;
case 2:
printf("\n Enter Val : ");
scanf("%d", &val);
found = seach_hashtable(&hashtable1, val, hashtable1size);
if (found == 0)
printf("\n Val not found");
else
printf("\n %d found at slot %d ", val,
val % hashtable1size);
break;
case 3:
print_hashtable(&hashtable1, hashtable1size);
break;
case 4:
printf("\n Thank you !!! ");
break;
default:
break;
}
} while (key != 4);
printf("\n Thanks for using Codingstreet.com 's Data structure solution\n");
return 0;
}
Output:
Enter the size of HashTable : 5
Table Created Successfully !!
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :1
Enter Val : 10
Enter Val : 20
Enter Val : 30
Enter Val : 40
Enter Val : 30
30 found at slot 0
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :3
40 30 20 10
Enter Val : 40
40 found at slot 0
Hash Table Collision Resolution by Codingstreet.com
1. Insert value
2. Search Value
3. Print Table
4. Exit
Enter the key [1-4] :
Result:
Thus the C program for collision resolution in hash table has been verified and executed
successfully.