Ds Lab Programs
Ds Lab Programs
Write a C program that uses functions to perform the following on Singly Linked List: i)
Creation ii) Insertion iii) Deletion iv) Traversal
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*newn,*trav,*temp;
void create_list()
{
int value;
temp=head;
newn=(struct node *)malloc(sizeof (struct node));
printf("\nenter the value to be inserted");
scanf("%d",&value);
newn->data=value;
if(head==NULL)
{
head=newn;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->next=NULL;
}
}
void insert_at_begning(int value)
{
newn=(struct node*)malloc(sizeof(struct node));
newn->data=value;
if(head==NULL)
{
head=newn;
head->next=NULL;
}
else
{
newn->next=head;
head=newn;
}
}
void insert_at_end(int value)
{
temp=head;
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=newn;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->next=NULL;
}
}
void insert_at_middle()
{
int loc,value;
printf("\n after which value you want to insert : ");
scanf("%d",&loc);
printf("\nenter the value to be inserted");
scanf("%d",&value);
newn=(struct node*)malloc(sizeof(struct node));
newn->data=value;
temp=head;
if(head==NULL)
{
head=newn;
head->next=NULL;
}
else
{
while(temp->data!=loc)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next=newn;
}
}
void delete_from_middle()
{
struct node *var;
int value;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",value);
}
else
{
while(temp->data!=value)
{
var=temp;
temp=temp->next;
}
var->next=temp->next;
temp->next=NULL;
free(temp);
}
}
void delete_from_front()
{
temp=head;
if(head==NULL)
{
printf("\nno elements for deletion in the list\n");
}
else
{
head=temp->next;
temp->next=NULL;
free(temp);
}
}
void delete_from_end()
{
struct node *var;
temp=head;
if(head==NULL)
{
printf("\nno elemts in the list");
}
else
{
while(temp->next!=NULL)
{
var=temp;
temp=temp->next;
}
var->next=NULL;
free(temp);
}
}
void display()
{
temp=head;
if(temp==NULL)
{
printf("\nList is Empty\n");
}
else
{
while(temp!=NULL)
{
printf(" -> %d ",temp->data);
temp=temp->next;
}
printf("\n");
}
}
void main()
{
int ch=0;
char ch1;
head=NULL;
printf("\[Link] linked list");
printf("\[Link] at begning of linked list");
printf("\[Link] at the end of linked list");
printf("\[Link] at the middle where you want");
printf("\[Link] from the front of linked list");
printf("\[Link] from the end of linked list ");
printf("\[Link] of the middle data that you want");
printf("\[Link] the linked list");
printf("\[Link]\n");
while(1)
{
printf("\nenter the choice of operation to perform on linked list");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
do{
create_list();
display();
printf("do you want to create list ,y / n");
getchar();
scanf("%c",&ch1);
}while(ch1=='y'||ch1=='Y');
break;
}
case 2:
{
int value;
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begning(value);
display();
break;
}
case 3:
{
int value;
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value);
display();
break;
}
case 4:
{
insert_at_middle();
display();
break;
}
case 5:
{
delete_from_front();
display();
break;
}
case 6:
{
delete_from_end();
display();
break;
}
case 7:
{
display();
delete_from_middle();
display();
break;
}
case 8:
{
display();
break;
}
case 9:
{
exit(1);
}
default:printf("\n****Please enter correct choice****\n");
}
getch();
}
}
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<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next,*prev;
}*head,*newn,*trav,*temp;
void create_list()
{
int value;
temp=head;
newn=(struct node*)malloc(sizeof(struct node));
printf("\n enter value");
scanf("%d",&value);
newn->data=value;
if(head==NULL)
{
head=newn;
newn->prev=NULL;
newn->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
newn->next=NULL;
newn->prev=temp;
temp->next=newn;
temp=newn;
}
}
void insert_at_begning(int value)
{
newn=(struct node*)malloc(sizeof(struct node));
newn->data=value;
if(head==NULL)
{
head=newn;
head->prev=NULL;
head->next=NULL;
}
else
{
newn->next=head;
head->prev=newn;
newn->prev=NULL;
head=newn;
}
}
void insert_at_end(int value)
{
temp=head;
newn=(struct node*)malloc(sizeof(struct node));
newn->data=value;
if(head==NULL)
{
head=newn;
head->prev=NULL;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
newn->next=NULL;
newn->prev=temp;
temp->next=newn;
temp=newn;
}
}
void insert_at_middle()
{
struct node *var;
int loc,value;
printf("\nselect location where you want to insert the data");
scanf("%d",&loc);
printf("\nenter which value do u want to inserted");
scanf("%d",&value);
newn=(struct node*)malloc(sizeof(struct node));
newn->data=value;
temp=head;
if(temp==NULL)
{
printf("\n the list is empty");
}
else
{
while(temp->data!=loc)
{
temp=temp->next;
var=temp->next;
}
temp->next=newn;
newn->prev=temp;
newn->next=var;
var->prev=newn;
}
}
void delete_from_middle()
{
int loc;
struct node *var;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&loc);
if(head==NULL)
{
printf("The list is empty\n");
}
else
{
while(temp->data!=loc)
{
var=temp;
temp=temp->next;
}
var->next=temp->next;
temp->next->prev=var;
temp->prev=NULL;
temp->next=NULL;
free(temp);
}
}
void delete_from_front()
{
struct node *var;
temp=head;
if(head==NULL)
{
printf("no elements for deletion in the list");
}
else
{
var=temp->next;
head=temp->next;
temp->next=NULL;
temp->prev=NULL;
var->prev=NULL;
free(temp);
}
}
void delete_from_end()
{
struct node *var;
temp=head;
if(head==NULL)
{
printf("no elemts in the list");
}
else
{
while(temp->next!=NULL)
{
var=temp;
temp=temp->next;
}
var->next=NULL;
temp->prev=NULL;
free(temp);
}
}
void display()
{
trav=head;
if(trav==NULL)
{
printf("\nList is Empty");
}
else
{
while(trav!=NULL)
{
printf("%d<--> ",trav->data);
trav=trav->next;
}
printf("\n");
}
}
void main()
{
int ch=0;
char ch1;
clrscr();
head=NULL;
printf("\n Double Linked List Operations");
printf("\[Link] Double Linked List");
printf("\[Link] at begning of linked list");
printf("\[Link] at the end of linked list");
printf("\[Link] at the middle where you want");
printf("\[Link] from the front of linked list");
printf("\[Link] from the end of linked list ");
printf("\[Link] of the middle data that you want");
printf("\[Link]");
printf("\[Link]\n");
while(1)
{
printf("\nenter the choice of operation to perform on linked list");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
do{
create_list();
display();
printf("do you want to create list ,y / n");
getchar();
scanf("%c",&ch1);
}while(ch1=='y'||ch1=='Y');
break;
}
case 2:
{
int value;
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begning(value);
display();
break;
}
case 3:
{
int value;
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value);
display();
break;
}
case 4:
{
insert_at_middle();
display();
break;
}
case 5:
{
delete_from_front();
display();
break;
}
case 6:
{
delete_from_end();
display();
break;
}
case 7:
{
display();
delete_from_middle();
display();
break;
}
case 8:
{
display();
break;
}
case 9:
{
exit(0);
}
}
}
getch();
}
Output :
3. Write a program that uses functions to perform the following operations on circular
linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void beginsert ();
void lastinsert ();
void insertatspecified();
void begin_delete();
void last_delete();
void delete_from_middle();
void display();
void main ()
{
int choice =0;
clrscr();
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\[Link] in begining\[Link] at last\n [Link] at specified location \n [Link] from
Beginning\n [Link] from last\n6.random_delete\[Link]\[Link]\n");
while(1)
{
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
display();
break;
case 2:
lastinsert();
display();
break;
case 3:
insertatspecified();
display();
break;
case 4:
begin_delete();
display();
break;
case 5:
last_delete();
display();
break;
case 6:
delete_from_middle();
display();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *newn,*temp;
int value;
temp = head;
newn = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the node data?");
scanf("%d",&value);
newn -> data = value;
if(head == NULL)
{
head = newn;
newn -> next = head;
}
else
{
while(temp->next != head)
{
temp = temp->next;
}
newn->next = head;
temp -> next = newn;
head = newn;
}
printf("\nnode inserted\n");
}
void lastinsert()
{
struct node *newn,*temp;
int value;
temp=head;
newn = (struct node *)malloc(sizeof(struct node));
printf("\nEnter Data?");
scanf("%d",&value);
newn->data = value;
if(head == NULL)
{
head = newn;
newn -> next = head;
}
else
{
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = newn;
newn -> next = head;
temp=newn;
}
printf("\nnode inserted\n");
}
void insertatspecified()
{
struct node *newn,*temp,*var;
int value,loc;
temp=head;
newn = (struct node *)malloc(sizeof(struct node));
printf("enter the specified location value");
scanf("%d",&loc);
printf("\nEnter Data?");
scanf("%d",&value);
newn->data = value;
if(head == NULL)
{
head = newn;
newn -> next = head;
}
else
{
while(temp->data!=loc)
{
var=temp;
temp=temp->next;
}
newn->next=var->next;
var->next=newn;
}
printf("node inserted");
}
void begin_delete()
{
struct node *temp,*var;
temp=var=head;
if(temp == NULL)
{
printf("\nLinked list is empty");
}
else
{
while(var->next != head)
{
var=var->next;
}
head=temp->next;
temp->next=NULL;
var->next=head;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *temp,*var;
temp=head;
if(temp==NULL)
{
printf("\nUNDERFLOW");
}
else
{
while(temp->next!=head)
{
var=temp;
temp=temp->next;
}
var->next = temp -> next;
temp->next=NULL;
free(temp);
printf("\nnode deleted\n");
}
}
void delete_from_middle()
{
struct node *temp,*var;
int value;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",value);
}
else
{
while(temp->data!=value)
{
var=temp;
temp=temp->next;
}
var->next=temp->next;
temp->next=NULL;
}
printf("\ndata deleted from list is %d",value);
free(temp);
}
void display()
{
struct node *trav;
trav=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
while(trav -> next != head)
{
printf("%d\n", trav -> data);
trav = trav -> next;
}
printf("%d\n", trav -> data);
}
}
Output :
4. i)Write a program that implement stack (its operations) using Arrays
#define MAX 4 //you can take any number to limit your stack size
#include<stdio.h>
#include<conio.h>
int stack[MAX];
int top;
void push()
{
char a;
int value;
if(top==MAX-1)
{
printf("Stack full or stack overflow");
return;
}
do
{
printf("\n Enter the value to be inserted:");
scanf("%d",&value);
top=top+1;
stack[top]=value;
printf("do you want to continue insertion Y/N");
getchar();
scanf("%c",&a);
}while(a=='y'||a=='Y');
}
void pop()
{
int t;
if(top==-1)
{
printf("Stack empty or stack overflow");
}
t=stack[top];
top=top-1;
printf("The deleted element id :%d",t);
}
void show()
{
int i;
printf("\nThe Stack elements are:");
for(i=top;i>=0;i--)
{
printf("%d",stack[i]);
}
}
void main()
{
char ch;
int choice,value;
top=-1;
clrscr();
printf("[Link]");
printf("\[Link]");
printf("\[Link] or display");
printf("\n [Link]");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
show();
break;
}
case 2:
{
pop();
show();
break;
}
case 3:
{
show();
break;
}
case 4:
{
exit(1);
}
default:printf("Wrong choice");
}
printf("\nDo you want to continue(y/n):");
getchar();
scanf("%c",&ch);
}
while(ch=='y'||ch=='Y');
getch();
}
Output:
ii) Write a program that implement stack (its operations) using Pointers
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*top;
void insert()
{
int value;
if (rear==MAX-1)
{
printf("Queue Overflow \n");
}
else
{
if (front == - 1)
{
front=front+1;
}
printf("Inset the element in queue : ");
scanf("%d", &value);
rear=rear+1;
queue[rear] =value;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
ii) Write a program that implement Queue (its operations) using Pointers
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
struct queue
{
int data;
struct queue *next;
}*front=NULL,*rear=NULL;
void add();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("[Link] an element in Queue\n");
printf("[Link] an element from Queue\n");
printf("[Link] the Queue\n");
printf("[Link]\n");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:add();
display();
break;
case 2:del();
display();
break;
case 3:display();
getch();
break;
case 4:exit(0);
break;
default:printf("\nYou entered wrong choice");
}
}
getch();
}
void add()
{
struct queue *newn;
int value;
newn=(struct queue*)malloc(sizeof(struct queue));
printf("\nEnter the element:");
scanf("%d",&value);
newn->data=value;
newn->next=NULL;
if(front==NULL&&rear==NULL)
{
rear=front=newn;
}
else
{
rear->next=newn;
rear=newn;
}
}
void del()
{
struct queue *temp;
temp=front;
if(front==NULL)
{
printf("\nQueue is Empty");
}
else
{
printf("deleted data %d\n",front->data);
front=front->next;
free(temp);
}
}
void display()
{
struct queue *temp;
if(front==NULL)
{
printf("queue is empty");
}
else
{
temp=front;
while(temp!=NULL)
{
printf("->%d",temp->data);
temp=temp->next;
}
}
}
Output :
6. Write a program that implements the following sorting methods to sort a given list of Integers in
a ascending order
i) Quick sort :
#include<stdio.h>
#include<conio.h>
{
//declaaring index variables
pivotIndex = firstIndex;
index1 = firstIndex;
index2 = lastIndex;
index1++;
while(array[index2]>array[pivotIndex])
index2--;
if(index1<index2)
//Swapping opertation
temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
//At the end of first iteration, swap pivot element with index2 element
temp = array[pivotIndex];
array[pivotIndex] = array[index2];
array[index2] = temp;
void main()
//Declaring variables
int array[100],n,i;
scanf("%d",&n);
scanf("%d",&array[i]);
quicksort(array,0,n-1);
for(i=0;i<n;i++)
printf(" %d",array[i]);
getch();
Output :
ii) Heap sort:
#include <stdio.h>
int temp,maximum,left_index,right_index;
maximum = i;
right_index=2*i+2;
left_index=2*i+1;
if(left_index<n&&arr[left_index]>arr[maximum])
maximum=left_index;
if(right_index<n&&arr[right_index]>arr[maximum])
maximum=right_index;
if(maximum!=i)
temp=arr[i];
arr[i]=arr[maximum];
arr[maximum]=temp;
heapify(arr,n,maximum);
int i,temp;
for (i=n/2-1;i>=0;i--)
heapify(arr,n,i);
for (i=n-1;i>0;i--)
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr,i,0);
void main()
int arr[20],n,i;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
}
printf("\n");
heapsort(arr, n);
for(i=0;i<n;i++)
printf("%d\t", arr[i]);
getch();
Output:
III)Merge sort:
#include <stdio.h>
#define max 10
int a[10]={76,32,21,12,45,26,57,54,24,48};
int b[10];
int l1,l2,i;
l1=low;
l2=mid+1;
for(i=low;l1<=mid&&l2<=high;i++)
if(a[l1]<=a[l2])
b[i]=a[l1++];
else
b[i]=a[l2++];
while(l1<=mid)
b[i++]=a[l1++];
b[i++]=a[l2++];
for(i=low;i<=high;i++)
a[i]=b[i];
int mid;
if(low<high)
mid=(low+high)/2;
sort(low,mid);
sort(mid+1,high);
merging(low,mid,high);
void main()
int i;
clrscr();
printf("Enter the elements \n");
for(i=0;i<max;i++)
printf("%d\n",a[i]);
sort(0,max);
for(i=0;i<max;i++)
printf("%d\t",a[i]);
getch();
Output:
7. Write a program to implement the tree traversal methods (Recursive and Non Recursive) ?
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
};
// Inorder traversal
inorderTraversal(root->left);
inorderTraversal(root->right);
// preorderTraversal traversal
preorderTraversal(root->left);
preorderTraversal(root->right);
// postorderTraversal traversal
postorderTraversal(root->left);
postorderTraversal(root->right);
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
root->left = createNode(value);
return root->left;
root->right = createNode(value);
return root->right;
void main() {
clrscr();
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
inorderTraversal(root);
preorderTraversal(root);
postorderTraversal(root);
getch();
Output:
8) Write a program to implement
i) Binary Search tree ii) B Trees iii) B+ Trees iv) AVL trees v) Red - Black trees
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
temp->data = x;
temp->left_child=NULL;
temp->right_child=NULL;
return temp;
if(root==NULL||root->data==x)
return root;
else if(x>root->data)
return search(root->right_child,x);
else
return search(root->left_child,x);
if (root == NULL)
return new_node(x);
else if(x>root->data)
root->right_child=insert(root->right_child,x);
else
root->left_child=insert(root->left_child,x);
return root;
if (root == NULL)
return NULL;
else if(root->left_child!=NULL)
return find_minimum(root->left_child);
return root;
if(root==NULL)
return NULL;
if(x>root->data)
root->right_child=delete(root->right_child, x);
else if(x<root->data)
root->left_child=delete(root->left_child,x);
else {
if(root->left_child==NULL&&root->right_child==NULL){
free(root);
return NULL;
else if(root->left_child==NULL||root->right_child==NULL){
if (root->left_child==NULL)
temp=root->right_child;
else
temp=root->left_child;
free(root);
return temp;
else {
root->data=temp->data;
root->right_child=delete(root->right_child,temp->data);
return root;
}
void inorder(struct node *root){
if (root!=NULL)
inorder(root->left_child);
inorder(root->right_child);
void main()
root=new_node(20);
clrscr();
insert(root,5);
insert(root,1);
insert(root,15);
insert(root,9);
insert(root,7);
insert(root,12);
insert(root,30);
insert(root,25);
insert(root,40);
insert(root,45);
insert(root,42);
search(root,7);
inorder(root);
printf("\n");
root=delete(root,1);
root=delete(root,40);
root=delete(root,45);
root=delete(root,9);
inorder(root);
printf("\n");
getch();
Output :
ii) B Trees:
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
#define MIN 2
struct btree_node
int data_item[MAX+1],counter;
new_node->data_item[1]=data_item;
new_node->counter=1;
new_node->the_link[0]=root_node;
new_node->the_link[1]=child_node;
return new_node;
int j=the_node->counter;
while(j>position) {
the_node->data_item[j+1]=the_node->data_item[j];
the_node->the_link[j+1]=the_node->the_link[j];
j--;
the_node->data_item[j+1]=data_item;
the_node->the_link[j+1]=child_node;
the_node->counter++;
void splitNode(int data_item, int *p_value, int position, struct btree_node *the_node,
int median_key,j;
if(position>MIN)
median_key=MIN+1;
else
median_key=MIN;
j = median_key+1;
while(j<=MAX) {
(*new_node)->data_item[j-median_key]=the_node->data_item[j];
(*new_node)->the_link[j-median_key]=the_node->the_link[j];
j++;
the_node->counter=median_key;
(*new_node)->counter=MAX-median_key;
if(position<=MIN) {
insert_value(data_item,position,the_node,child_node);
} else {
insert_value(data_item,position-median_key,*new_node,child_node);
*p_value=the_node->data_item[the_node->counter];
(*new_node)->the_link[0]=the_node ->the_link[the_node->counter];
int position;
if(!the_node) {
*p_value = data_item;
*child_node = NULL;
return 1;
if(data_item<the_node ->data_item[1]) {
position = 0;
} else {
return 0;
} else {
return 1;
return 0;
int the_flag, i;
if (the_flag)
while (j > 0) {
x ->data_item[j + 1] = x ->data_item[j];
x ->the_link[j + 1] = x ->the_link[j];
x ->data_item[1] = my_node->data_item[position];
x ->the_link[1] = x->the_link[0];
x -> counter++;
x -> counter--;
return;
int j = 1;
x -> counter++;
x = my_node ->the_link[position];
x ->the_link[0] = x ->the_link[1];
x -> counter--;
j++;
return;
int j = 1;
x2 -> counter++;
x2 -> counter++;
j++;
j = position;
j++;
free(x1);
}
void adjustNode(struct btree_node *my_node, int position) {
if (!position) {
left_shift(my_node, 1);
} else {
merge_nodes(my_node, 1);
} else {
right_shift(my_node, position);
} else {
} else {
merge_nodes(my_node, position);
} else {
right_shift(my_node, position);
else
merge_nodes(my_node, position);
int i;
if (my_node) {
for (i = 0; i<my_node -> counter; i++) {
tree_traversal(my_node ->the_link[i]);
tree_traversal(my_node ->the_link[i]);
void main() {
int data_item,ch;
clrscr();
insertion_operation(4);
insertion_operation(6);
insertion_operation(2);
insertion_operation(8);
insertion_operation(10);
insertion_operation(9);
insertion_operation(1);
insertion_operation(3);
insertion_operation(12);
insertion_operation(11);
insertion_operation(13);
tree_traversal(root_node);
getch();
Output:
iii) B+ Trees:
#include<stdio.h>
#include<stdlib.h>
#define maxx 20
#define false 0
#define true 1
void create_graph();
void displayt();
int adjt[maxx][maxx];
int visited[maxx];
int nt;
void main()
int i,v,choice;
system("cls");
create_graph();
printf("[Link] matrix\n");
printf("[Link] first search using stack \n");
printf("[Link] \n");
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
displayt();
break;
case 2:
scanf("%d",&v);
for(i=1;i<=nt;i++)
visited[i]=false;
dfs(v);
break;
case 3:
return;
default:
break;
void create_graph()
{
int i,max_edges,source,destin;
char graph_type;
scanf("%d",&nt);
fflush(stdin);
scanf("%c",&graph_type);
max_edges=nt*(nt-1)/2;
for(i=1;i<=max_edges;i++)
scanf("%d%d",&source,&destin);
if((source==0)&&(destin==0))
break;
if(source>nt||destin>nt||source<=0||destin<=0)
i--;
else
adjt[source][destin]=1;
if(graph_type=='u')
adjt[destin][source]=1;
void displayt()
{
int i,j;
for(i=1;i<=nt;i++)
for(j=1;j<=nt;j++)
printf("%4d",adjt[i][j]);
printf("\n");
void dfs(int v)
int i,stack[maxx],top=-1,pop_v,j,t;
top++;
stack[top]=v;
while(top>=0)
pop_v=stack[top];
top--;
if(visited[pop_v]==false)
printf("%d",pop_v);
visited[pop_v]=true;
else
continue;
for(i=nt;i>=1;i--)
if((adjt[pop_v][i]==1)||(visited[i]==false))
top++;
stack[top]=i;
Output:
#include<stdio.h>
#include<stdlib.h>
#define Maxx 20
#define false 0
#define true 1
void create_grapht();
void displayt();
int adj[Maxx][Maxx];
int visited[Maxx];
int nt;
void main()
int i,v,choice;
system("cls");
printf("\[Link] matrix\n");
printf("\[Link] vertices\n");
printf("[Link]\n");
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
create_grapht();
printf("Adjacency matrix\n");
displayt();
break;
case 2:
scanf("%d",&v);
for(i=1;i<=nt;i++)
visited[i]=false;
bfs(v);
break;
case 3:
scanf("%d",&v);
break;
case 4:
exit(0);
break;
default:
break;
//getch();
void create_grapht()
int i,max_edges,source,destin;
char graph_type;
scanf("%d",&nt);
fflush(stdin);
scanf("%c",&graph_type);
max_edges=nt*(nt-1)/2;
for(i=1;i<=max_edges;i++)
scanf("%d%d",&source,&destin);
if((source==0)&&(destin==0))
break;
if(source>nt||destin>nt||source<=0||destin<=0)
{
printf("\n invalid edge\n");
i--;
else
adj[source][destin]=1;
if(graph_type=='u')
adj[destin][source]=1;
void displayt()
int i,j;
for(i=1;i<=nt;i++)
for(j=1;j<=nt;j++)
printf("%d\t",adj[i][j]);
printf("\n");
void bfs(int v)
int i,front,rear;
int que[20];
front=rear=-1;
printf("%d",v);
visited[v]=true;
rear++;
front++;
que[rear]=v;
while(front<=rear)
v=que[front];
front++;
for(i=1;i<=nt;i++)
if(adj[v][i]==1&&visited[i]==false)
printf("%d",i);
visited[i]=true;
rear++;
que[rear]=i;
void adj_nodes(int v)
int i;
for(i=1;i<=nt;i++)
if(adj[v][i]==1)
printf("%d",i);
printf("\n");
Output:
[Link] a pattern matching algorithms using Boyer-Moore, Knuh-Morris-Pratt.
i) Boyer-Moore :
#include <stdio.h>
#include <string.h>
return (a > b) ? a : b;
}
// Function to preprocess the pattern and generate the bad character heuristic table
int i;
badCharHeuristic[(int)pattern[i]] = i;
int badCharHeuristic[ALPHABET_SIZE];
int shift=0;
int j = patternLength - 1;
j--;
if (j < 0) {
} else {
int main()
clrscr();
searchBoyerMoore(text, pattern);
return 0;
ii)Knuh-Morris-Pratt:
#include <stdio.h>
#include <string.h>
int len = 0;
int i = 1;
lps[0] = 0;
while (i<patternLength) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
} else {
lps[i] = 0;
i++;
int i=0,j=0;
while (i<textLength) {
if (pattern[j] == text[i]) {
j++;
i++;
if (j == patternLength) {
j = lps[j - 1];
if (j != 0) {
j = lps[j - 1];
} else {
i++;
free(lps);
int main() {
clrscr();
searchKMP(text, pattern);
return 0;
}
Output: