DSL Record Pandeeswari 19IT1036

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 171

DEPARTMENT OF INFORMATION

ON TECHNOLOGY

B.TECH-III SEMESTER

RECORDwORk

Of

IT 205 - DATA STRUCTURES LABORATORY


LAB

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

Subject :IT 205 - DATA STRUCTURES LABORATORY

DEPARTMENT OF INFORMATION
ON TECHNOLOGY
PONDICHERRY
CHERRY ENG
ENGINEERING
NG COLLEGE
PUDUCHERRY – 605 014.
BONAFIDE CERTIFICATE

This is to Certify that the practical record of IT 205-Data


Structure Laboratory is a bonafide work done by Mr/MsM. S.
PANDEESWARI.K Reg no: 19IT1036 of III
SemesterB.Tech(Information Technology) of Pondicherry
Engineering College, Puducherryduring the year 2020-2021.

Staff In-charge Head of the Department

Submitted for the university examination held on ________________

Internal Examiner External Examiner

TABLE OFC O N T E N T

ProgramNo. Date Name of the Experiment Marks Staff


Out of 10 Sign
1 21.09.2020 BUBBLE SORT
2 21.09.2020 INSERTION SORT

3 21.09.2020 SELECTION SORT

5
4 03.10.2020 MERGE SORT
5 03.10.2020 RADIX SORT

6 03.10.2020 HEAP SORT


7 11.10.2020 SHELL SORT
8 11.10.2020 QUICK SORT

9 11.10.2020 LINEAR SEARCH

10 11.10.2020 BINARY SEARCH


11 01.11.2020 SINGLY LINKED LIST
12 08.11.2020 DOUBLY LINKED LIST
13 23.11.2020 QUEUE

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:

To write a c program to do bubble sorting.

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;
}
}
}

printf("Ascending order of given numbers is\n");

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);

printf("Ascending order of given numbers is\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;

merge_sort(i, mid, a, IT36);


merge_sort(mid + 1, j, a, IT36);

int pointer_left = i;
int pointer_right = mid + 1;
int k;

for (k = i; k <= j; k++) {


if (pointer_left == mid + 1) {
IT36[k] = a[pointer_right];
pointer_right++;
} else if (pointer_right == j + 1) {
IT36[k] = a[pointer_left];
pointer_left++;
} else if (a[pointer_left] < a[pointer_right]) {
IT36[k] = a[pointer_left];

pointer_left++;
} else {
IT36[k] = a[pointer_right];
pointer_right++;
}
}

for (k = i; k <= j; k++) {


}
}

13
int main() {
int a[100], IT36[100], n, i, d, swap;

printf("Enter number of elements in the array:\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)


scanf("%d", &a[i]);

merge_sort(0, n - 1, a, IT36);

printf("Printing the sorted array:\n");

for (i = 0; i < n; i++)


printf("%d\n", a[i]);

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);

for (k = 0 ; k < num; k++)


{
scanf("%d", &arr[k]);
}
shellsort(arr, num);
printf("\n Sorted array is: ");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
return 0;
}

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];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}

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);

printf("Enter the numbers in any order\n");


for(i=0;i<n;i++)
{

24
scanf("%d",&a[i]);
}

printf("Enter the element to be searched\n");


scanf("%d",&x);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

printf("Sorted array is\n");


for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
low=1;
high=n;

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;
};

struct node *root = NULL;

struct node* createNode(int data){

struct node *newNode = (struct node*)malloc(sizeof(struct


node));

newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;

27
return newNode;
}

struct queue
{
int front, rear, size;
struct node* *arr;
};

struct queue* createQueue()


{
struct queue* newQueue = (struct queue*) malloc(sizeof( struct
queue ));

newQueue->front = -1;
newQueue->rear = 0;
newQueue->IT36 = 0;

newQueue->arr = (struct node**) malloc(100 * sizeof( struct


node* ));

return newQueue;
}

void enqueue(struct queue* queue, struct node *temp){


queue->arr[queue->rear++] = temp;
queue->IT36++;
}

struct node *dequeue(struct queue* queue){


queue->IT36--;
return queue->arr[++queue->front];
}

28
void insertNode(int data) {

struct node *newNode = createNode(data);

if(root == NULL){
root = newNode;
return;
}
else {

struct queue* queue = createQueue();

enqueue(queue, root);

while(true) {
struct node *node = dequeue(queue);

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;
}
}

29
}
}

void inorderTraversal(struct node *node) {

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);

printf("Binary tree after insertion: \n");

inorderTraversal(root);

insertNode(2);
insertNode(3);

printf("\nBinary tree after insertion: \n");

inorderTraversal(root);

insertNode(4);

30
insertNode(5);

printf("\nBinary tree after insertion: \n");

inorderTraversal(root);

insertNode(6);
insertNode(7);

printf("\nBinary tree after insertion: \n");

inorderTraversal(root);

return 0;
}

OUTPUT:

RESULT:

Thus the c program for implementing binary search has executed


and verified successfully.

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;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
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 for an element");
printf("\n8.Display");

32
printf("\n9.exit");

printf("\nEnter your choice\n");


scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}

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.

12.DOUBLY LINKED LIST

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");

printf("\nEnter your choice?\n");


scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:

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)
{

printf("1.Insert element to queue \n");


printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your IT36 : ");
scanf("%d", &IT36);
switch(IT36)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong IT36 n");
}
}
}

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);

default: printf("\nWrong Choice!!");


}
}
}

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.

17.QUEUE IN LINKED LIST


AIM:
To write a c program to implement the operations of queue using
linked list.

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");

printf("\n 2- Front element");


printf("\n 3 - Empty");
printf("\n 4 - Exit");
printf("\n 5 - Display");
printf("\n 6 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");

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.

18.STACK IN LINKED LIST


AIM:
To write a c program to implement operations of stack using
linked list.

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);
}

void push(int data)


{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));

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 initdqueue ( struct dqueue * ) ;


void addqatend ( struct dqueue *, int item ) ;
void addqatbeg ( struct dqueue *, int item ) ;
int delqatbeg ( struct dqueue * ) ;
int delqatend ( struct dqueue * ) ;
void display ( struct dqueue ) ;
int count ( struct dqueue ) ;
void deldqueue ( struct dqueue * ) ;

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( ) ;
}

void initdqueue ( struct dqueue *p )


{
p -> IT36 = p -> rear = NULL ;
}

void addqatend ( struct dqueue *p, int item )


{
struct node *temp ;

temp = ( struct node * ) malloc ( sizeof ( struct node ) );


temp -> data = item ;
temp -> link = NULL ;

if ( p -> IT36 == NULL )


p -> IT36 = temp ;
else
p -> rear -> link = temp ;
p -> rear = temp ;
}

void addqatbeg ( struct dqueue *p, int item )


{
struct node *temp ;
int *q ;
temp = ( struct node * ) malloc ( sizeof ( struct node ) );
temp -> data = item ;
temp -> link = NULL ;

if ( p -> IT36 == NULL )


p -> IT36 = p -> rear = temp ;
else
{

89
temp -> link = p -> IT36 ;
p -> IT36 = temp ;
}
}

int delqatbeg ( struct dqueue *p )


{
struct node *temp = p -> IT36 ;
int item ;
if ( temp == NULL )
{
printf ( "\nQueue is empty." ) ;
return 0 ;
}
else
{
temp = p -> IT36 ;
item = temp -> data ;
p -> IT36 = temp -> link ;
free ( temp ) ;

if ( temp == NULL )
p -> rear = NULL ;
return ( item ) ;
}
}

int delqatend ( struct dqueue *p )


{
struct node *temp , *rleft, *q ;
int item ;
temp = p -> IT36 ;
if ( p -> rear == NULL )
{
printf ( "\nQueue is empty." ) ;
return 0 ;

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 ) ;
}
}

void display ( struct dqueue dq )


{
struct node *temp = dq.IT36 ;

printf ( "\nIT36 ->" ) ;


while ( temp != NULL )
{
if ( temp -> link == NULL )
{
printf ( "\t%d", temp -> data ) ;
printf ( "<- rear" ) ;
}
else
printf ( "\t%d", temp -> data ) ;
temp = temp -> link ;
}
printf ( "\n" ) ;
}

91
int count ( struct dqueue dq )
{
int c = 0 ;
struct node *temp = dq.IT36 ;

while ( temp != NULL )


{
temp = temp -> link ;
c++ ;
}

return c ;
}

void deldqueue ( struct dqueue *p )


{
struct node *temp ;

if ( p -> IT36 == NULL )


return ;

while ( p -> IT36 != NULL )


{
temp = p -> IT36 ;
p -> IT36 = p -> IT36 -> link ;
free ( temp ) ;
}
};

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>

typedef struct node


{
int IT36;
struct node *left;
struct node *right;
} node;

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;

printf("Enter left child of %d:\n",x);


p->left=create();

printf("Enter right child of %d:\n",x);


p->right=create();

return p;
}

void preorder(node *t)


{
if(t!=NULL)
{
printf("\n%d",t->IT36);
preorder(t->left);
preorder(t->right);
}
}

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.

20.ARRAY AND LINKED LIST


IMPLEMENTATION OF BINARY TREES.

AIM:
To write a c program to implement binary trees using arrays and
linked list.

SOURCE CODE:
#include<stdio.h>

typedef struct node


{
struct node*left;
struct node*right;
char IT36;
}node;

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;
};

struct node *root = NULL;

struct node* createNode(int IT36){

struct node *newNode = (struct node*)malloc(sizeof(struct


node));

newNode->IT36 = IT36;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

struct queue
{
int front, rear, size;
struct node* *arr;
};

struct queue* createQueue()


{
struct queue* newQueue = (struct queue*) malloc(sizeof( struct
queue ));

newQueue->front = -1;
newQueue->rear = 0;
newQueue->IT36 = 0;

97
newQueue->arr = (struct node**) malloc(100 * sizeof( struct
node* ));

return newQueue;
}

void enqueue(struct queue* queue, struct node *temp){


queue->arr[queue->rear++] = temp;
queue->IT36++;
}

struct node *dequeue(struct queue* queue){


queue->IT36--;
return queue->arr[++queue->front];
}

void insertNode(int IT36) {

struct node *newNode = createNode(IT36);

if(root == NULL){
root = newNode;
return;
}
else {

struct queue* queue = createQueue();

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;
}
}
}
}

void inorderTraversal(struct node *node) {

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);

printf("Binary tree after insertion: \n");

inorderTraversal(root);

insertNode(2);
insertNode(3);

printf("\nBinary tree after insertion: \n");

inorderTraversal(root);

insertNode(4);
insertNode(5);

printf("\nBinary tree after insertion: \n");

inorderTraversal(root);

insertNode(6);
insertNode(7);

printf("\nBinary tree after insertion: \n");

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;

struct node *leftChild;


struct node *rightChild;
};

struct node *root = NULL;

void insert(int IT36) {


struct node *tempNode = (struct node*) malloc(sizeof(struct
node));
struct node *current;
struct node *parent;

tempNode->IT36 = IT36;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;

if(IT36 < parent->IT36) {


current = current->leftChild;

//insert to the left


if(current == NULL) {
parent->leftChild = tempNode;
return;
}

102
}
else {
current = current->rightChild;

//insert to the right


if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}

struct node* search(int IT36) {


struct node *current = root;
printf("Visiting elements: ");

while(current->IT36 != IT36) {
if(current != NULL)
printf("%d ",current->IT36);

if(current->IT36 > IT36) {


current = current->leftChild;
}
//else go to right tree
else {
current = current->rightChild;
}

//not found
if(current == NULL) {
return NULL;
}
}

return current;

103
}

void pre_order_traversal(struct node* root) {


if(root != NULL) {
printf("%d ",root->IT36);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}
}

void inorder_traversal(struct node* root) {


if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->IT36);
inorder_traversal(root->rightChild);
}
}

void post_order_traversal(struct node* root) {


if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
printf("%d ", root->IT36);
}
}

int main() {
int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };

for(i = 0; i < 7; i++)


insert(array[i]);

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);
}

printf("\nPreorder traversal: ");


pre_order_traversal(root);

printf("\nInorder traversal: ");


inorder_traversal(root);

printf("\nPost order traversal: ");


post_order_traversal(root);

return 0;
}

OUTPUT:

105
RESULT:
Thus the c program to implement tree traversals has executed
and verified successfully.

23.BREADTH FIRST SEARCH

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;

void bfs(int IT36) {


for(i = 1; i <= n; i++)
if(a[IT36][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);

106
}
}

void main() {
int IT36;
printf("\n Enter the number of vertices:");
scanf("%d", &n);

for(i=1; i <= n; i++) {


q[i] = 0;
visited[i] = 0;
}

printf("\n Enter graph data in matrix form:\n");


for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}

printf("\n Enter the starting vertex:");


scanf("%d", &IT36);
bfs(IT36);
printf("\n The node which are reachable are:\n");

for(i=1; i <= n; i++) {


if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are reachable");
break;
}
}
}

OUTPUT:

107
RESULT:
Thus the c program to implement breadth first search has
executed and verified successfully.

24.DEPTH FIRST SEARCH


AIM:

To write a c program to implement depth first search.

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>

#define MAX 100


#define NIL -1

struct edge
{
int u;

110
int v;
int IT36;
struct edge *link;
}*front = NULL;

void make_tree(struct edge tree[]);


void insert_pque(int i,int j,int wt);
struct edge *del_pque();
int isEmpty_pque( );
void create_graph();

int n;

int main()
{
int i;
struct edge tree[MAX];
int wt_tree = 0;

create_graph();

make_tree(tree);

printf("\nEdges to be included in minimum spanning tree are


:\n");
for(i=1; i<=n-1; i++)
{
printf("\n%d->",tree[i].u);
printf("%d\n",tree[i].v);
wt_tree += tree[i].IT36;
}
printf("\nIT36 of this minimum spanning tree is : %d\n",
wt_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;

for(i=0; i<n; i++)


father[i] = NIL;

while( !isEmpty_pque( ) && count < n-1 )


{
tmp = del_pque();
v1 = tmp->u;
v2 = tmp->v;

while( v1 !=NIL )
{
root_v1 = v1;
v1 = father[v1];
}
while( v2 != NIL )
{
root_v2 = v2;
v2 = father[v2];
}

if( root_v1 != root_v2 )/*Insert the edge (v1, v2)*/


{
count++;
tree[count].u = tmp->u;
tree[count].v = tmp->v;
tree[count].IT36 = tmp->IT36;
father[root_v2]=root_v1;
}
}

112
if(count < n-1)
{
printf("\nGraph is not connected, no spanning tree
possible\n");
exit(1);
}

void insert_pque(int i,int j,int wt)


{
struct edge *tmp,*q;

tmp = (struct edge *)malloc(sizeof(struct edge));


tmp->u = i;
tmp->v = j;
tmp->IT36 = wt;

/*Queue is empty or edge to be added has IT36 less than first


edge*/
if( front == NULL || tmp->IT36 < front->IT36 )
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while( q->link != NULL && q->link->IT36 <= tmp->IT36 )
q = q->link;
tmp->link = q->link;
q->link = tmp;
if(q->link == NULL) /*Edge to be added at the end*/
tmp->link = NULL;
}
}

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;

printf("\nEnter number of vertices : ");


scanf("%d",&n);
max_edges = n*(n-1)/2;

for(i=1; i<=max_edges; i++)


{
printf("\nEnter edge %d(-1 -1 to quit): ",i);
scanf("%d %d",&origin,&destin);
if( (origin == -1) && (destin == -1) )
break;
printf("\nEnter IT36 for this edge : ");
scanf("%d",&wt);
if( origin >= n || destin >= n || origin<0 || destin<0)
{
printf("\nInvalid edge!\n");
i--;
}

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.

26.BINARY SEARCH TREE

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 search(struct btnode *t)


{
if ((temp->IT36 > t->IT36) && (t->r != NULL))
search(t->r);
else if ((temp->IT36 > t->IT36) && (t->r == NULL))
t->r = temp;
else if ((temp->IT36 < t->IT36) && (t->l != NULL))
search(t->l);
else if ((temp->IT36 < t->IT36) && (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);
printf("%d -> ", t->IT36);
if (t->r != NULL)
inorder(t->r);
}

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);
}

void preorder(struct btnode *t)


{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->IT36);
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)

119
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->IT36);
}

void search1(struct btnode *t, int data)


{
if ((data>t->IT36))
{
t1 = t;
search1(t->r, data);
}
else if ((data < t->IT36))
{
t1 = t;
search1(t->l, data);
}
else if ((data==t->IT36))
{
delete1(t);
}
}

void delete1(struct btnode *t)


{
int k;

/* To delete leaf node */


if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else

120
{
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;
}

121
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->IT36 = k;
}

int smallest(struct btnode *t)


{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}

122
else
return (t->IT36);
}

int largest(struct btnode *t)


{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
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 };

void insert(int key);


void display(struct node *root,int);
void DelNode(int x);
void search(int x);
enum KeyStatus ins(struct node *r, int x, int* y, struct node** u);
int searchPos(int x,int *key_arr, int n);
enum KeyStatus del(struct node *r, int x);

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;
}

void insert(int key)


{
struct node *newnode;
int upKey;
enum KeyStatus value;
value = ins(root, key, &upKey, &newnode);
if (value == Duplicate)
printf("Key already available\n");
if (value == InsertIt)
{
struct node *uproot = root;
root=malloc(sizeof(struct node));
root->n = 1;
root->keys[0] = upKey;
root->p[0] = uproot;
root->p[1] = newnode;
}
}

enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct


node
**newnode)
{
struct node *newPtr, *lastPtr;
int pos, i, n,splitPos;
int newKey, lastKey;
enum KeyStatus value;
if (ptr == NULL)
{
*newnode = NULL;
*upKey = key;
return InsertIt;
}
n = ptr->n;
pos = searchPos(key, ptr->keys, n);

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);

for (i=n; 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;
++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()*/

void display(struct node *ptr, int blanks)


{
if (ptr)
{
int i;
for(i=1;i<=blanks;i++)
printf(" ");
for (i=0; i < ptr->n; i++)
printf("%d ",ptr->keys[i]);
printf("\n");
for (i=0; i <= ptr->n; i++)
display(ptr->p[i], blanks+10);
}
}

void search(int key)


{
int pos, i, n;
struct node *ptr = root;

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);
}

int searchPos(int key, int *key_arr, int n)


{
int pos=0;
while (pos < n && key > key_arr[pos])
pos++;
return pos;
}

void DelNode(int key)


{
struct node *uproot;
enum KeyStatus value;
value = del(root,key);
switch (value)
{
case SearchFailure:
printf("Key %d is not available\n",key);
break;

129
case LessKeys:
uproot = root;
root = root->p[0];
free(uproot);
break;
}
}

enum KeyStatus del(struct node *ptr, int key)


{
int pos, i, pivot, n ,min;
int *key_arr;
enum KeyStatus value;
struct node **p,*lptr,*rptr;

if (ptr == NULL)
return SearchFailure;

n=ptr->n;
key_arr = ptr->keys;
p = ptr->p;
min = (M - 1)/2;

pos = searchPos(key, key_arr, n);


if (p[0] == NULL)
{
if (pos == n || key < key_arr[pos])
return SearchFailure;
/*Shift keys and pointers left*/
for (i=pos+1; i < n; i++)
{
key_arr[i-1] = key_arr[i];
p[i] = p[i+1];
}
return --ptr->n >= (ptr==root ? 1 : min) ? Success : LessKeys;
}/*End of if */

if (pos < n && key == key_arr[pos])

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;

if (pos > 0 && p[pos-1]->n > min)


{
pivot = pos - 1; /*pivot for left and right node*/
lptr = p[pivot];
rptr = p[pos];
/*Assigns values for right node*/
rptr->p[rptr->n + 1] = rptr->p[rptr->n];
for (i=rptr->n; i>0; i--)
{
rptr->keys[i] = rptr->keys[i-1];
rptr->p[i] = rptr->p[i-1];
}
rptr->n++;
rptr->keys[0] = key_arr[pivot];
rptr->p[0] = lptr->p[lptr->n];
key_arr[pivot] = lptr->keys[--lptr->n];
return Success;
}
if (pos > min)
{

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

typedef struct record {


int IT36;
} record;

// Node
typedef struct node {
void **pointers;
int *keys;
struct node *parent;
bool is_leaf;
int num_keys;
struct node *next;
} node;

int order = ORDER;


node *queue = NULL;
bool verbose_output = false;

// 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);

record *makeRecord(int IT36);


node *makeNode(void);
node *makeLeaf(void);
int getLeftIndex(node *parent, node *left);
node *insertIntoLeaf(node *leaf, int key, record *pointer);
node *insertIntoLeafAfterSplitting(node *root, node *leaf, int key,
record *pointer);
node *insertIntoNode(node *root, node *parent,
int left_index, int key, node *right);
node *insertIntoNodeAfterSplitting(node *root, node *parent,
int left_index,
int key, node *right);
node *insertIntoParent(node *root, node *left, int key, node
*right);
node *insertIntoNewRoot(node *left, int key, node *right);
node *startNewTree(int key, record *pointer);
node *insert(node *root, int key, int IT36);

// 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;
}

// Print the leaves


void printLeaves(node *const root) {
if (root == NULL) {
printf("Empty tree.\n");
return;
}
int i;
node *c = root;
while (!c->is_leaf)
c = c->pointers[0];
while (true) {
for (i = 0; i < c->num_keys; i++) {
if (verbose_output)
printf("%p ", c->pointers[i]);
printf("%d ", c->keys[i]);
}
if (verbose_output)
printf("%p ", c->pointers[order - 1]);
if (c->pointers[order - 1] != NULL) {
printf(" | ");
c = c->pointers[order - 1];
} else
break;
}
printf("\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;
}

// Get path to root


int pathToLeaves(node *const root, node *child) {
int length = 0;
node *c = child;
while (c != root) {
c = c->parent;
length++;
}
return length;
}

// Print the tree


void printTree(node *const root) {
node *n = NULL;
int i = 0;
int rank = 0;
int new_rank = 0;

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");
}

// Find the node and print it


void findAndPrint(node *const root, int key, bool verbose) {
node *leaf = NULL;
record *r = find(root, key, verbose, NULL);
if (r == NULL)
printf("Record not found under key %d.\n", key);
else
printf("Record at %p -- key %d, IT36 %d.\n",
r, key, r->IT36);
}

// Find and print the range


void findAndPrintRange(node *const root, int key_start, int
key_end,

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);
}
}

// Find the range


int findRange(node *const root, int key_start, int key_end, bool
verbose,
int returned_keys[], void *returned_pointers[]) {
int i, num_found;
num_found = 0;
node *n = findLeaf(root, key_start, verbose);
if (n == NULL)
return 0;
for (i = 0; i < n->num_keys && n->keys[i] < key_start; i++)
;
if (i == n->num_keys)
return 0;
while (n != NULL) {
for (; i < n->num_keys && n->keys[i] <= key_end; i++) {
returned_keys[num_found] = n->keys[i];
returned_pointers[num_found] = n->pointers[i];
num_found++;

140
}
n = n->pointers[order - 1];
i = 0;
}
return num_found;
}

// Find the leaf


node *findLeaf(node *const root, int key, bool verbose) {
if (root == NULL) {
if (verbose)
printf("Empty tree.\n");
return root;
}
int i = 0;
node *c = root;
while (!c->is_leaf) {
if (verbose) {
printf("[");
for (i = 0; i < c->num_keys - 1; i++)
printf("%d ", c->keys[i]);
printf("%d] ", c->keys[i]);
}
i = 0;
while (i < c->num_keys) {
if (key >= c->keys[i])
i++;
else
break;
}
if (verbose)
printf("%d ->\n", i);
c = (node *)c->pointers[i];
}
if (verbose) {
printf("Leaf [");
for (i = 0; i < c->num_keys - 1; i++)
printf("%d ", c->keys[i]);

141
printf("%d] ->\n", c->keys[i]);
}
return c;
}

record *find(node *root, int key, bool verbose, node **leaf_out) {


if (root == NULL) {
if (leaf_out != NULL) {
*leaf_out = NULL;
}
return NULL;
}

int i = 0;
node *leaf = NULL;

leaf = findLeaf(root, key, verbose);

for (i = 0; i < leaf->num_keys; i++)


if (leaf->keys[i] == key)
break;
if (leaf_out != NULL) {
*leaf_out = leaf;
}
if (i == leaf->num_keys)
return NULL;
else
return (record *)leaf->pointers[i];
}

int cut(int length) {


if (length % 2 == 0)
return length / 2;
else
return length / 2 + 1;
}

record *makeRecord(int IT36) {

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
}

int getLeftIndex(node *parent, node *left) {


int left_index = 0;
while (left_index <= parent->num_keys &&
parent->pointers[left_index] != left)
left_index++;
return left_index;
}

node *insertIntoLeaf(node *leaf, int key, record *pointer) {


int i, insertion_point;

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;
}

node*insert(node*root,int key,int IT36){


record*record_pointer=NULL;
node*leaf=NULL;

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>

typedef struct node


{
int IT36;
struct node *left,*right;
int ht;
}node;

node *insert(node *,int);

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;

case 2: printf("\nEnter a IT36:");


scanf("%d",&x);
root=insert(root,x);
break;

case 3: printf("\nEnter a IT36:");


scanf("%d",&x);
root=Delete(root,x);
break;

case 4: printf("\nPreorder sequence:\n");


preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
}while(op!=5);

return 0;
}

node * insert(node *T,int x)


{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->IT36=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->IT36) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)

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);
}

node * Delete(node *T,int x)


{
node *p;

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);

if(BF(T)==2)//Rebalance during windup


if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);\
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}

int height(node *T)

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);
}

node * rotateright(node *x)


{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}

node * rotateleft(node *x)


{
node *y;
y=x->right;
x->right=y->left;
y->left=x;

156
x->ht=height(x);
y->ht=height(y);

return(y);
}

node * RR(node *T)


{
T=rotateleft(T);
return(T);
}

node * LL(node *T)


{
T=rotateright(T);
return(T);
}

node * LR(node *T)


{
T->left=rotateleft(T->left);
T=rotateright(T);

return(T);
}

node * RL(node *T)


{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}

int BF(node *T)


{
int lh,rh;
if(T==NULL)
return(0);

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);
}

void preorder(node *T)


{
if(T!=NULL)
{
printf("%d(Bf=%d)",T->IT36,BF(T));
preorder(T->left);
preorder(T->right);
}
}

void inorder(node *T)


{
if(T!=NULL)
{
inorder(T->left);
printf("%d(Bf=%d)",T->IT36,BF(T));
inorder(T->right);
}
}

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>

void insert(int ary[],int hFn, int size){


int element,IT36,n=0;

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;
}

void delete(int ary[],int hFn,int size){

int element,n=0,IT36;

printf("Enter element to delete\n");


scanf("%d",&element);

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");
}

void search(int ary[],int hFn,int size){


int element,IT36,n=0;

printf("Enter element you want to search\n");


scanf("%d",&element);

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");
}

void display(int ary[],int size){


int i;

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;

printf("Enter size of hash table\n");


scanf("%d",&size);

int ary[size];

printf("Enter hash function [if mod 10 enter 10]\n");


scanf("%d",&hFn);

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

You might also like