DS Lab Manual (2023-2024)
DS Lab Manual (2023-2024)
Laboratory Record
II B.Tech - I Semester
(A.Y. 2023-24)
Roll No : ________________________________________________
VISION & MISSION OF THE INSTITUTE & DEPARTMENT
To empower female students with professional education using creative & innovative technical
practices of global competence and research aptitude to become competitive engineers with
ethical values and entrepreneurial skills.
1. To impart value based professional education through creative and innovative teaching
learning process to face the global challenges of the new era technology.
Debouch as a center of excellence for computer science engineering by imparting social, moral
and ethical values oriented education through advanced pedagogical techniques and produce
technologically and highly competent professionals of global standards with capabilities of
solving challenges of the time through innovative and creative solutions.
M2: To advance the department industry collaborations through interaction with professional
society through seminars/workshops/guest lectures and student internship programs.
M3: To nurture students with leadership qualities, communication skills and imbibe qualities
to work as a team member and a leader for the economic and technological development
in cutting edge technologies in national and global arena.
PEO-1: To provide graduates the foundational and essential knowledge in mathematics, science,
computer science and engineering and interdisciplinary engineering to emerge as
technocrats.
PEO-2: To inculcate the capabilities to analyze, design and develop innovative solutions of
computer support systems for benefits of the society, by diligence and teamwork.
Course Objectives:
● It covers various concepts of C programming language
● It introduces searching and sorting algorithms
● It provides an understanding of data structures such as stacks and queues.
Course Outcomes:
● Ability to develop C programs for computing and real-life applications using basic elements
like control statements, arrays, functions, pointers and strings, and data structures like stacks,
queues and linked lists.
● Ability to Implement searching and sorting algorithms
INSTRUCTIONS TO STUDENTS
All students must observe the Dress Code while in the laboratory.
Sandals or open-toed shoes are NOT allowed.
Foods, drinks and smoking are NOT allowed.
All bags must be left at the indicated place.
The lab timetable must be strictly followed.
Be PUNCTUAL for your laboratory session.
Noise must be kept to a minimum.
Workspace must be kept clean and tidy at all time.
All students are liable for any damage to the accessories due to their own negligence.
All interfacing kits connecting cables must be RETURNED if you take from the lab
supervisor.
Students are strictly PROHIBITED from taking out any items from the laboratory.
Students are NOT allowed to work alone in the laboratory without the Lab Supervisor
Report immediately to the Lab Supervisor if any malfunction of the accessories, is there.
List of Experiments:
1. Write a program that uses functions to perform the following operations on singly
linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
2. Write a program that uses functions to perform the following operations on doubly
linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
3. Write a program that uses functions to perform the following operations on circular
linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
4. Write a program that implement stack (its operations) using
i) Arrays ii) Pointers
5. Write a program that implement Queue (its operations) using
i) Arrays ii) Pointers
6. Write a program that implements the following sorting methods to sort a given list of
integers in ascending order
i) Quick sort ii) Heap sort iii) Merge sort
7. Write a program to implement the tree traversal methods( Recursive and Non Recursive).
8. Write a program to implement
i) Binary Search tree ii) B Trees iii) B+ Trees iv) AVL
trees v) Red - Black trees
9. Write a program to implement the graph traversal methods.
10. Implement a Pattern matching algorithms using Boyer- Moore, Knuth-Morris-Pratt
TEXT BOOKS:
1. Fundamentals of Data Structures in C, 2nd Edition, E. Horowitz, S. Sahni and
Susan Anderson Freed, Universities Press.
2. Data Structures using C – A. S. Tanenbaum, Y. Langsam, and M. J. Augenstein,
PHI/Pearson Education.
REFERENCE BOOK:
1. Data Structures: A Pseudocode Approach with C, 2nd Edition, R. F. Gilberg
and B. A. Forouzan, Cengage Learning
Exp. No Experiment Name Page No
INDEX
EVALUATION SHEET
A-Regularity B – Record submission in-time C – Viva-Voce D - Experimentation
Cycle 1:
Exp A B C D Faculty
Experiment Name Date TOTAL
No 2 3 4 6 (T=A+B+C+D) Sign
Write a program that uses
functions to perform the
1
following operations on singly
linked list i)Creation ii)
Insertion iii) Deletion iv)
Traversal.
Write a program that uses
functions to perform the
following operations on doubly
2
linked list i)Creation ii)
Insertion iii) Deletion iv)
Traversal.
Write a program that uses
functions to perform the
3
following operations on circular
linked List i)Creation ii)
Insertion iii) Deletion iv)
Traversal.
Write a program that implement
4
stack (its operations)
Using i)Arrays ii) Linked
list(Pointers).
Write a program that implement
5
Queue(its operations)
Using i) Arrays ii) Linked
list(Pointers).
First Cycle Average Marks : Faculty Sign with Date
Cycle 2:
1. Write a program that uses functions to perform the following operations on singly linked list.:
i) Creation ii) Insertion iii) Deletion iv)Traversal
#include<stdio.h>
#include<stdlib.h>
struct Node;
struct Node
int e;
Position next;
};
Position TmpCell;
if(TmpCell == NULL)
else
TmpCell->e = x;
TmpCell->next = p->next;
p->next = TmpCell;
int isLast(Position p)
Position p = l;
p = p->next;
return p;
Position p, TmpCell;
p = FindPrevious(x, l);
if(!isLast(p))
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
else
}
void Display(List l)
Position p = l->next;
while(p != NULL)
p = p->next;
int i, n, x, j;
Position p;
scanf("%d",&n);
p = l1;
scanf("%d", &x);
p = p->next;
Display(l1);
p = l;
while(p->next != NULL)
p = p->next;
p->next = l1->next;
Display(l);
int main()
List l, l1;
l->next = NULL;
List p = l;
do
scanf("%d", &ch);
switch(ch)
case 1:
p = l;
scanf("%d",&x);
scanf("%d",&pos);
p = p->next;
Insert(x,l,p);
break;
case 2:
p = l;
scanf("%d",&x);
Delete(x,p);
break;
case 3:
l1->next = NULL;
Merge(l, l1);
break;
case 4:
Display(l);
break;
while(ch<5);
return 0;
OUTPUT:
2. Write a program that uses functions to perform the following operations on doubly linked list:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
voiddelete();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case1:
insert1();
break;
case2:
insert2();
break;
case3:
insert3();
break;
case4:
delete();
break;
case5:
traversebeg();
break;
case6:
temp2 = h;
if (temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case7:
search();
break;
case8:
sort();
break;
case9:
update();
break;
case10:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To delete an element */
voiddelete()
{
int i = 1, pos;
if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
if (temp2 == NULL)
{
printf("\n Error : List empty to search for data");
return;
}
printf("\n Enter value to search : ");
scanf("%d", &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf("\n Data found in %d position",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
}
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
temp2 = h;
temp4 = h;
if (temp2 == NULL)
{
printf("\n List empty to sort");
return;
}
3. write a program that uses functions to perform the following operations on circular linked list
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
void create();
void ins_at_beg();
void ins_at_pos();
void del_at_beg();
void del_at_pos();
void traverse();
void search();
void sort();
void update();
void main()
{
int ch;
printf("\n7.Search\n8.sort\n9.update\n10.Exit\n");
while (1)
scanf("%d", &ch);
switch(ch)
case 1:
create();
break;
case 2:
ins_at_beg();
break;
case 3:
ins_at_pos();
break;
case 4:
del_at_beg();
break;
case 5:
del_at_pos();
break;
case 6:
traverse();
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
rev_traverse(head);
break;
default:
exit(0);
void create()
int c;
scanf("%d", &x->data);
x->link = x;
head = x;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d", &c);
while (c != 0)
scanf("%d", &y->data);
x->link = y;
y->link = head;
x = y;
scanf("%d", &c);
void ins_at_beg()
x = head;
scanf("%d", &y->data);
x = x->link;
x->link = y;
y->link = head;
head = y;
}
void ins_at_pos()
if (head == NULL)
scanf("%d", &y->data);
scanf("%d", &pos);
x = head;
ptr = head;
count++;
ptr = ptr->link;
count++;
printf("OUT OF BOUND");
return;
}
while (c < pos)
z = x;
x = x->link;
c++;
y->link = x;
z->link = y;
void del_at_beg()
if (head == NULL)
else
x = head;
y = head;
x = x->link;
head = y->link;
x->link = head;
free(y);
if (head == NULL)
else
int c = 1, pos;
scanf("%d", &pos);
x = head;
y = x;
x = x->link;
c++;
y->link = x->link;
free(x);
void traverse()
if (head == NULL)
else
x = head;
while (x->link != head)
printf("%d->", x->data);
x = x->link;
printf("%d", x->data);
void search()
scanf("%d", &search_val);
if (head == NULL)
else
x = head;
if (x->data == search_val)
flag = 1;
break;
count++;
x = x->link;
if (x->data == search_val)
if (flag == 0)
void sort()
int temp;
if (head == NULL)
printf("empty linkedlist");
else
ptr = head;
nxt = ptr->link;
while (nxt != head)
if (nxt != head)
temp = ptr->data;
ptr->data = nxt->data;
nxt->data = temp;
else
break;
nxt = nxt->link;
ptr = ptr->link;
void update()
int search_val;
int replace_val;
int flag = 0;
if (head == NULL)
else
scanf("%d", &search_val);
fflush(stdin);
scanf("%d", &replace_val);
ptr = head;
if (ptr->data == search_val)
ptr->data = replace_val;
flag = 1;
break;
ptr = ptr->link;
if (ptr->data == search_val)
ptr->data = replace_val;
flag = 1;
}
if (flag == 1)
printf("\nUPdate sucessful");
else
int i = 0;
if (head == NULL)
else
if (p->link != head)
i = p->data;
rev_traverse(p->link);
if (p->link == head)
{
printf(" %d", p->data);
output
1.Creation
2.Insertion at beginning
3.Insertion at remaining
4.Deletion at beginning
5.Deletion at remaining
6.traverse
7.Search
8.sort
9.update
10.Exit
Enter your choice:6
List is empty
Enter your choice:5
List is empty
Enter your choice:9
empty list
Enter your choice:7
enter the element to search
12
List is empty nothing to search
Enter your choice:1
Enter the data:10
If you wish to continue press 1 otherwise 0:0
Enter your choice:3
Enter the data:20
Enter the position to be inserted:5
OUT OF BOUND
Enter your choice:2
Enter the data:12
Enter your choice:6
12->10
Enter your choice:3
Enter the data:13
Enter the position to be inserted:3
Enter your choice:3
Enter the data:14
Enter the position to be inserted:4
Enter your choice:6
12->10->13->14
Enter your choice:3
Enter the data:24
Enter the position to be inserted:4
Enter your choice:6
12->10->13->24->14
Enter your choice:3
Enter the data:10
Enter the position to be inserted:100
OUT OF BOUND
Enter your choice:4
Enter your choice:6
10->13->24->14
Enter your choice:5
Enter the position to be deleted:4
Enter your choice:6
10->13->24
Enter your choice:5
Enter the position to be deleted:2
Enter your choice:6
10->24
Enter your choice:9
enter the value to be edited
23
enter the value to be replace
24
update not successful
Enter your choice:9
enter the value to be edited
24
enter the value to be replace
26
UPdate sucessful
Enter your choice:6
10->26
Enter your choice:7
enter the element to search
26
element found at postion 1
element not found
Enter your choice:7
enter the element to search
27
element not found
Enter your choice:8
Enter your choice:6
10->26
Enter your choice:10
26 10
Enter your choice:11
4. write a program that implements stack using
i. arrays ii. Pointers
i. arrays
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
//clrscr();
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
}
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
}
}
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
OUTPUT:
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
98
24
12
12
EXIT POINT
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 3
void push(int i);
void pop(void);
int choice, i;
int *tos, *p1, arr_stack[MAX_SIZE];
int exit_p = 1;
int main()
{
int value;
tos = arr_stack; /* tos points to the top of stack */
p1 = arr_stack; /* initialize p1 */
printf("\n Simple Stack Example - Pointers");
do {
printf("\nStack Pointer : Main Menu");
printf("\n1.Push \t2.Pop \tOthers to exit : Your Choice : ");
scanf("%d", &choice);
switch (choice) {
case1:
printf("Enter value: ");
scanf("%d", &value);
push(value);
break;
case2:
pop();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);
return0;
}
void push(int i) {
if (p1 == (tos + MAX_SIZE)) {
printf("\nStatus : Stack Overflow.\n");
} else {
*p1 = i;
printf("\nPush Value : %d ", *(p1));
p1++;
}
}
void pop(void) {
if (p1 == tos) {
printf("\nStatus : Stack Underflow.\n");
//return 0;
} else {
p1--;
printf("\nPop Value : %d ", *(p1));
}
}
OUTPUT
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
OUTPUT
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3
Queue Elements are:10
54
98
234
Enter the Choice:2
Deleted Element is 10
Enter the Choice:3
Queue Elements are:
54
98
234
Enter the Choice:4
ii.Pointers
#include<stdio.h>
#include<stdlib.h>
#define max 10
int insertq ( int queue[max], int *rear , int *data)
{
if ( *rear == max -1 )
return(-1);
else
{
*rear = *rear + 1;
queue[*rear] = *data;
return(1);
}
}
int delq( int queue[max], int *front, int *rear , int *data)
{
if ( *front == *rear)
return(-1);
else
{
(*front)++;
*data = queue[*front];
return(1);
}
}
int main()
{
int queue[max],data;
int front,rear,reply,option;
//... init queue
front = rear = -1;
printf("\tMenu");
printf("\n----------------------------");
printf("\n 1. Insert element in queue");
printf("\n 2. Delete element from queue");
printf("\n 3. Exit");
printf("\n----------------------------");
while(1)
{
printf("\nChoose operation : ");
scanf("%d",&option);
switch(option)
{
case1 ://insert
printf("\nEnter Number : ");
scanf("%d",&data);
reply = insertq(queue,&rear,&data);
if ( reply == - 1)
printf("Queue is full");
else
printf("%d is inserted in queue.\n",data);
break;
case2 : //dele
reply = delq(queue,&front,&rear,&data);
if ( reply == -1 )
printf("Queue is empty ");
else
printf("\nDeleted number is : %d\n", data);
break;
case3 : exit(0);
}
}
}
OUTPUT
Menu
----------------------------
1. Insert element in queue
2. Delete element from queue
3. Exit
----------------------------
Choose operation : 1
Enter Number : 2
2 is inserted in queue.
Choose operation : 1
Enter Number : 34
34 is inserted in queue.
Choose operation : 2
Deleted number is : 2
Choose operation : 3
int main() {
int arr[] = { 12, 17, 6, 25, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
Sorted array:
1 5 6 12 17 25
iii)Merge sort
#include <stdio.h>
#include <stdlib.h>
// merge function
void Merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int size1 = mid - left + 1;
int size2 = right - mid;
// driver code
int main()
{
int size;
printf("Enter the size: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
Merge_Sort(arr, 0, size - 1);
#include <stdlib.h>
struct node {
int data;
};
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1)
parent = current;
current = current->leftChild;
if(current == NULL) {
parent->leftChild = tempNode;
return;
else {
current = current->rightChild;
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
current = current->leftChild;
else {
current = current->rightChild;
//not found
if(current == NULL) {
return NULL;
return current;
if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}
}
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
int main() {
int i;
insert(array[i]);
i = 31;
if(temp != NULL) {
printf("\n");
}else {
}
i = 15;
temp = search(i);
if(temp != NULL) {
printf("\n");
}else {
pre_order_traversal(root);
inorder_traversal(root);
post_order_traversal(root);
return 0;
Output
Preorder traversal: 27 14 10 19 35 31 42
Inorder traversal: 10 14 19 27 31 35 42
#include <stdlib.h>
struct btnode
int value;
void delete1();
void insert();
void delete();
void create();
int flag = 1;
void main()
int ch;
printf("\nOPERATIONS ---");
printf("6 - Exit\n");
while(1)
scanf("%d", &ch);
switch (ch)
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 :
break;
void insert()
create();
if (root == NULL)
root = temp;
else
search(root);
/* To create a node */
void create()
int data;
scanf("%d", &data);
temp->value = data;
}
/* Function to search the appropriate position to insert the new node */
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
t->l = temp;
if (root == NULL)
return;
if (t->l != NULL)
inorder(t->l);
if (t->r != NULL)
inorder(t->r);
}
/* To check for the deleted node */
void delete()
int data;
if (root == NULL)
return;
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
if (root == NULL)
return;
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
if (root == NULL)
return;
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
if ((data>t->value))
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
t1 = t;
search1(t->l, data);
else if ((data==t->value))
delete1(t);
/* To delete a node */
int k;
if (t1->l == t)
t1->l = NULL;
else
t1->r = NULL;
t = NULL;
free(t);
return;
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;
if (t1 == t)
root = t->r;
t1 = root;
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
t2 = root;
if (t->r != NULL)
k = smallest(t->r);
flag = 1;
else
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
t2 = t;
if (t->l != NULL)
t2 = t;
return(smallest(t->l));
else
return (t->value);
if (t->r != NULL)
t2 = t;
return(largest(t->r));
}
else
return(t->value);
$ cc tree43.c
$ a.out
OPERATIONS ---
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal
6 - Exit
40
/\
/ \
20 60
/\ \
10 30 80
90
ii)B Trees
// Searching a key on a B-tree in C
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
#define MIN 2
struct BTreeNode {
int val[MAX + 1], count;
struct BTreeNode *link[MAX + 1];
};
// Create a node
struct BTreeNode *createNode(int val, struct BTreeNode *child) {
struct BTreeNode *newNode;
newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode));
newNode->val[1] = val;
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
}
// Insert node
void insertNode(int val, int pos, struct BTreeNode *node,
struct BTreeNode *child) {
int j = node->count;
while (j > pos) {
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
}
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
}
// Split node
void splitNode(int val, int *pval, int pos, struct BTreeNode *node,
struct BTreeNode *child, struct BTreeNode **newNode) {
int median, j;
// Search node
void search(int val, int *pos, struct BTreeNode *myNode) {
if (!myNode) {
return;
}
return;
}
int main() {
int val, ch;
insert(8);
insert(9);
insert(10);
insert(11);
insert(15);
insert(16);
insert(17);
insert(18);
insert(20);
insert(23);
traversal(root);
printf("\n");
search(11, &ch, root);
}
Output:
8 9 10 11 15 16 17 18 20 23
11 is found
ii)B+tree
// Searching on a B+ Tree in C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#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[]);
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;
}
}
// 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;
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;
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");
}
int i = 0;
node *leaf = NULL;
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;
}
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;
}
insertion_point = 0;
while (insertion_point < leaf->num_keys && leaf->keys[insertion_point] < key)
insertion_point++;
new_leaf = makeLeaf();
insertion_index = 0;
while (insertion_index < order - 1 && leaf->keys[insertion_index] < key)
insertion_index++;
temp_keys[insertion_index] = key;
temp_pointers[insertion_index] = pointer;
leaf->num_keys = 0;
free(temp_pointers);
free(temp_keys);
new_leaf->parent = leaf->parent;
new_key = new_leaf->keys[0];
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];
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;
}
if (parent == NULL)
return insertIntoNewRoot(left, key, right);
record_pointer = makeRecord(value);
if (root == NULL)
return startNewTree(key, record_pointer);
int main() {
node *root;
char instruction;
root = NULL;
struct Node
{
int key;
struct Node *left;
struct Node *right;
int height;
};
int getHeight(struct Node *n){
if(n==NULL)
return 0;
return n->height;
}
x->right = y;
y->left = T2;
return x;
}
y->left = x;
x->right = T2;
int main(){
struct Node * root = NULL;
#include <stdio.h>
#include <stdlib.h>
enum nodeColor {
RED,
BLACK
};
struct rbNode {
int data, color;
struct rbNode *link[2];
};
// Insert an node
void insertion(int data) {
struct rbNode *stack[98], *ptr, *newnode, *xPtr, *yPtr;
int dir[98], ht = 0, index;
ptr = root;
if (!root) {
root = createNode(data);
return;
}
stack[ht] = root;
dir[ht++] = 0;
while (ptr != NULL) {
if (ptr->data == data) {
printf("Duplicates Not Allowed!!\n");
return;
}
index = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
ptr = ptr->link[index];
dir[ht++] = index;
}
stack[ht - 1]->link[index] = newnode = createNode(data);
while ((ht >= 3) && (stack[ht - 1]->color == RED)) {
if (dir[ht - 2] == 0) {
yPtr = stack[ht - 2]->link[1];
if (yPtr != NULL && yPtr->color == RED) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 0) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
yPtr = xPtr->link[1];
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
stack[ht - 2]->link[0] = yPtr;
}
xPtr = stack[ht - 2];
xPtr->color = RED;
yPtr->color = BLACK;
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
} else {
yPtr = stack[ht - 2]->link[0];
if ((yPtr != NULL) && (yPtr->color == RED)) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 1) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
yPtr = xPtr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
stack[ht - 2]->link[1] = yPtr;
}
xPtr = stack[ht - 2];
yPtr->color = BLACK;
xPtr->color = RED;
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
}
}
root->color = BLACK;
}
// Delete a node
void deletion(int data) {
struct rbNode *stack[98], *ptr, *xPtr, *yPtr;
struct rbNode *pPtr, *qPtr, *rPtr;
int dir[98], ht = 0, diff, i;
enum nodeColor color;
if (!root) {
printf("Tree not available\n");
return;
}
ptr = root;
while (ptr != NULL) {
if ((data - ptr->data) == 0)
break;
diff = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
dir[ht++] = diff;
ptr = ptr->link[diff];
}
if (ptr->link[1] == NULL) {
if ((ptr == root) && (ptr->link[0] == NULL)) {
free(ptr);
root = NULL;
} else if (ptr == root) {
root = ptr->link[0];
free(ptr);
} else {
stack[ht - 1]->link[dir[ht - 1]] = ptr->link[0];
}
} else {
xPtr = ptr->link[1];
if (xPtr->link[0] == NULL) {
xPtr->link[0] = ptr->link[0];
color = xPtr->color;
xPtr->color = ptr->color;
ptr->color = color;
if (ptr == root) {
root = xPtr;
} else {
stack[ht - 1]->link[dir[ht - 1]] = xPtr;
}
dir[ht] = 1;
stack[ht++] = xPtr;
} else {
i = ht++;
while (1) {
dir[ht] = 0;
stack[ht++] = xPtr;
yPtr = xPtr->link[0];
if (!yPtr->link[0])
break;
xPtr = yPtr;
}
dir[i] = 1;
stack[i] = yPtr;
if (i > 0)
stack[i - 1]->link[dir[i - 1]] = yPtr;
yPtr->link[0] = ptr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = ptr->link[1];
if (ptr == root) {
root = yPtr;
}
color = yPtr->color;
yPtr->color = ptr->color;
ptr->color = color;
}
}
if (ht < 1)
return;
if (ptr->color == BLACK) {
while (1) {
pPtr = stack[ht - 1]->link[dir[ht - 1]];
if (pPtr && pPtr->color == RED) {
pPtr->color = BLACK;
break;
}
if (ht < 2)
break;
if (dir[ht - 2] == 0) {
rPtr = stack[ht - 1]->link[1];
if (!rPtr)
break;
if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;
stack[ht - 1]->link[1] = rPtr->link[0];
rPtr->link[0] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 0;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;
rPtr = stack[ht - 1]->link[1];
}
if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;
stack[ht - 1]->link[0] = rPtr->link[1];
rPtr->link[1] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 1;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;
rPtr = stack[ht - 1]->link[0];
}
if ((!rPtr->link[0] || rPtr->link[0]->color == BLACK) &&
(!rPtr->link[1] || rPtr->link[1]->color == BLACK)) {
rPtr->color = RED;
} else {
if (!rPtr->link[0] || rPtr->link[0]->color == BLACK) {
qPtr = rPtr->link[1];
rPtr->color = RED;
qPtr->color = BLACK;
rPtr->link[1] = qPtr->link[0];
qPtr->link[0] = rPtr;
rPtr = stack[ht - 1]->link[0] = qPtr;
}
rPtr->color = stack[ht - 1]->color;
stack[ht - 1]->color = BLACK;
rPtr->link[0]->color = BLACK;
stack[ht - 1]->link[0] = rPtr->link[1];
rPtr->link[1] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
break;
}
}
ht--;
}
}
}
// Driver code
int main() {
int ch, data;
while (1) {
printf("1. Insertion\t2. Deletion\n");
printf("3. Traverse\t4. Exit");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to insert:");
scanf("%d", &data);
insertion(data);
break;
case 2:
printf("Enter the element to delete:");
scanf("%d", &data);
deletion(data);
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Not available\n");
break;
}
printf("\n");
}
return 0;
}
OUTPUT:
1. Insertion 2. Deletion
3. Traverse 4. Exit
Enter your choice:1
Enter the element to insert:22
1. Insertion 2. Deletion
3. Traverse 4. Exit
Enter your choice:1
Enter the element to insert:33
1. Insertion 2. Deletion
3. Traverse 4. Exit
Enter your choice:3
22 33
1. Insertion 2. Deletion
3. Traverse 4. Exit
Enter your choice:
9. Write a program to implement the Graph Traversal methods
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();
int main()
{
create_graph();
BF_Traversal();
return0;
}
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS: \n");
scanf("%d", &v);
BFS(v);
}
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
int isEmpty_queue()
{
if(front == -1 || front > rear)
return1;
else
return0;
}
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
#include<stdio.h>
#include<stdlib.h>
typedefstruct node
{
struct node *next;
int vertex;
}node;
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
OUTPUT
0
1
5
7
2
3
6
4
10.Implement a Pattern matching algorithms using Boyer- Moore, Knuth-Morris-
Pratt
# include <limits.h>
# include <string.h>
# include <stdio.h>
return (a > b) ? a : b;
int i;
badchar[i] = -1;
badchar[(int) str[i]] = i;
int n = strlen(txt);
int badchar[NO_OF_CHARS];
badCharHeuristic(pat, m, badchar);
int j = m - 1;
j--;
if (j < 0) {
else
}
}
int main() {
search(txt, pat);
return 0;
Output:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int M = strlen(pat);
int N = strlen(txt);
// create lps[] that will hold the longest prefix suffix values for pattern
computeLPSArray(pat, M, lps);
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
if (j == M) {
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
int i;
i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
if (len != 0) {
} else // if (len == 0)
lps[i] = 0;
i++;
int main() {
KMPSearch(pat, txt);
return 0;
OUTPUT: