II-I AIDS DS
II-I AIDS DS
DATA STRUCTURES
LAB MANUAL (R22)
B.Tech II Year – I Semester
ACADEMIC YEAR (2024-2025)
Department of Information Technology
VISION
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.
MISSION
To impart value based professional education through creative and innovative teaching-learning
process to face the global challenges of the new era technology.
To inculcate research aptitude and to bring out creativity in students by imparting engineering
knowledge imbibing interpersonal skills to promote innovation, research and entrepreneurship.
Vision:
To be a leading department of Artificial Intelligence and Data Science that provides cutting-edge
education, research, and innovation in the field, and prepares graduates to become globally
competitive professionals, researchers, and entrepreneurs.
Mission:
DM1: Providing comprehensive education and training in the principles, tools, and applications of
Artificial Intelligence and Data Science, to prepare graduates for a wide range of careers and
research opportunities.
DM2: Conducting cutting-edge research in the field of Artificial Intelligence and Data Science,
including the development of new algorithms, models, and platforms for data analysis, machine
learning, and deep learning.
DM3: Fostering collaborations and partnerships with industry, government, and academia to
promote the transfer of technology, innovation, and entrepreneurship.
Program Outcomes:
1 PSO1: Professional skills : the ability to understand ,analyze and develop computer
programs in the areas related to algorithms,system software
2 PSO2:Problem solving skills:the ability to apply standard practices and strategies in
software project development to deliver a quality and defect free product.
3 PSO3:Successful career and Entrepreneurship: the ability to employ modern computer
languages, techniques, in creating innovative career paths to be an entrepreneur and a zest
for higher studies
To prepare students to fit into any industry associated with developing and
PEO3
implementation of software products or technologies.
PEO4 To equip the graduates with ability to analyze, design and test the novel products.
Course Structure
Course Code
Programme B.Tech II-I
Course Structure
Practical
L T P Credits
0 0 3 1.5
COURSE OBJECTIVES
S. NO. Course Objectives
DS LAB
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
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
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{
int data;
struct node*next;
}*head=NULL;
int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
i++;
}
return(i);
}
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void delete_end()
{
struct node *temp1,*temp2;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=NULL;
free(temp1);
}
}
void delete_pos(int pos)
{
struct node *temp1,*temp2;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else
{
temp1=head;
while(c<=pos && temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
c++;
}
temp2->next=temp1->next;
free(temp1);
}
}
void display()
{
void main()
{
int ch,pos,value;
do
{
printf("\n1.Insert Begin\n2.Insert End\n3.Insert Position\n4.Delete Begin\n5.Delete
End\n6.Delete Position\n7.Display\n8.Exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter the value:");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("enter value:");
scanf("%d",&value);
insert_end(value);
break;
case 3: printf("enter value:");
scanf("%d",&value);
printf("enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}
OUTPUT
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:1
enter the value:10
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:2
enter value:30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:3
enter value:20
enter position you want to insert: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
10-> 20-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:3
enter value:40
enter position you want to insert: 4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
10-> 20-> 30-> 40
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:5
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
20-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:6
enter position you want to delete: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:8
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 *llink;
int data;
struct node *rlink;
}*head=NULL,*tail=NULL;
int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->rlink!=NULL)
{
temp=temp->rlink;
i++;
}
return(i);
}
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->rlink;
if(head==NULL)
tail=NULL;
else
head->llink=NULL;
free(temp);
}
}
void delete_end()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=tail;
tail=tail->llink;
if(tail==NULL)
head=NULL;
else
tail->rlink=NULL;
free(temp);
}
}
void delete_pos(int pos)
{
struct node *temp1,*temp2,*temp;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else if(pos==i)
{
delete_end();
}
else
{
temp=head;
while(c<pos && temp->rlink!=NULL)
{
temp=temp->rlink;
c++;
}
temp1=temp->llink;
temp2=temp->rlink;
temp1->rlink=temp2;
temp2->llink=temp1;
free(temp);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp->rlink!=NULL)
{
printf("%d <-> ",temp->data);
temp=temp->rlink;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1.Insert Begin\n2.Insert End\n3.Insert Position\n4.Delete Begin\n5.Delete
End\n6.Delete Position\n7.Display\n8.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the value: ");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("Enter value: ");
scanf("%d",&value);
insert_end(value);
break;
case 3: printf("Enter value: ");
scanf("%d",&value);
printf("Enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("Enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}
OUTPUT
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 1
Enter the value: 10
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 2
Enter value: 20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 3
Enter value: 30
Enter position you want to insert: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 30 <-> 20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 3
Enter value: 40
Enter position you want to insert: 4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 30 <-> 20 <-> 40
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 6
Enter position you want to delete: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 20 <-> 40
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 5
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 9
your choice is wrong!..
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 8
3.Aim:
Write a program that uses functions to perform the following operations on circular
linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
Solution :
C Program to implement Circular Linked List
Circular Linked List
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *llink;
int data;
struct node *rlink;
}*head=NULL,*tail=NULL;
int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->rlink!=head)
{
temp=temp->rlink;
i++;
}
return(i);
}
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->rlink;
if(head==tail)
head=tail=NULL;
else
{
head->llink=tail;
tail->rlink=head;
}
free(temp);
}
}
void delete_end()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=tail;
if(tail==head)
{
head=tail=NULL;
}else
{
tail=tail->llink;
tail->rlink=head;
head->llink=tail;
}
free(temp);
}
}
void delete_pos(int pos)
{
struct node *temp1,*temp2,*temp;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else if(pos==i)
{
delete_end();
}
else
{
temp=head;
while(c<pos)
{
temp=temp->rlink;
c++;
}
temp1=temp->llink;
temp2=temp->rlink;
temp1->rlink=temp2;
temp2->llink=temp1;
free(temp);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp!=tail)
{
printf("%d <-> ",temp->data);
temp=temp->rlink;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1.Insert Begin\n2.Insert End\n3.Insert Position\n4.Delete Begin\n5.Delete End\n6.Delete
Position\n7.Display\n8.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the value: ");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("Enter value: ");
scanf("%d",&value);
insert_end(value);
break;
case 3: printf("Enter value: ");
scanf("%d",&value);
printf("Enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("Enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}
OUTPUT
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 1
Enter the value: 10
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 3
Enter value: 20
Enter position you want to insert: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 2
Enter value: 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 20 <-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 3
Enter value: 40
Enter position you want to insert: 3
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 20 <-> 40 <-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 6
Enter position you want to delete: 3
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
10 <-> 20 <-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
20 <-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 5
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 7
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 9
your choice is wrong!..
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
Enter your choice: 8
4.Aim:Write a program that implement Stack (its operations) using Array
Solution :
#define MAX_SIZE 5
int stack[MAX_SIZE],top=-1;
// Main function
int main() {
int ch,data;
do{
printf("\n1. Push\n2. Pop\n3. Peek\n4. Show\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to push: ");
scanf("%d",&data);
push(data);
break;
case 2: printf("Popped: %d\n", pop());
break;
case 3: printf("Top element: %d\n", peek());
break;
case 4: show();
break;
case 5: break;
default: printf("Enter valid choice");
}
}while(ch!=5);
return 0;
}
OUTPUT
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 20
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 30
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 3
Top element: 40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
40
30
20
10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 50
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 60
Stack Overflow
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
50
40
30
20
10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 50
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 30
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 20
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Stack Underflow
Popped: -1
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
Stack is Empty
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 5
5. Aim:Write a program that implement Stack (its operations) using Linked List (Pointer)
Solution :
C Program to implement Stack using Linked List(Pointer)
Stack using Linked List
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
}*head=NULL;
void pop()
{
struct node *temp;
if(head==NULL)
{
printf("Stack is underflow");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void show()
{
struct node *temp;
if(head==NULL)
{
printf("Stack is empty");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1. Push\n2. Pop\n3. Show\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the value: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: show();
break;
case 4:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=4);
}
OUTPUT
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 1
Enter the value: 10
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 1
Enter the value: 20
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 1
Enter the value: 30
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 3
30
20
10
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 3
20
10
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 2
Stack is underflow
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 3
Stack is empty
1. Push
2. Pop
3. Show
4. Exit
Enter your choice: 4
6.Aim: Write a program that implement Queue(its operations) using Array
Solution :
C Program to implement Queue (its operations) using Array
Queue using Array
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
// Main function
int main() {
int ch,data;
do{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to insert: ");
scanf("%d",&data);
enqueue(data);
break;
case 2: printf("Deleted: %d\n", dequeue());
break;
case 3: display();
break;
case 4: break;
default: printf("your choice is wrong!..");
}
}while(ch!=4);
return 0;
}
OUTPUT
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
10 20 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 40
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 50
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
10 20 30 40 50
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 60
Queue Overflow
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted: 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
40 50
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted: 40
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted: 50
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Queue Underflow
Deleted: -1
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
Queue is Empty
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 4
7. Aim: Write a program that implement Queue (its operations) using Linked
List (Pointer)
Solution :
C Program to implement Queue using Linked List(pointer)
Queue using Linked List
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
}*head=NULL;
void dequeue()
{
struct node *temp;
if(head==NULL)
{
printf("Queue Underflow");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("Queue is empty");
}
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d, ",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to insert: ");
scanf("%d",&value);
enqueue(value);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4:break;
default: printf("\nyour choice is wrong!..");
}
}while(ch!=4);
}
OUTPUT
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter data to insert: 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
10, 20, 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
20, 30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Queue Underflow
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
Queue is empty
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 4
8. Aim: Write a program that implements Quick sort sorting methods to sort a given list of
integers in ascending order
Solution :
C program that implements Quick sort sorting methods to sort a given list of integers in ascending
order
Quick sort
#include <stdio.h>
OUTPUT
How many elements are u going to enter?: 10
Enter 1 element: 3
Enter 2 element: 6
Enter 3 element: 9
Enter 4 element: 8
Enter 5 element: 5
Enter 6 element: 2
Enter 7 element: 1
Enter 8 element: 4
Enter 9 element: 7
Enter 10 element: 10
Order of Sorted elements: 1 2 3 4 5 6 7 8 9 10
9. Aim:Write a program that implements Merge sort sorting methods to sort a given list of integers in
ascending order
Solution :
C program that implements Merge sort sorting methods to sort a given list of integers in ascending order
Merge sort
#include <stdio.h>
void merge(int A[], int mid, int low, int high)
{
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
mergeSort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT
How many elements are u going to enter?: 10
Enter 1 element: 10
Enter 2 element: 1
Enter 3 element: 4
Enter 4 element: 7
Enter 5 element: 8
Enter 6 element: 5
Enter 7 element: 2
Enter 8 element: 3
Enter 9 element: 6
Enter 10 element: 9
Order of Sorted elements: 1 2 3 4 5 6 7 8 9 10
10. Aim: Write a program that implements Heap sort sorting methods to sort a given list of
integers in ascending order
Solution :
C program that implements Heap sort sorting methods to sort a given list of integers in ascending
order
Heap sort
#include <stdio.h>
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
heapsort(number,count);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT
How many elements are u going to enter?: 10
Enter 1 element: 2
Enter 2 element: 5
Enter 3 element: 8
Enter 4 element: 10
Enter 5 element: 3
Enter 6 element: 1
Enter 7 element: 4
Enter 8 element: 6
Enter 9 element: 7
Enter 10 element: 9
Order of Sorted elements: 1 2 3 4 5 6 7 8 9 10
11. Aim:Write a program that implements Heap sort sorting methods to sort a given list of
integers in ascending order
Solution :
C program that implements Heap sort sorting methods to sort a given list of integers in ascending
order
Heap sort
#include <stdio.h>
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
heapsort(number,count);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT
How many elements are u going to enter?: 10
Enter 1 element: 2
Enter 2 element: 5
Enter 3 element: 8
Enter 4 element: 10
Enter 5 element: 3
Enter 6 element: 1
Enter 7 element: 4
Enter 8 element: 6
Enter 9 element: 7
Enter 10 element: 9
Order of Sorted elements: 1 2 3 4 5 6 7 8 9 10
12. Aim:Write a program to implement the tree traversal methods using
Non Recursive
Solution :
C program to implement the tree traversal methods using Recursive
Tree Traversal using Recursive
#include <stdio.h>
#include <stdlib.h>
int main() {
// Constructing a binary tree
// 1
// /\
// 2 3
// / \
// 4 5
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
/* Traversals
printf("Pre-order traversal: ");
preOrder(root);
printf("\n"); */
return 0;
}
OUTPUT
Pre-order traversal: 1 2 4 5 3
In-order traversal: 4 2 5 1 3
Post-order traversal: 4 5 2 3 1
13. Aim:Write a program to implement Binary Search Tree (its operations)
Solution :
C program to implement the Binary Search Tree
Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root)
{
if (root != NULL)
{ // Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// preorder Traversal
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d -> ", root->key);
preorder(root->left);
preorder(root->right);
}
}
// postorder Traversal
void postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d -> ", root->key);
}
}
// Insert a node
struct node *insert(struct node *node, int key)
{ // Return a new node if the tree is empty
if (node == NULL)
return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key)
{ // Return if the tree is empty
if (root == NULL)
return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else
{
// If the node is with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
int main() {
int choice,value;
struct node *root = NULL;
do
{
printf("\n1. Insertion\n2. Deletion\n3. inorder\n4. preorder\n5. postorder\n6. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
root = insert(root, value);
break;
case 2: printf("Enter the value to be deleted: ");
scanf("%d",&value);
root = deleteNode(root, value);
break;
case 3: inorder(root);
break;
case 4: preorder(root);
break;
case 5: postorder(root);
break;
case 6: freeMemory(root);
break;
default: printf("\nWrong selection!!! Try again!!!");
}
}while(choice!=6);
return (0);
}
OUTPUT
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 50
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 20
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 80
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 5
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 30
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 65
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 1
Enter the value to be insert: 90
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 3
5 -> 20 -> 30 -> 50 -> 65 -> 80 -> 90 ->
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 4
50 -> 20 -> 5 -> 30 -> 80 -> 65 -> 90 ->
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 5
5 -> 30 -> 20 -> 65 -> 90 -> 80 -> 50 ->
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 2
Enter the value to be deleted: 50
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 4
65 -> 20 -> 5 -> 30 -> 80 -> 90 ->
1. Insertion
2. Deletion
3. inorder
4. preorder
5. postorder
6. Exit
Enter your choice: 6
14. Aim:Write a program to implement AVL Tree (its operations)
Solution :
C program to implement the AVL Tree
AVL Tree
#include <stdio.h>
#include <stdlib.h>
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}
// Function to perform left rotation
struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
return root;
}
// Function to find the node with the minimum value in the tree
struct Node* findMinValueNode(struct Node* node) {
struct Node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
} else // One child case
*root = *temp; // Copy the contents of the non-empty child
free(temp);
} else {
// Node with two children: Get the inorder successor (smallest
// in the right subtree)
struct Node* temp = findMinValueNode(root->right);
return root;
}
int main() {
int choice,value;
struct Node* root = NULL;
do
{
printf("\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
root = insert(root, value);
break;
case 2: printf("Enter the value to be deleted: ");
scanf("%d",&value);
root = deleteNode(root, value);
break;
case 3: inOrderTraversal(root);
break;
case 4: freeMemory(root);
break;
default: printf("\nWrong selection!!! Try again!!!");
}
}while(choice!=4);
return 0;
}
OUTPUT
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 1
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 2
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 3
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 4
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 5
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 6
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
123456
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 2
Enter the value to be deleted: 2
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 2
Enter the value to be deleted: 3
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 2
Enter the value to be deleted: 1
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
456
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 4
15. Aim:Write a program to implement Red - Black Tree (its operations)
Solution :
C program to implement the Red Black Tree
Red Black Tree
// Red Black Tree operations in C
#include <stdio.h>
#include <stdlib.h>
while (x != NULL) {
y = x;
if (z->data < x->data)
x = x->left;
else
x = x->right;
}
z->parent = y;
if (y == NULL)
tree->root = z;
else if (z->data < y->data)
y->left = z;
else
y->right = z;
insertFixup(tree, z);
}
// Function to find the minimum value node in the tree rooted at a given node
Node* findMinValueNode(Node* node) {
Node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
if (z == NULL) {
printf("Node not found in the tree\n");
return; // Node to be deleted not found
}
if (z->left == NULL) {
x = z->right;
transplant(tree, z, z->right);
} else if (z->right == NULL) {
x = z->left;
transplant(tree, z, z->left);
} else {
y = findMinValueNode(z->right); // Find the minimum node of the right subtree
yOriginalColor = y->color;
x = y->right;
if (y->parent == z) {
x->parent = y; // Necessary when x is NULL
} else {
transplant(tree, y, y->right);
y->right = z->right;
y->right->parent = y;
}
transplant(tree, z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
}
free(z);
if (yOriginalColor == 0) {
deleteFixup(tree, x);
}
}
int main() {
int choice,value;
RedBlackTree* tree = createRedBlackTree();
do
{
printf("\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
insert(tree, value);
break;
case 2: printf("Enter the value to be deleted: ");
scanf("%d",&value);
delete(tree, value);
break;
case 3: inOrderTraversal(tree->root);
break;
case 4: freeMemory(tree->root);
break;
default: printf("\nWrong selection!!! Try again!!!");
}
}while(choice!=4);
return(0);
}
OUTPUT
/tmp/tnOjm2NG3L.o
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 1
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 2
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 3
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
1,RED -> 2,BLACK -> 3,RED ->
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 4
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 5
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
1,BLACK -> 2,BLACK -> 3,RED -> 4,BLACK -> 5,RED ->
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 2
Enter the value to be deleted: 3
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
1,BLACK -> 2,BLACK -> 4,BLACK -> 5,RED ->
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 2
Enter the value to be deleted: 5
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
1,BLACK -> 2,BLACK -> 4,BLACK ->
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 4
16. Aim:Write a program to implement B Trees (its operations )
Solution :
C program to implement the B-Tree
B-Tree
/* For simplicity, provide a basic implementation focusing on insertion, search, and a simple
traversal. We will not include deletion as it is quite complex and would make the code exceedingly
long. In practice, B-tree deletion requires handling numerous cases to redistribute or merge nodes.
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_KEYS 3 // Maximum keys in a node (t-1 where t is the minimum degree)
#define MIN_KEYS 1 // Minimum keys in a node (ceil(t/2) - 1)
#define MAX_CHILDREN (MAX_KEYS + 1) // Maximum children in a node (t)
if (!child->isLeaf) {
for (int i = 0; i < MIN_KEYS + 1; i++) {
newChild->children[i] = child->children[i + MIN_KEYS + 1];
}
}
child->numKeys = MIN_KEYS;
parent->children[index + 1] = newChild;
parent->keys[index] = child->keys[MIN_KEYS];
parent->numKeys++;
}
if (node->isLeaf) {
while (i >= 0 && node->keys[i] > key) {
node->keys[i + 1] = node->keys[i];
i--;
}
node->keys[i + 1] = key;
node->numKeys++;
} else {
while (i >= 0 && node->keys[i] > key) {
i--;
}
if (!root->isLeaf) {
traverse(root->children[i]);
}
}
int main() {
BTreeNode* root = createNode(1);
insert(&root, 10);
insert(&root, 20);
insert(&root, 5);
insert(&root, 6);
insert(&root, 12);
insert(&root, 30);
insert(&root, 7);
insert(&root, 17);
return 0;
}
OUTPUT
Traversal of the constructed B-tree is:
5 6 7 10 12 17 20 30
17. Aim:Write a program to implement B Trees (its operations)
Solution :
C program to implement the B+ Tree
B+ Tree
#include
#include<stdlib.h>
#include<stdbool.h>
struct BPTreeNode {
int *data;
struct BPTreeNode **child_ptr;
bool leaf;
int n;
}*root = NULL, *np = NULL, *x = NULL;
void insert(int a) {
int i, temp;
x = root;
if (x == NULL) {
root = init();
x = root;
} else {
if (x->leaf == true && x->n == 5) {
temp = split_child(x, -1);
x = root;
for (i = 0; i < (x->n); i++) {
if ((a > x->data[i]) && (a < x->data[i + 1])) {
i++;
break;
} else if (a < x->data[0]) {
break;
} else {
continue;
}
}
x = x->child_ptr[i];
} else {
while (x->leaf == false) {
for (i = 0; i < (x->n); i++) {
if ((a > x->data[i]) && (a < x->data[i + 1])) {
i++;
break;
} else if (a < x->data[0]) {
break;
} else {
continue;
}
}
if ((x->child_ptr[i])->n == 5) {
temp = split_child(x, i);
x->data[x->n] = temp;
x->n++;
continue;
} else {
x = x->child_ptr[i];
}
}
}
}
x->data[x->n] = a;
sort(x->data, x->n);
x->n++;
}
int main() {
int i, n, t;
printf("enter the no of elements to be inserted\n");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("enter the element\n");
scanf("%d", &t);
insert(t);
}
printf("traversal of constructed tree\n");
traverse(root);
return 0;
}
OUTPUT
enter the no of elements to be inserted
8
enter the element
10
enter the element
20
enter the element
5
enter the element
6
enter the element
12
enter the element
30
enter the element
7
enter the element
17
traversal of constructed tree
5 6 7 10 12 17 20 30
18. Aim:Write a program to implement the graph traversal methods (Breadth
First Search)
Solution :
C program to implement the Breadth First Search a graph traversal methods
BFS
#include<stdio.h>
int main()
{
int v,n,i,j;
// adjacenty matrix representing graph
int graph[10][10];
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter graph data in matrix form: \n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &graph[i][j]);
OUTPUT
Enter the number of vertices: 6
Enter graph data in matrix form:
011000
101000
110110
001000
001001
000010
Enter the starting vertex: 2
2132456
19. Aim:Write a program to implement the graph traversal methods (Depth
First Search)
Solution :
C program to implement the Depth First search a graph traversal methods
DFS
#include <stdio.h>
int a[20][20], visited[20], n;
void dfs(int v)
{
int i;
visited[v] = 1;
for (i = 1; i <= n; i++)
{
if (a[v][i] && !visited[i])
{
printf("\n %d->%d", v, i);
dfs(i);
}
}
}
int main( )
{
int i, j,v, count = 0;
printf("\n Enter number of vertices:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
visited[i] = 0;
for (j = 1; j <= n; j++)
a[i][j] = 0;
}
printf("\n Enter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("Enter the starting vertex: ");
scanf("%d", &v);
dfs(v);
return 0;
}
OUTPUT
Enter number of vertices:6
Enter the adjacency matrix:
011000
101000
110110
001000
001001
000010
Enter the starting vertex: 2
2->1
1->3
3->4
3->5
5->6
20. Aim:Write a program to Implement a Pattern matching algorithms using Boyer-
Moore
Solution :
C program to Implement a Pattern matching algorithms using Boyer- Moore
Boyer-Moore Pattern matching
#include <stdio.h>
#include <string.h>
OUTPUT
pattern is present in text at position 14
21. Aim:Write a program to Implement a Pattern matching algorithms using Knuth-Morris-
Pratt
Solution :
C program to Implement a Pattern matching algorithms using Knuth-Morris-Pratt
Knuth-Morris-Pratt Pattern matching
#include <stdio.h>
#include <string.h>
int lps[100];
void longestPrefixSuffix(char p[])
{
int i=1,j=0;
int m = strlen(p);
lps[0] = 0;
while(i < m)
{
if( p[j] == p[i])
{
lps[i]=j+1;
i++;
j++;
}
else if(j>0)
j = lps[j-1];
else
{
lps[i]=0;
i++;
}
}
}
OUTPUT
pattern is present in text at position 14
DATA STRUCTURES VIVA QUESTIONS
1Q) What is a Data Structure?
Ans) A Data Structure is a data object together with the relationships that exists among the instances & among the
individual elements that compose an instance.
2Q) Types of Data Structures and give examples?
Ans) There are two types of Data Structures:
1. Linear Data Structures: A data structure is said to be linear if the elements form a sequence. It is sequential and
continues in nature i.e. access the data in sequential manner. In linear data structure we can not insert an item in
middle place and it maintains a linear relationship between its elements egs: Array, Linked list, Stack, Queue, Dequeue
etc.
2. Non Linear Data Structures: A data structure is said to be non-linear if elements do not form a sequence. (Not
sequential). It does not maintain any linear relationship between their elements. Every data item is attached to
several other data items in a way that is specific for reflecting relationships. The data items are not arranged in a
sequential structure. egs: Trees, Graphs. [A data structure is linear if every item is related with next and previous item
and it is non linear if it is attach with many of the items in specific ways to reflect relationship.]
3Q) What is a Singly Linked List?
Ans) Singly Linked List is a Sequence of dynamically allocated Storage elements, each element of which contains a
pointer to its successor. A pointer to the first element of the list is called as head and a pointer to the last element of
the list is called as tail used to keep track of the list elements.
4Q) What is Doubly Linked List?
Ans) In Doubly Linked List each element contains two pointers: One Pointer points to its successor and another to its
predecessor (previous element).It is also called as two way linked list (traversing can be done in both directions).
5Q) Differentiate Array and Linked List?
Ans Array ) Linked List
1.Size of the array is fixed 1.Size of the linked list is not fixed
2. Memory is allocated dynamically (at runtime).
2. Memory is allocated Statically (or) Dynamically (at run time). (If the memory is allocated for an array Statically(at
compile time) it is called Static Array and if memory is allocated at run time (dynamically)using operator new it is
called Dynamic Array)
3.Memory wastage will be there if all the array positions are not utilized 3.Memory is not wasted as only Required
memory is allocated STACKS: (LIFO DATA STRUCTURE)
6Q) What is a Stack? (LIFO Data Structure)
Ans) Stack is an ordered collection of items into which items can be inserted and deleted from only one end called as
“Top” of the Stack. It is also called as LIFO list.(Last In First Out).
7Q) What is Stack Underflow?
Ans) Is Stack is empty and POP operation is performed it is not possible to delete the items. This situation is called
Stack Underflow.
8Q) What is Stack Overflow?
Ans) If Stack is full and PUSH operation is performed it is not possible to insert or Push the new items into the stack.
This situation is called Stack Overflow.
9Q) What are the Applications of Stack?
Ans) i) Stacks are used to convert Infix expression into Postfix. ii) Stacks are used to Evaluate Postfix Expression. iii)
Stacks are used in recursion etc.
10Q) What is the use of Postfix expressions?
Ans) Postfix Expressions are easy to evaluate as postfix expressions does not make use of operator precedence not
does it require the use of parenthesis.
(FIFO DATA STRUCTURE)
11Q) What is a Queue?
Ans) It is an ordered collection of items into which items can be inserted from one end called as REAR end and items
are deleted from other end called as FRONT end of the Queue. It is also called as FIRST IN FIRST OUT (FIFO) LIST).
12Q) What are the applications of Queues?
Ans) i) Queues are used in Breadth First Traversal of a Tree. ii) Queues are used in implementation of Scheduling
algorithms of Operating Systems.
13Q) What is a Circular Queue?
Ans) In Circular Queue, the first position of the array is kept behind the last position of the array.
14Q) Differentiate Linear Queue and Circular Queue?
Ans) In Linear Queue once the queue is full and the deletion is performed, even if first position is free(vacant) it is not
possible to insert the item in that position whereas in Circular Queue it is possible since the first position is kept
behind the last position.
15Q) What is Dequeue? (Double Ended Queue)
Ans) In Double Ended Queue insertion and deletion are possible from both the ends.
TREES:
16Q) What is a Tree?
Ans) Tree is a finite non-empty set of nodes with the following properties: i) ii) A designated node of the set is called
as root of the tree and The remaining nodes are partitioned into n>=0 subsets, each of which is a tree. Degree of a
node: The number of sub trees attached to a node is called degree of that node and the maximum degree of any node
in a tree is called degree of that tree. [Note: In a general tree degree of a node is not fixed] Nodes that have degree
zero are called Leaf or Terminal Nodes. Consequently, the other nodes are referred to as Non-Terminals. The Level of
a node is defined by letting the root be at level o or 1. The height or depth of a tree is defined to be the maximum
level of any node in the tree.
17Q) What is a Binary Tree?
Ans) A Binary tree T is a finite set of nodes with the following properties: i) ii) Either the set is empty, T=Ø or The set
consists of a root and exactly two distinct binary trees TL and TR,T={r,TL,TR}.TL is the left subtree and TR is the right
subtree of T. [Note: Maximum degree of any node in a binary tree is 2.Degree of a node in a Binary Tree be either 0 or
1 or 2]
18Q) What is Tree Traversal? List different Tree Traversal Techniques?
Ans) Visiting all nodes of a tree exactly once in a systematic way is called Tree Traversal. Different types of tree
traversals are i) ii) Depth First Traversals: PREOREDER (N L R) ,INORDER (L N R) & POSTORDER (L R N) Traversals.
Breadth First Traversal (or) Level Order Traversal (Visiting Level by level from left to right)
19Q) What is a Binary Search Tree? Give one example?
Ans) A Binary Search Tree T is a finite set of keys. Either the set is empty T=Ø ,or the set consists of a root “r” and
exactly two binary search trees TL and TR,T = {r,TL,TR} with the following properties: i) ii) All the keys contained in the
left subtree are less than the root key. All the keys contained in the right subtree are larger than the root key. [Note:
Duplicates are not allowed in a Binary Search Tree]
20Q) What is the best, average and worst case time complexity of insertion, deletion and Search operations in a
Binary Search Tree?
Ans) In Best and Avg case O(log n) and in Worst case O(n). 21Q) What is an AVL Search Tree? What is AVL Balance
Condition? Ans) An AVL Search Tree is a balanced binary search tree. An empty binary tree is AVL balanced. A non –
empty binary tree, T={r,TL,TR} is AVL balanced if both TL & TR are AVL balanced and | hL – hR | <=1, Where : hL is the
Height of the left subtree and hR is the Height of the right subtree. [Note : Allowable balance factors of any node is an
AVL tree are 0 or 1 or -1 and the use of balancing a binary search tree is even in worst case the time complexity of
insert, delete and search operations is reduced to O(log n) from O(n)]
22Q) What is a Full (perfect) Binary Tree?
Ans) If a binary tree of height ‘h’ has exactly [2h+1 -1] nodes then that binary tree is called as Full Binary Tree.
23Q) What is a Complete Binary Tree?
Ans) Complete Binary Tree is a binary tree T = {r,TL,TR} with the following properties: If “i” is the index of any node in
a complete binary tree then: i) ii) iii) The parent of “i” is at position “i/2” [if i=1 it is the root node and has no parent]
The left child of node “i” is at position “2i” [if 2i>n then no left child exists] The right child of node “i” is at position
“2i+1” [if 2i+1>n then no right child exists] [Note: In a complete binary tree nodes are filled level by level from left to
right]
24Q) List one application of trees (Search trees)?
Ans) Search trees are used to implement dictionaries.
PRIORITY QUEUES:
25Q) What is Priority Queue and differentiate Priority Queue and Queue?
Ans) In Priority Queue each item is associated with a priority. In Priority Queue items can be inserted arbitrary order
but the items are deleted based upon the priority that is the item with highest priority is deleted first. Whereas in
Queue the items are inserted from rear end and deleted from front end and the item which is inserted first is deleted
first (FIFO).
26Q) What is a Min Heap?
Ans) Min Heap is a Complete Binary Tree in which the key value at parent is always less than or equal to its child
values.
27Q) What is a Max Heap?
Ans) In Max Heap the key value at parent is always larger or equal to its child values.
28Q) What is a (Binary) Heap?
Ans) A (Binary) Heap is a Complete Binary Tree with Heap Ordered Property (Min Heap or Max Heap) [Note:
Duplicates are allowed in a Heap] GRAPHS:
29Q) What is a Graph? Name various Graph Representation Techniques? Ans) A Graph G = (V,E) is Set of Vertices and
Edges. A Graph can be represented as an Adjacency Matrix (or) as an Adjacency List.
30Q) What is difference between a Graph and a Tree?
Ans) A Graph contains Cycles (loops) but a Tree does not contain any cycles. [A Tree contains a root node but Graph
does not contain the root node.]
31Q) What is a Spanning Tree?
Ans) A Spanning Tree T = (V', E') is a Sub Graph of G = (V,E) with following properties: i) ii) iii) V = V' [The vertices in a
graph and spanning tree are same] T is Acyclic. T is connected. [Note: I f Graph has “n” vertices the Spanning Tree
contains exactly “n - 1” edges] 32Q) Name the methods to construct a Minimum cost Spanning Tree? Ans) Prim’s
method and Kruskal’s method. SORTING & SEARCHING: