Professional Documents
Culture Documents
DSL Record Pandeeswari 19IT1036
DSL Record Pandeeswari 19IT1036
DSL Record Pandeeswari 19IT1036
ON TECHNOLOGY
B.TECH-III SEMESTER
RECORDwORk
Of
NAME : PANDEESwARI.k
REG NO: 19IT1036
Submitted
to
PONDICHERRY
CHERRY ENGINEERING
ENG COLLEGE
PILLACHAVADY,
LLACHAVADY, PUDUCHERRY – 605 014.
(AUTONOMOUS)
JANUARY 2021
DEPARTMENT OF INFORMATION TECHNOLOGY
PONDICHERRY ENGINEERING COLLEGE
PUDUCHERRY – 605 014
2
3
Name :PANDEESWARI.K
PANDEESWARI.K
Roll No :19IT1036
Semester:III
DEPARTMENT OF INFORMATION
ON TECHNOLOGY
PONDICHERRY
CHERRY ENG
ENGINEERING
NG COLLEGE
PUDUCHERRY – 605 014.
BONAFIDE CERTIFICATE
TABLE OFC O N T E N T
5
4 03.10.2020 MERGE SORT
5 03.10.2020 RADIX SORT
14 23.11.2020 STACK
15 23.11.2020 PRIORITY QUEUE
16 23.11.2020 DEQUE
17 07.12.2020 QUEUE IN LINKED LIST
18 07.12.2020 STACK IN LINKED LIST
19 07.12.2020 DEQUE IN LINKED LIST
20 14.12.2020 BINARY TREES
21 14.12.2020 ARRAY AND LINKED IMPLEMENTATION
OF BINARY TREES
22 14.12.2020 TREE TRANSVERSALS
23 21.12.2020 BREADTH FIRST SEARCH
24 21.12.2020 DEPTH FIRST SEARCH
25 21.12.2020 SPANNING TREES
26 21.12.2020 BINARY SEARCH TREE
27 28.12.2020 B-TREE INDEXING
28 28.12.2020 B+ TREES
29 28.12.2020 AVL TREES
30 28.12.2020 HASH TABLE
31 28.12.2020 HASH FUNCTIONS
6
1.BUBBLE SORTING
AIM:
SOURCE CODE:
#include<stdio.h>
void main()
{
int i,j,n,IT36;
printf("Enter number of elements\n");
scanf("%d",&n);
int a[50];
printf("Enter numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
IT36=a[i];
a[i]=a[j];
a[j]=IT36;
}
}
}
7
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
OUTPUT:
RESULT:
Thus the c program for bubble sorting has executed and verified
Successfully.
8
2.INSERTION SORT
AIM:
To write a c program to perform insertion sort.
SOURCE CODE:
#include<stdio.h>
void insertionsort(int a[],int n);
void main()
{
int i,j,IT36;
printf("Enter number of elements\n");
scanf("%d",&IT36);
int a[IT36];
printf("Enter numbers\n");
for(i=0;i<IT36;i++)
{
scanf("%d",&a[i]);
}
insertionsort(a,n);
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
void insertionsort(int a[],int n)
{
int temp,i,j;
for(i=1;i<n;i++)
{
9
j=i;
while((a[j]<a[j-1])&&j>0)
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
}
}
}
OUTPUT:
RESULT:
Thus the c program for insertion sort has executed and verify
Successfully.
10
3.SELECTION SORT
AIM:
To write a c program for performing selection sort.
SOURCE CODE:
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, IT36;
printf("Enter number of elements");
scanf("%d", &n);
printf("Enter %d Numbers", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
IT36=a[i];
a[i]=a[position];
a[position]=IT36;
}
}
printf("Sorted Array:");
for(i = 0; i < n; i++)
printf(" %d ", a[i]);
11
return 0;
}
OUTPUT:
RESULT:
Thus the c program for selection sorting has executed and
verified successfully.
4.MERGE SORT
AIM:
To write a c program for implementing merge sort.
SOURCE CODE:
#include <stdio.h>
12
void merge_sort(int i, int j, int a[], int IT36[]) {
if (j <= i) {
return;
}
int mid = (i + j) / 2;
int pointer_left = i;
int pointer_right = mid + 1;
int k;
pointer_left++;
} else {
IT36[k] = a[pointer_right];
pointer_right++;
}
}
13
int main() {
int a[100], IT36[100], n, i, d, swap;
merge_sort(0, n - 1, a, IT36);
return 0;
}
OUTPUT:
14
RESULT:
Thus the c program for merge sorting has executed and verified
successfully.
5.RADIX SORT
AIM:
To write a c program to perform radix sorting.
SOURCE CODE:
#include<stdio.h>
int get_max (int IT36[], int n){
int max = IT36[0];
for (int i = 1; i <n; i++)
if (IT36[i] >max)
max = IT36[i];
return max;
}
void radix_sort (int IT36[], int n){
int bucket[10][10], bucket_cnt[10];
int i, j, k, r, NOP = 0, divisor = 1, lar, pass;
lar = get_max (IT36, n);
while (lar >0){
NOP++;
lar /= 10;
}
for (pass = 0; pass <NOP; pass++){
for (i = 0; i <10; i++){
bucket_cnt[i] = 0;
}
for (i = 0; i <n; i++){
r = (IT36[i] / divisor) % 10;
bucket[r][bucket_cnt[r]] = IT36[i];
bucket_cnt[r] += 1;
15
}
i = 0;
for (k = 0; k <10; k++){
for (j = 0; j <bucket_cnt[k]; j++){
IT36[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
printf ("After pass %d : ", pass + 1);
for (i = 0; i <n; i++)
printf ("%d ", IT36[i]);
printf ("\n");
}
}
int main (){
int i, n, a[10];
printf ("Enter the number of items to be sorted: ");
scanf ("%d", &n);
printf ("Enter items: ");
for (i = 0; i <n; i++){
scanf ("%d", &a[i]);
}
radix_sort (a, n);
printf ("Sorted items : ");
for (i = 0; i <n; i++)
printf ("%d ", a[i]);
printf ("\n");
return 0;
}
OUTPUT:
16
RESULT:
Thus the c program for radix sorting has executed and verified
successfully.
6.HEAP SORTING
AIM:
To write a c program for performing heap sort.
SOURCE CODE:
#include<stdio.h>
void heapsort(int[],int);
void heapify(int[],int);
17
void adjust(int[],int);
main() {
int n,i,IT36[50];
system("clear");
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the elements:");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\nThe Sorted Elements Are:\n");
for (i=0;i<n;i++)
printf("\t%d",IT36[i]);
printf("\n");
}
void heapsort(int IT36[],int n) {
int i,t;
heapify(a,n);
for (i=n-1;i>0;i--) {
t = a[0];
IT36[0] = IT36[i];
a[i] = t;
adjust(a,i);
}
}
void heapify(int a[],int n) {
int k,i,j,item;
for (k=1;k<n;k++) {
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j])) {
a[i] = a[j];
i = j;
j = (i-1)/2;
}
IT36[i] = item;
}
18
}
void adjust(int a[],int n) {
int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1) {
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i]) {
a[j] = a[i];
j = i;
i = 2*j+1;
} else
break;
}
a[j] = item;
}
OUTPUT:
19
RESULT:
Thus the c program for heap sorting has executed and verified
Successfully.
7.SHELL SORT
AIM:
To write a c program to perform shell sort.
SOURCE CODE:
#include <stdio.h>
void shellsort(int arr[], int num)
{
int i, j, k, IT36;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
IT36 = arr[k];
arr[k] = arr[k+i];
arr[k+i] = IT36;
}
}
}
}
}
int main()
{
20
int arr[30];
int k, num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
printf("\nEnter %d numbers: ", num);
OUTPUT:
RESULT:
Thus the c program for shell sorting has executed and verified
successfully.
21
8.QUICK SORT
AIM:
To write a c program for quick sorting.
SOURCE CODE:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, IT36, temp;
if(first<last){
IT36=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[IT36]&&i<last)
i++;
while(number[j]>number[IT36])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
22
temp=number[IT36];
number[IT36]=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:
23
RESULT:
Thus the c program for quick sort has executed and verified
successfully.
9.LINEAR SEARCH
AIM:
To write a c program to implement linear search.
SOURCE CODE:
#include<stdio.h>
main()
{
int i,a[50],j,n,x,temp,low,mid,high,IT36=0;
printf("Enter size of array\n");
scanf("%d",&n);
24
scanf("%d",&a[i]);
}
do
{
mid=(high+low)/2;
if(x<a[mid])
{
high=mid-1;
IT36=0;
}
else if(x>a[mid])
{
low=mid+1;
IT36=0;
25
}
else
{
IT36=1;
break;
}
}while(x!=a[mid]&&low<=high);
if(IT36==1)
{
printf("Element is present\n");
}
else
{
printf("Element is not presentl\n");
}
OUTPUT:
26
RESULT:
Thus the c program for linear search has executed and verified
successfully.
10.BINARY SEARCH
AIM:
To write a c program to implement binary search.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node{
int data;
struct node *left;
struct node *right;
};
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
27
return newNode;
}
struct queue
{
int front, rear, size;
struct node* *arr;
};
newQueue->front = -1;
newQueue->rear = 0;
newQueue->IT36 = 0;
return newQueue;
}
28
void insertNode(int data) {
if(root == NULL){
root = newNode;
return;
}
else {
enqueue(queue, root);
while(true) {
struct node *node = dequeue(queue);
if(node->left == NULL) {
node->left = newNode;
enqueue(queue, node->left);
}
else {
node->right = newNode;
enqueue(queue, node->right);
}
break;
}
}
29
}
}
if(root == NULL){
printf("Tree is empty\n");
return;
}
else {
if(node->left != NULL)
inorderTraversal(node->left);
printf("%d ", node->data);
if(node->right != NULL)
inorderTraversal(node->right);
}
}
int main(){
insertNode(1);
inorderTraversal(root);
insertNode(2);
insertNode(3);
inorderTraversal(root);
insertNode(4);
30
insertNode(5);
inorderTraversal(root);
insertNode(6);
insertNode(7);
inorderTraversal(root);
return 0;
}
OUTPUT:
RESULT:
31
11.SINGLY LINKED LIST
AIM:
To write a c program to implement the basic operations of singly
linked list.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int IT36;
struct node *next;
};
struct node *head;
32
printf("\n9.exit");
33
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->IT36 = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->IT36 = item;
if(head == NULL)
{
34
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->IT36 = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
35
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
36
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want
to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
37
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->IT36 == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
void display()
{
38
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->IT36);
ptr = ptr -> next;
}
}
}
OUTPUT:
39
40
41
42
RESULT:
Thus the c program for implementing the operations of singly
linked list has executed and verified successfully.
AIM:
To write c program to implement the basic operations of
doubly linked list.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
43
{
struct node *prev;
struct node *next;
int IT36;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n1. Insertion at beginning");
printf("\n2.Insertion at end");
printf("\n3.Insertion after specific position");
printf("\n4.Deletion from beginning");
printf("\n5.Deletion from end");
printf("\n6.Deletion after specific position");
printf("\n7.Search");
printf("\n8.Display");
printf("\n9.Exit");
44
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
45
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->IT36=item;
head=ptr;
}
else
{
ptr->IT36=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->IT36=item;
if(head == NULL)
46
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
47
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->IT36 = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
48
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the IT36 after which the node is to be deleted :
");
scanf("%d", &val);
ptr = head;
while(ptr -> IT36 != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
49
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->IT36);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
50
while (ptr!=NULL)
{
if(ptr->IT36 == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
OUTPUT:
51
52
53
54
RESULT:
Thus the c program for implementating basic operations of
doubly linked list has executed and verified successfully.
36.QUEUE
AIM:
To write a c program to implement the operations of queue using
array.
SOURCE CODE:
#include <stdio.h>
55
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear36 = - 1;
int front36 = - 1;
int main()
{
int IT36;
while (1)
{
56
void insert()
{
int item;
if(rear36 == MAX - 1)
printf("Queue Overflow \n");
else
{
if(front36== - 1)
front36 = 0;
printf("Insert the element in queue : ");
scanf("%d", &item);
rear36 = rear36 + 1;
queue_array[rear36] = item;
}
}
void delete()
{
if(front36 == - 1 || front36>rear36)
{
printf("Queue Underflow n");
return;
}
else
{
printf("Element deleted from queue is : %d \n",
queue_array[front36]);
front36 = front36 + 1;
}
}
void display()
{
int i;
if(front36 == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : ");
for(i = front36; i<= rear36; i++)
57
printf("%d ", queue_array[i]);
printf("\n");
}
}
OUTPUT:
RESULT:
58
Thus the c program for implementating basic operations of
queues using arrays is executed and verified successfully.
14.STACKS
AIM:
To write a c program to implement stacks using arrays.
SOURCE CODE:
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
int top_36=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
while(1)
{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
59
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
void push()
{
int val;
if(top_36==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top_36=top_36+1;
stack[top_36]=val;
}
}
void pop()
{
if(top_36==-1)
{
printf("\nStack is empty!!");
}
else
60
{
printf("\nDeleted element is %d",stack[top_36]);
top_36=top_36-1;
}
}
void display()
{
int i;
if(top_36==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top_36;i>=0;--i)
printf("%d\n",stack[i]);
}
}
61
OUTPUT:
62
RESULT:
Thus the c program for implementating stacks has executed and
verified successfully.
15.PRIORITY QUEUE
AIM:
To write a c program to implement priority queue.
SOURCE CODE:
#include<stdio.h>
#define N 20
int Q[N],Pr[N];
63
int r = -1,f = -1;
void enqueue(int IT36,int p)
{
int i;
if((f==0)&&(r==N-1))
printf("Queue is full");
else
{
if(f==-1)
{
f = r = 0;
Q[r] = IT36;
Pr[r] = p;
}
else if(r == N-1)
{
for(i=f;i<=r;i++) { Q[i-f] = Q[i]; Pr[i-f] = Pr[i]; r = r-f;
f = 0; for(i = r;i>f;i--)
{
if(p>Pr[i])
{
Q[i+1] = Q[i];
Pr[i+1] = Pr[i];
}
else
break;
Q[i+1] = IT36;
Pr[i+1] = p;
r++;
}
}
}
else
{
for(i = r;i>=f;i--)
{
if(p>Pr[i])
64
{
Q[i+1] = Q[i];
Pr[i+1] = Pr[i];
}
else
break;
}
Q[i+1] = IT36;
Pr[i+1] = p;
r++;
}
}
}
void print()
{
int i;
for(i=f;i<=r;i++)
{
printf("\nElement = %d\tPriority = %d",Q[i],Pr[i]);
}
}
int dequeue()
{
if(f == -1)
{
printf("Queue is Empty");
}
else
{
printf("deleted Element = %d\t Its Priority =
%d",Q[f],Pr[f]);
if(f==r)
f = r = -1;
else
f++;
}
}
65
int main()
{
int opt,n,i,IT36,p;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the IT36 in Queue\n2 for show
the IT36 in Queue \n3 for Delete the IT36 from the Queue\n0 for
Exit");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the number of IT36");
scanf("%d",&n);
printf("\nEnter your IT36 and Priority of
IT36");
i=0;
while(i<n){
scanf("%d %d",&IT36,&p);
enqueue(IT36,p);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");
}
}while(opt!=0);
return 0;
}
66
OUTPUT:
67
68
RESULT:
Thus the c program for implementation of priority queue has
executed and verified successfully.
16.DEQUE
AIM:
To write a c program to implement deque.
SOURCE CODE:
69
#include<stdio.h>
#include<conio.h>
#define MAX 10
int deque[MAX];
int leftIT36=-1, rightIT36=-1;
void insert_rightIT36(void);
void insert_leftIT36(void);
void delete_rightIT36(void);
void delete_leftIT36(void);
void display(void);
int main()
{
int choice;
do
{
printf("Deque IT36\n");
printf("\n1.Insert at rightIT36 ");
printf("\n2.Insert at leftIT36 ");
printf("\n3.Delete from rightIT36 ");
printf("\n4.Delete from leftIT36 ");
printf("\n5.Display ");
printf("\n6.Exit");
printf("\n\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_rightIT36();
break;
case 2:
insert_leftIT36();
break;
case 3:
delete_rightIT36();
70
break;
case 4:
delete_leftIT36();
break;
case 5:
display();
break;
}
}while(choice!=6);
getch();
return 0;
}
void insert_rightIT36()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (leftIT36==0 &&rightIT36==MAX-1) ||
(leftIT36==rightIT36+1) )
{
printf("\nOVERFLOW");
}
if(leftIT36==-1) /
{
leftIT36=0;
rightIT36=0;
}
else
{
if(rightIT36==MAX-1)
rightIT36=0;
else
rightIT36=rightIT36+1;
}
deque[rightIT36]=val;
71
}
void insert_leftIT36()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (leftIT36==0 &&rightIT36==MAX-1) ||
(leftIT36==rightIT36+1) )
{
printf("\nOVERFLOW");
}
if(leftIT36==-1) {
leftIT36=0;
rightIT36=0;
}
else
{
if(leftIT36==0)
leftIT36=MAX-1;
else
leftIT36=leftIT36-1;
}
deque[leftIT36]=val;
}
void delete_rightIT36()
{
if(leftIT36==-1)
{
printf("\nUNDERFLOW");
return;
}
printf("\nThe deleted element is %d\n", deque[rightIT36]);
72
if(leftIT36==rightIT36)
{
leftIT36=-1;
rightIT36=-1;
}
else
{
if(rightIT36==0)
rightIT36=MAX-1;
else
rightIT36=rightIT36-1;
}
}
void delete_leftIT36()
{
if(leftIT36==-1)
{
printf("\nUNDERFLOW");
return;
}
printf("\nThe deleted element is %d\n", deque[leftIT36]);
if(leftIT36==rightIT36)
{
leftIT36=-1;
rightIT36=-1;
}
else
{
if(leftIT36==MAX-1)
leftIT36=0;
else
leftIT36=leftIT36+1;
}
}
73
void display()
{
int front=leftIT36, rear=rightIT36;
if(front==-1)
{
printf("\nQueue is Empty\n");
return;
}
printf("\nThe elements in the queue are: ");
if(front<=rear)
{
while(front<=rear)
{
printf("%d\t",deque[front]);
front++;
}
}
else
{
while(front<=MAX-1)
{
printf("%d\t",deque[front]);
front++;
}
front=0;
while(front<=rear)
{
printf("%d\t",deque[front]);
front++;
}
}
printf("\n");
}
74
OUTPUT:
75
RESULT:
Thus the c program for deque using array is executed and
verified successfully.
SOURCE CODE:
76
#include <stdio.h>
#include <stdlib.h>
struct node
{
int IT36;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Enque");
77
scanf("%d", &no);
enq(no);
break;
case 2:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 3:
empty();
break;
case 4:
exit(0);
case 5:
display();
break;
case 6:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create an empty queue */
void create()
{
front = rear = NULL;
}
/* Returns queue size */
void queuesize()
{
printf("\n Queue size : %d", count);
}
78
/* Enqueing the queue */
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->IT36 = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->IT36 = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
/* Displaying the queue elements */
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->IT36);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->IT36);
}
79
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->IT36);
else
return 0;
}
/* Display if queue is empty or not */
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
OUTPUT:
80
RESULT:
Thus the c program for implementation of queues using linked
list is executed and verified successfully.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int IT36;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
81
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
82
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
83
top->ptr = NULL;
top->IT36 = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->IT36 = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->IT36);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
84
printf("\n Popped value : %d", top->IT36);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->IT36);
}
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
void destroy()
{
top1 = top;
while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count =0;
}
85
OUTPUT:
RESULT:
Thus the c program for implementating stacks using linked list is
executed and verified successfully.
86
19.DEQUE IN LINKED LIST
AIM:
To write a c program to implement deque using linked list.
SOURCE CODE:
#include <stdio.h>
struct node
{
int data ;
struct node *link ;
};
struct dqueue
{
struct node *IT36 ;
struct node *rear ;
};
void main( )
{
struct dqueue dq ;
87
int i, n ;
initdqueue ( &dq ) ;
addqatend ( &dq, 11 ) ;
addqatbeg ( &dq, 10 ) ;
addqatend ( &dq, 12 ) ;
addqatbeg ( &dq, 9 ) ;
addqatend ( &dq, 13 ) ;
addqatbeg ( &dq, 8 ) ;
addqatend ( &dq, 14 ) ;
addqatbeg ( &dq, 7 ) ;
display ( dq ) ;
n = count ( dq ) ;
printf ( "\nTotal elements: %d", n ) ;
i = delqatbeg ( &dq ) ;
printf ( "\nItem extracted = %d", i ) ;
i = delqatbeg ( &dq ) ;
printf ( "\nItem extracted = %d", i ) ;
i = delqatbeg ( &dq ) ;
printf ( "\nItem extracted = %d", i ) ;
i = delqatend ( &dq ) ;
printf ( "\nItem extracted = %d", i ) ;
display ( dq ) ;
n = count ( dq ) ;
printf ( "\nElements Left: %d", n ) ;
deldqueue ( &dq ) ;
88
getch( ) ;
}
89
temp -> link = p -> IT36 ;
p -> IT36 = temp ;
}
}
if ( temp == NULL )
p -> rear = NULL ;
return ( item ) ;
}
}
90
}
else
{
while ( temp != p -> rear )
{
rleft = temp ;
temp = temp -> link ;
}
q = p -> rear ;
item = q -> data ;
free ( q ) ;
p -> rear = rleft ;
p -> rear -> link = NULL ;
if ( p -> rear == NULL )
p -> IT36 = NULL ;
return ( item ) ;
}
}
91
int count ( struct dqueue dq )
{
int c = 0 ;
struct node *temp = dq.IT36 ;
return c ;
}
OUTPUT:
92
RESULT:
Thus the c program implementating deque using linked list is
executed and verified successfully.
20.BINARY TREE
AIM:
To write a c program to implement binary tree.
SOURCE CODE:
#include<stdio.h>
node *create()
{
node *p;
93
int x;
printf("Enter IT36(-1 for no IT36):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->IT36=x;
return p;
}
int main()
{
node *root;
root=create();
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
return 0;
}
94
OUTPUT:
RESULT:
Thus the c program for implementating binary trees has executed
and verified successfully.
AIM:
To write a c program to implement binary trees using arrays and
linked list.
SOURCE CODE:
#include<stdio.h>
95
node* insert(char c[],int n)
{ node*tree=NULL;
if(c[n]!='\0')
{
tree=(node*)malloc(sizeof(node));
tree->left=insert(c,2*n+1);
tree->IT36=c[n];
tree->right=insert(c,2*n+2);
}
return tree;
}
//traverse the tree in inorder
void inorder(node*tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%c\t",tree->IT36);
inorder(tree->right);
}
}
void main()
{
node*tree=NULL;
char
c[]={'A','B','C','D','E','F','\0','G','\0','\0','\0','\0','\0','\0','\0','\0','\0',
'\0','\0','\0','\0'};
tree=insert(c,0);
inorder(tree);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
96
struct node{
int IT36;
struct node *left;
struct node *right;
};
newNode->IT36 = IT36;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct queue
{
int front, rear, size;
struct node* *arr;
};
newQueue->front = -1;
newQueue->rear = 0;
newQueue->IT36 = 0;
97
newQueue->arr = (struct node**) malloc(100 * sizeof( struct
node* ));
return newQueue;
}
if(root == NULL){
root = newNode;
return;
}
else {
enqueue(queue, root);
while(true) {
struct node *node = dequeue(queue);
98
if(node->left != NULL && node->right != NULL) {
enqueue(queue, node->left);
enqueue(queue, node->right);
}
else {
if(node->left == NULL) {
node->left = newNode;
enqueue(queue, node->left);
}
else {
node->right = newNode;
enqueue(queue, node->right);
}
break;
}
}
}
}
if(root == NULL){
printf("Tree is empty\n");
return;
}
else {
if(node->left != NULL)
inorderTraversal(node->left);
printf("%d ", node->IT36);
if(node->right != NULL)
inorderTraversal(node->right);
}
}
99
int main(){
insertNode(1);
inorderTraversal(root);
insertNode(2);
insertNode(3);
inorderTraversal(root);
insertNode(4);
insertNode(5);
inorderTraversal(root);
insertNode(6);
insertNode(7);
inorderTraversal(root);
return 0;
}
OUTPUT:
100
RESULT:
Thus the c program for implementating binary trees using arrays
and linked list has executed and verified successfully.
22.TREE TRAVERSALS
AIM:
To write a c program to implement tree traversals.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
101
struct node {
int IT36;
tempNode->IT36 = IT36;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
102
}
else {
current = current->rightChild;
while(current->IT36 != IT36) {
if(current != NULL)
printf("%d ",current->IT36);
//not found
if(current == NULL) {
return NULL;
}
}
return current;
103
}
int main() {
int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };
i = 31;
struct node * temp = search(i);
if(temp != NULL) {
printf("[%d] Element found.", temp->IT36);
104
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}
i = 15;
temp = search(i);
if(temp != NULL) {
printf("[%d] Element found.", temp->IT36);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}
return 0;
}
OUTPUT:
105
RESULT:
Thus the c program to implement tree traversals has executed
and verified successfully.
AIM:
To write a c program to implement breadth first search.
SOURCE CODE:
#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
106
}
}
void main() {
int IT36;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
OUTPUT:
107
RESULT:
Thus the c program to implement breadth first search has
executed and verified successfully.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int source,V,E,IT36,visited[20],G[20][20];
void DFS(int i)
{
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
108
{
if(G[i][j]==1&&visited[j]==0)
DFS(j);
}
}
int main()
{
int i,j,v1,v2;
printf("\t\t\tGraphs\n");
printf("Enter the no of edges:");
scanf("%d",&E);
printf("Enter the no of vertices:");
scanf("%d",&V);
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
G[i][j]=0;
}
for(i=0;i<E;i++)
{
printf("Enter the edges (format: V1 V2) : ");
scanf("%d%d",&v1,&v2);
G[v1-1][v2-1]=1;
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
printf(" %d ",G[i][j]);
printf("\n");
}
printf("Enter the source: ");
scanf("%d",&source);
DFS(source-1);
return 0;
}
109
OUTPUT:
RESULT:
Thus the c program to implement depth first search has executed
and verified successfully.
25.SPANNING TREES
AIM:
To write a c program to implement spanning tree method.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct edge
{
int u;
110
int v;
int IT36;
struct edge *link;
}*front = NULL;
int n;
int main()
{
int i;
struct edge tree[MAX];
int wt_tree = 0;
create_graph();
make_tree(tree);
return 0;
}/*End of main()*/
111
void make_tree(struct edge tree[])
{
struct edge *tmp;
int v1,v2,root_v1,root_v2;
int father[MAX];
int i,count = 0;
while( v1 !=NIL )
{
root_v1 = v1;
v1 = father[v1];
}
while( v2 != NIL )
{
root_v2 = v2;
v2 = father[v2];
}
112
if(count < n-1)
{
printf("\nGraph is not connected, no spanning tree
possible\n");
exit(1);
}
113
struct edge *del_pque()
{
struct edge *tmp;
tmp = front;
front = front->link;
return tmp;
}
int isEmpty_pque( )
{
if ( front == NULL )
return 1;
else
return 0;
}
void create_graph()
{
int i,wt,max_edges,origin,destin;
114
else
insert_pque(origin,destin,wt);
}
}
OUTPUT:
RESULT:
Thus the c program to implement the method of spanning trees
has executed and verified successfully.
AIM:
To write a c program to implement binary search tree.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
115
struct btnode
{
int IT36;
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;
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)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
116
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
void create()
{
int data;
117
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->IT36 = data;
temp->l = temp->r = NULL;
}
void delete()
{
118
int data;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted: ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
119
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->IT36);
}
120
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
121
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}
122
else
return (t->IT36);
}
OUTPUT:
123
RESULT:
Thus the c program to implement binary search tree has
executed and verified successfully.
27.B-TREE INDEXING
AIM:
To write a c program to implement B-tree
SOURCE CODE:
#include<stdio.h>
#include <conio.h>
#define M 5
struct node{
int n;
int keys[M-1];
struct node *p[M];
}*root=NULL;
enum KeyStatus {
Duplicate,SearchFailure,Success,InsertIt,LessKeys };
int main()
{
int key;
int IT36;
printf("Creation of B tree for node %d\n",M);
124
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Search\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your IT36 : ");
scanf("%d",&IT36);
switch(IT36)
{
case 1:
printf("Enter the key : ");
scanf("%d",&key);
insert(key);
break;
case 2:
printf("Enter the key : ");
scanf("%d",&key);
DelNode(key);
break;
case 3:
printf("Enter the key : ");
scanf("%d",&key);
search(key);
break;
case 4:
printf("Btree is :\n");
display(root,0);
break;
case 5:
exit(1);
default:
printf("Wrong IT36\n");
break;
}
}
125
return 0;
}
126
if (pos < n && key == ptr->keys[pos])
return Duplicate;
value = ins(ptr->p[pos], key, &newKey, &newPtr);
if (value != InsertIt)
return value;
if (n < M - 1)
{
pos = searchPos(newKey, ptr->keys, n);
ptr->keys[pos] = newKey;
ptr->p[pos+1] = newPtr;
++ptr->n;
return Success;
}
if (pos == M - 1)
{
lastKey = newKey;
lastPtr = newPtr;
}
else
{
lastKey = ptr->keys[M-2];
lastPtr = ptr->p[M-1];
for (i=M-2; i>pos; i--)
{
ptr->keys[i] = ptr->keys[i-1];
ptr->p[i+1] = ptr->p[i];
}
ptr->keys[pos] = newKey;
ptr->p[pos+1] = newPtr;
127
}
splitPos = (M - 1)/2;
(*upKey) = ptr->keys[splitPos];
(*newnode)=malloc(sizeof(struct node));
ptr->n = splitPos;
(*newnode)->n = M-1-splitPos;
for (i=0; i < (*newnode)->n; i++)
{
(*newnode)->p[i] = ptr->p[i + splitPos + 1];
if(i < (*newnode)->n - 1)
(*newnode)->keys[i] = ptr->keys[i + splitPos + 1];
else
(*newnode)->keys[i] = lastKey;
}
(*newnode)->p[(*newnode)->n] = lastPtr;
return InsertIt;
}/*End of ins()*/
128
printf("Search path:\n");
while (ptr)
{
n = ptr->n;
for (i=0; i < ptr->n; i++)
printf(" %d",ptr->keys[i]);
printf("\n");
pos = searchPos(key, ptr->keys, n);
if (pos < n && key == ptr->keys[pos])
{
printf("Key %d found in position %d of last displayed
node\n",key,i);
return;
}
ptr = ptr->p[pos];
}
printf("Key %d is not available\n",key);
}
129
case LessKeys:
uproot = root;
root = root->p[0];
free(uproot);
break;
}
}
if (ptr == NULL)
return SearchFailure;
n=ptr->n;
key_arr = ptr->keys;
p = ptr->p;
min = (M - 1)/2;
130
{
struct node *qp = p[pos], *qp1;
int nkey;
while(1)
{
nkey = qp->n;
qp1 = qp->p[nkey];
if (qp1 == NULL)
break;
qp = qp1;
}/*End of while*/
key_arr[pos] = qp->keys[nkey-1];
qp->keys[nkey - 1] = key;
}/*End of if */
value = del(p[pos], key);
if (value != LessKeys)
return value;
131
pivot = pos; /*pivot for left and right node*/
lptr = p[pivot];
rptr = p[pivot+1];
/*Assigns values for left node*/
lptr->keys[lptr->n] = key_arr[pivot];
lptr->p[lptr->n + 1] = rptr->p[0];
key_arr[pivot] = rptr->keys[0];
lptr->n++;
rptr->n--;
for (i=0; i < rptr->n; i++)
{
rptr->keys[i] = rptr->keys[i+1];
rptr->p[i] = rptr->p[i+1];
}/*End of for*/
rptr->p[rptr->n] = rptr->p[rptr->n + 1];
return Success;
}/*End of if */
if(pos == n)
pivot = pos-1;
else
pivot = pos;
lptr = p[pivot];
rptr = p[pivot+1];
lptr->keys[lptr->n] = key_arr[pivot];
lptr->p[lptr->n + 1] = rptr->p[0];
for (i=0; i < rptr->n; i++)
{
lptr->keys[lptr->n + 1 + i] = rptr->keys[i];
lptr->p[lptr->n + 2 + i] = rptr->p[i+1];
}
lptr->n = lptr->n + rptr->n +1;
free(rptr);
for (i=pos+1; i < n; i++)
{
key_arr[i-1] = key_arr[i];
132
p[i] = p[i+1];
}
return --ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys;
}
OUTPUT:
133
RESULT:
Thus the c program for b indexing has executed.
28.B+ TREES
AIM:
To write a c program to implement b+ trees.
SOURCE CODE:
// Searching on a B+ Tree in C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
134
#include <string.h>
// Default order
#define ORDER 3
// Node
typedef struct node {
void **pointers;
int *keys;
struct node *parent;
bool is_leaf;
int num_keys;
struct node *next;
} node;
// Enqueue
void enqueue(node *new_node);
// Dequeue
node *dequeue(void);
int height(node *const root);
int pathToLeaves(node *const root, node *child);
void printLeaves(node *const root);
void printTree(node *const root);
void findAndPrint(node *const root, int key, bool verbose);
void findAndPrintRange(node *const root, int range1, int range2,
bool verbose);
int findRange(node *const root, int key_start, int key_end, bool
verbose,
int returned_keys[], void *returned_pointers[]);
135
node *findLeaf(node *const root, int key, bool verbose);
record *find(node *root, int key, bool verbose, node **leaf_out);
int cut(int length);
// Enqueue
void enqueue(node *new_node) {
node *c;
if (queue == NULL) {
queue = new_node;
queue->next = NULL;
} else {
c = queue;
while (c->next != NULL) {
c = c->next;
}
c->next = new_node;
new_node->next = NULL;
}
}
136
// Dequeue
node *dequeue(void) {
node *n = queue;
queue = queue->next;
n->next = NULL;
return n;
}
// Calculate height
int height(node *const root) {
int h = 0;
137
node *c = root;
while (!c->is_leaf) {
c = c->pointers[0];
h++;
}
return h;
}
if (root == NULL) {
printf("Empty tree.\n");
return;
}
queue = NULL;
enqueue(root);
while (queue != NULL) {
n = dequeue();
if (n->parent != NULL && n == n->parent->pointers[0]) {
new_rank = pathToLeaves(root, n);
if (new_rank != rank) {
rank = new_rank;
138
printf("\n");
}
}
if (verbose_output)
printf("(%p)", n);
for (i = 0; i < n->num_keys; i++) {
if (verbose_output)
printf("%p ", n->pointers[i]);
printf("%d ", n->keys[i]);
}
if (!n->is_leaf)
for (i = 0; i <= n->num_keys; i++)
enqueue(n->pointers[i]);
if (verbose_output) {
if (n->is_leaf)
printf("%p ", n->pointers[order - 1]);
else
printf("%p ", n->pointers[n->num_keys]);
}
printf("| ");
}
printf("\n");
}
139
bool verbose) {
int i;
int array_size = key_end - key_start + 1;
int returned_keys[array_size];
void *returned_pointers[array_size];
int num_found = findRange(root, key_start, key_end, verbose,
returned_keys, returned_pointers);
if (!num_found)
printf("None found.\n");
else {
for (i = 0; i < num_found; i++)
printf("Key: %d Location: %p IT36: %d\n",
returned_keys[i],
returned_pointers[i],
((record *)
returned_pointers[i])
->IT36);
}
}
140
}
n = n->pointers[order - 1];
i = 0;
}
return num_found;
}
141
printf("%d] ->\n", c->keys[i]);
}
return c;
}
int i = 0;
node *leaf = NULL;
142
record *new_record = (record *)malloc(sizeof(record));
if (new_record == NULL) {
perror("Record creation.");
exit(EXIT_FAILURE);
} else {
new_record->IT36 = IT36;
}
return new_record;
}
node *makeNode(void) {
node *new_node;
new_node = malloc(sizeof(node));
if (new_node == NULL) {
perror("Node creation.");
exit(EXIT_FAILURE);
}
new_node->keys = malloc((order - 1) * sizeof(int));
if (new_node->keys == NULL) {
perror("New node keys array.");
exit(EXIT_FAILURE);
}
new_node->pointers = malloc(order * sizeof(void *));
if (new_node->pointers == NULL) {
perror("New node pointers array.");
exit(EXIT_FAILURE);
}
new_node->is_leaf = false;
new_node->num_keys = 0;
new_node->parent = NULL;
new_node->next = NULL;
return new_node;
}
node *makeLeaf(void) {
node *leaf = makeNode();
leaf->is_leaf = true;
return leaf;
143
}
insertion_point = 0;
while (insertion_point < leaf->num_keys && leaf-
>keys[insertion_point] < key)
insertion_point++;
for(i=leaf->num_keys;i>insertion_point;i--){
leaf->keys[i]=leaf->keys[i-1];
leaf->pointers[i]=leaf->pointers[i-1];
}
leaf->keys[insertion_point]=key;
leaf->pointers[insertion_point]=pointer;
leaf->num_keys++;
int returnleaf;
}
node*insertIntoLeafAfterSplitting(node*root,node*leaf,int
key,record*pointer){
node*new_leaf;
int*temp_keys;
void**temp_pointers;
int insertion_index,split,new_key,i,j;
new_leaf=makeLeaf();
temp_keys=malloc(order*sizeof(int));
if(temp_keys==NULL){
144
perror("Temporarykeysarray.");
exit(EXIT_FAILURE);
}
temp_pointers=malloc(order*sizeof(void*));
if(temp_pointers==NULL){
perror("Temporarypointersarray.");
exit(EXIT_FAILURE);
}
insertion_index=0;
while(insertion_index<order-1&&leaf-
>keys[insertion_index]<key)
insertion_index++;
for(i=0,j=0;i<leaf->num_keys;i++,j++){
if(j==insertion_index)
j++;
temp_keys[j]=leaf->keys[i];
temp_pointers[j]=leaf->pointers[i];
}
temp_keys[insertion_index]=key;
temp_pointers[insertion_index]=pointer;
leaf->num_keys=0;
split=cut(order-1);
for(i=0;i<split;i++){
leaf->pointers[i]=temp_pointers[i];
leaf->keys[i]=temp_keys[i];
leaf->num_keys++;
}
for(i=split,j=0;i<order;i++,j++){
new_leaf->pointers[j]=temp_pointers[i];
new_leaf->keys[j]=temp_keys[i];
145
new_leaf->num_keys++;
}
free(temp_pointers);
free(temp_keys);
new_leaf->pointers[order-1]=leaf->pointers[order-1];
leaf->pointers[order-1]=new_leaf;
for(i=leaf->num_keys;i<order-1;i++)
leaf->pointers[i]=NULL;
for(i=new_leaf->num_keys;i<order-1;i++)
new_leaf->pointers[i]=NULL;
new_leaf->parent=leaf->parent;
new_key=new_leaf->keys[0];
return insertIntoParent(root,leaf,new_key,new_leaf);
}
node*insertIntoNode(node*root,node*n,
int left_index,int key,node*right){
int i;
for(i=n->num_keys;i>left_index;i--){
n->pointers[i+1]=n->pointers[i];
n->keys[i]=n->keys[i-1];
}
n->pointers[left_index+1]=right;
n->keys[left_index]=key;
n->num_keys++;
return root;
}
node*insertIntoNodeAfterSplitting(node*root,node*old_node,int
left_index,
int key,node*right){
int i,j,split,k_prime;
146
node*new_node,*child;
int*temp_keys;
node**temp_pointers;
temp_pointers=malloc((order+1)*sizeof(node*));
if(temp_pointers==NULL){
exit(EXIT_FAILURE);
}
temp_keys=malloc(order*sizeof(int));
if(temp_keys==NULL){
exit(EXIT_FAILURE);
}
for(i=0,j=0;i<old_node->num_keys+1;i++,j++){
if(j==left_index+1)
j++;
temp_pointers[j]=old_node->pointers[i];
}
for(i=0,j=0;i<old_node->num_keys;i++,j++){
if(j==left_index)
j++;
temp_keys[j]=old_node->keys[i];
}
temp_pointers[left_index+1]=right;
temp_keys[left_index]=key;
split=cut(order);
new_node=makeNode();
old_node->num_keys=0;
for(i=0;i<split-1;i++){
old_node->pointers[i]=temp_pointers[i];
old_node->keys[i]=temp_keys[i];
old_node->num_keys++;
}
old_node->pointers[i]=temp_pointers[i];
k_prime=temp_keys[split-1];
147
for(++i,j=0;i<order;i++,j++){
new_node->pointers[j]=temp_pointers[i];
new_node->keys[j]=temp_keys[i];
new_node->num_keys++;
}
new_node->pointers[j]=temp_pointers[i];
free(temp_pointers);
free(temp_keys);
new_node->parent=old_node->parent;
for(i=0;i<=new_node->num_keys;i++){
child=new_node->pointers[i];
child->parent=new_node;
}
return insertIntoParent(root,old_node,k_prime,new_node);
}
node*insertIntoParent(node*root,node*left,int key,node*right){
int left_index;
node*parent;
parent=left->parent;
if(parent==NULL)
return insertIntoNewRoot(left,key,right);
left_index=getLeftIndex(parent,left);
if(parent->num_keys<order-1)
return insertIntoNode(root,parent,left_index,key,right);
return
insertIntoNodeAfterSplitting(root,parent,left_index,key,right);
}
node*insertIntoNewRoot(node*left,int key,node*right){
node*root=makeNode();
root->keys[0]=key;
148
root->pointers[0]=left;
root->pointers[1]=right;
root->num_keys++;
root->parent=NULL;
left->parent=root;
right->parent=root;
return root;
}
node*startNewTree(int key,record*pointer){
node*root=makeLeaf();
root->keys[0]=key;
root->pointers[0]=pointer;
root->pointers[order-1]=NULL;
root->parent=NULL;
root->num_keys++;
return root;
}
record_pointer=find(root,key,false,NULL);
if(record_pointer!=NULL){
record_pointer->IT36=IT36;
return root;
}
record_pointer=makeRecord(IT36);
if(root==NULL)
return startNewTree(key,record_pointer);
leaf=findLeaf(root,key,false);
if(leaf->num_keys<order-1){
leaf=insertIntoLeaf(leaf,key,record_pointer);
149
return root;
}
return insertIntoLeafAfterSplitting(root,leaf,key,record_pointer);
}
int main(){
node*root;
char instruction;
root=NULL;
root=insert(root,5,33);
root=insert(root,15,21);
root=insert(root,25,31);
root=insert(root,35,41);
root=insert(root,45,10);
printTree(root);
findAndPrint(root,15,instruction='a');
}
OUTPUT:
150
RESULT:
Thus the c program to implement b+ trees has executed and
verified successfully.
29.AVL TREES
AIM:
To write a c program to implement avl tree.
SOURCE CODE:
#include<stdio.h>
151
node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int height( node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
int main()
{
node *root=NULL;
int x,n,i,op;
do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree IT36:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
152
break;
return 0;
}
153
if(x>T->right->IT36)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->IT36)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->IT36)
T=LL(T);
else
T=LR(T);
}
T->ht=height(T);
return(T);
}
if(T==NULL)
{
return NULL;
}
else
if(x > T->IT36) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
154
}
else
if(x<T->IT36)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//IT36 to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left!= NULL)
p=p->left;
T->IT36=p->IT36;
T->right=Delete(T->right,p->IT36);
155
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
156
x->ht=height(x);
y->ht=height(y);
return(y);
}
return(T);
}
157
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
OUTPUT:
158
159
RESULT:
Thus the c program to implement avl tree has executed and
verified successfully.
30.HASH TABLE
AIM:
To write a c program to implement hash table.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
160
struct set
{
int key;
int IT36;
};
struct set *array;
int capacity = 10;
int size = 0;
int hashFunction(int key)
{
return (key % capacity);
}
int checkPrime(int n)
{
int i;
if (n == 1 || n == 0)
{
return 0;
}
for (i = 2; i < n / 2; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
int getPrime(int n)
{
if (n % 2 == 0)
{
n++;
}
while (!checkPrime(n))
{
n += 2;
}
161
return n;
}
void init_array()
{
capacity = getPrime(capacity);
array = (struct set *)malloc(capacity * sizeof(struct set));
for (int i = 0; i < capacity; i++)
{
array[i].key = 0;
array[i].IT36 = 0;
}
}
void insert(int key, int IT36)
{
int index = hashFunction(key);
if (array[index].IT36 == 0)
{
array[index].key = key;
array[index].IT36 = IT36;
size++;
printf("\n Key (%d) has been inserted \n", key);
}
else if (array[index].key == key)
{
array[index].IT36 = IT36;
}
else
{
printf("\n Collision occured \n");
}
}
void remove_element(int key)
{
int index = hashFunction(key);
if (array[index].IT36 == 0)
{
printf("\n This key does not exist \n");
}
162
else
{
array[index].key = 0;
array[index].IT36 = 0;
size--;
printf("\n Key (%d) has been removed \n", key);
}
}
void display()
{
int i;
for (i = 0; i < capacity; i++)
{
if (array[i].IT36 == 0)
{
printf("\n array[%d]: / ", i);
}
else
{
printf("\n key: %d array[%d]: %d \t", array[i].key, i,
array[i].IT36);
}
}
}
int size_of_hashtable()
{
return size;
}
int main()
{
int choice, key, IT36, n;
int c = 0;
init_array();
do
{
printf("1.Insert item in the Hash Table"
"\n2.Remove item from the Hash Table"
"\n3.Check the size of Hash Table"
163
"\n4.Display a Hash Table"
"\n\n Please enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter key -:\t");
scanf("%d", &key);
printf("Enter IT36 -:\t");
scanf("%d", &IT36);
insert(key, IT36);
break;
case 2:
printf("Enter the key to delete-:");
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Invalid Input\n");
}
printf("\nDo you want to continue (press 1 for yes): ");
scanf("%d", &c);
} while (c == 1);
}
OUTPUT:
164
165
RESULT:
Thus the c program to implement hash table has executed and
verified successfully.
31.HASH FUNCTIONS
AIM:
To write a c program to implement hash functions.
SOURCE CODE:
#include<stdio.h>
#include<limits.h>
166
printf("Enter key element to insert\n");
scanf("%d",&element);
IT36 = element%hFn;
while(ary[IT36]!= INT_MIN) {
if(ary[IT36]== INT_MAX)
break;
IT36 = (IT36+1)%hFn;
n++;
if(n==size)
break;
}
if(n==size)
printf("Hash table was full of elements\nNo Place to insert
this element\n\n");
else
ary[IT36] = element;
}
int element,n=0,IT36;
IT36 = element%hFn;
while(n++ != size){
if(ary[IT36]==INT_MIN){
printf("Element not found in hash table\n");
break;
}
else if(ary[IT36]==element){
ary[IT36]=INT_MAX;
167
printf("Element deleted\n\n");
break;
}
else{
IT36 = (IT36+1) % hFn;
}
}
if(--n==size)
printf("Element not found in hash table\n");
}
IT36 = element%hFn;
while(n++ != size){
if(ary[IT36]==element){
printf("Element found at index %d\n",IT36);
break;
}
else
if(ary[IT36]==INT_MAX ||ary[IT36]!=INT_MIN)
IT36 = (IT36+1) %hFn;
}
if(--n==size) printf("Element not found in hash table\n");
}
printf("Index\tValue\n");
for(i=0;i<size;i++)
printf("%d\t%d\n",i,ary[i]);
168
}
int main(){
int size,hFn,i,choice;
int ary[size];
for(i=0;i<size;i++)
ary[i]=INT_MIN;
do{
printf("Enter your choice\n");
printf(" 1-> Insert\n 2-> Delete\n 3-> Display\n 4->
Searching\n 0-> Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
insert(ary,hFn,size);
break;
case 2:
delete(ary,hFn,size);
break;
case 3:
display(ary,size);
break;
case 4:
search(ary,hFn,size);
break;
default:
printf("Enter correct choice\n");
break;
}
169
}while(choice);
return 0;
}
OUTPUT:
170
RESULT:
Thus the c program to the implement hash functions has
executed and verified successfully.
171