DS Programs
DS Programs
1.Wri t Write a program to search for an element in an array using binary and linear search.
2.
Write a program to sort list of n numbers using Bubble Sort algorithm
3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display
the output in descending order
4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion
5.
Wr Write a program to insert the elements {61,16,8,27} into linear queue and delete
three elements from the list. Display your list after each insertion and deletion.
6. Write a program to simulate the working of Circular queue using the array.
7. Write a program to insert the elements {61,16,8,27} into ordered singly linked list and
delete 8,61,27 from the list. Display your list after each insertion and deletion
8.
Wr Write a Program for Tower of Honoiu problem using recursion.
9.
Writ Write a recursive program to find GCD of 3 Numbers.
10.
Wr Write a program to demonstrate working of stack using linked list.
11. Write a Program to convert an infix expression x^y/(5*z)+2 to its postfix expression.
12.
Wr Write a program to evaluate a postfix expression 53+82-*
13. Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} .After
Creation insert 45 and 19 into tree and delete 15 ,17 and 41 from tree. Display the tree
on each insertion and deletion operation.
14. Write a program to create binary search tree with the elements{2,5,1,3,9,0,6} and
perform inorder , preorder and post order traversal.
15. Write a program to Sort the following elements using heap sort {9,16,32,8,4,1,5,8,0}.
16. Given S1={“Flowers”}; S2={“are beautiful”}; I.Find the length of S1 . II. Concatenate
S1 and S2 . III. Extract the substring “low” from S1. IV. Find “are ” in S2 and replace it
with “is”.
17. Write a Program to implement adjacency matrix of a graph.
18. Write a program to insert/retrieve an entry into hash from a hash table with open
addressing using linear probing .
PROGRAM 1:
WRITE A PROGRAM TO SEARCH FOR AN ELEMENT IN AN ARRAY USING BINARY
AND LINEAR SEARCH.
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Return the index if found
} else if (arr[mid] < target) {
low = mid + 1; // Search in the right half
} else {
high = mid - 1; // Search in the left half
}
}
return -1; // Return -1 if not found
}
int main() {
int search_list[] = {3, 1, 9, 8, 7, 12, 56, 23, 89};
int length = sizeof(search_list) / sizeof(search_list[0]);
int target,i,j;
int linearResult, binaryResult;
// Linear Search
printf("Enter a value to search using Linear Search: ");
scanf("%d", &target);
if (linearResult != -1) {
printf("Linear Search: Element found at index %d\n", linearResult);
} else {
printf("Linear Search: Element not found\n");
}
// Sorting the array for Binary Search
// Here we use a simple bubble sort for demonstration purposes
for ( i = 0; i < length - 1; i++) {
for ( j = 0; j < length - i - 1; j++) {
if (search_list[j] > search_list[j + 1]) {
int temp = search_list[j];
search_list[j] = search_list[j + 1];
search_list[j + 1] = temp;
}
}
}
printf("Sorted Array for Binary Search: ");
for ( i = 0; i < length; i++) {
printf("%d ", search_list[i]);
}
printf("\n");
// Binary Search
printf("Enter a value to search using Binary Search: ");
scanf("%d", &target);
if (binaryResult != -1) {
printf("Binary Search: Element found at index %d\n", binaryResult);
} else {
printf("Binary Search: Element not found\n");
}
return 0;
}
Output:
PROGRAM 2:
WRITE A PROGRAM TO SORT LIST OF N NUMBERS USING BUBBLE SORT
ALGORITHM.
#include <stdio.h>
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
// Outer loop for each pass
int i,j;
for ( i = 0; i < n - 1; i++) {
// Inner loop for comparing adjacent elements
for ( j = 0; j < n - i - 1; j++) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n,i;
int arr[100];
return 0;
}
Output:
PROGRAM 3: PERFORM THE INSERTION AND SELECTION SORT ON THE INPUT
{75,8,1,16,48,3,7,0} AND DISPLAY THE OUTPUT IN DESCENDING ORDER.
#include <stdio.h>
return 0;
}
Output:
Program 4: Program to insert the elements {61,16,8,27} into singly linked list and delete
8,61,27 from the list . Display your list after each insertion and deletion.
#include <stdio.h>
#include <malloc.h> //calloc: static & malloc: dynamic
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;//renaming struct with new name
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main()
{
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (1) //true in all ca
{
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("Others : Exit()\n"); // printf(“case 4: exit()\n”);
printf("Enter your option:");
scanf("%d", &option);
switch (option)
{
case 1:
insert();
display(); break;
case 2:
delete(); display(); break;
case 3:
display(); break;
case 4: default:
exit(0);
}
}
return 0;
}
void insert()
{
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0) //first node= header node
{
first_node = temp_node;
}
else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete()
{
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos); //input pos=2;
if (pos> 0 &&pos<= countvalue) //(if (2>0 && 2<=4) true)
{
if (pos == 1)// (2==1) false//exit goto else
{
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
}
else
{
while (temp_node != 0)
{
if (i == (pos - 1)) //0==2-1false(0==1)
{
prev_node->next = temp_node->next;
if(i == (countvalue - 1))//0==4-1->0==3
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
}
Else
{
i++;//i=1
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
}
else
printf("\nInvalid Position \n\n");
}
void display()
{
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0)
{
#include <stdio.h>
#define MAX 20
#include<stdlib.h>
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("\nEnter element : ");
scanf("%d", &add_item);
printf("\n%d is inserted in queue\n",add_item);
printf("------------------------------\n");
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("\nElement deleted from queue is : %d\n", queue_array[front]);
printf("------------------------------\n");
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("\nQueue is : ");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n------------------------------");
printf("\n");
}
}
int main()
{
printf("Perform operations on queue\n");
printf("------------------------------\n");
printf("\tMenu");
printf("\n------------------------------\n");
printf("1. Insert element \n");
printf("2. Delete element \n");
printf("3. Display queue\n");
printf("4. Exit\n");
printf("------------------------------\n");
int ch;
while (1)
{
printf("Choose operation : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Invalid operation \n");
}
}
return 0;
}
Program 6:
Write a program to simulate the working of Circular queue using the array.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d) //Insert elements in Queue
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
}
void dequeue() // Delete an element from Queue
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
free(t);
}
else{
f = f->next;
r->next = f;
free(t);
}
}
void print(){ // Print the elements of Queue
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
}
int main()
{
int opt,n,i,data;
printf("Enter Your Choice:-");
do{
printf("\n\n 1 for Insert the Data in Queue\n 2 for show the Data in Queue \n 3 for Delete
the data from the Queue\n 0 for Exit\n");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the size of data");
scanf("%d",&n);
printf("\nEnter your data");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");
}
}while(opt!=0);
return 0;
}
Program 7:
Write a program to insert the elements {61,16,8,27} into ordered singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion
#include <stdio.h>
#include <stdlib.h>
// Create a node
struct Node {
int data;
};
// Insertion operation
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
// Delete a node
*head_ref = temp->next;
free(temp);
return;
prev->next = temp->next;
free(temp);
int temp;
if (head_ref == NULL) {
return;
} else {
index = current->next;
temp = current->data;
current->data = index->data;
index->data = temp;
index = index->next;
current = current->next;
}
}
}
node = node->next;
// Driver program
int main() {
insert(&head, 27);
insert(&head, 8);
insert(&head, 16);
insert(&head, 61);
sortLinkedList(&head);
printList(head);
deleteNode(&head, 8);
deleteNode(&head, 61);
deleteNode(&head, 27);
printList(head);
}
Program 8:
#include <stdio.h>
void toh(int , char, char ,char);
int count = 0;
void main(){
char source = 's', temp = 'T', dest = 'D';
int n;
scanf("%d" , &n);
}
Program 9:
# include <stdio.h>
if(n==0)
{
return m;
else if(n>m)
return(GCD(n,m));
else
return(GCD(n , m %n));
void main(){
int k , n,m;
}
Program 10:
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
struct stack
int info;
};
//functions
//push function
void push(int item)
if(TOP == NULL)
TOP = NEWNODE;
else
{
//pop function
int pop(){
int ITEM;
if(TOP == NULL)
printf("stack underflow\n");
return -1;
TEMP = TOP;
free(TEMP);
return ITEM;
//display function
void display()
if(CURRPTR == NULL)
{
printf("stack is empty\n");
return;
}
printf("stack elements are:\n");
while(CURRPTR != NULL)
void main()
do
{
printf("\n *MainMenu*");
printf("\n 1.PUSH");
printf("\n 2. POP*");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\n Enter the number to be pushed on stack");
scanf("%d", &item);
push(item);
break;
break;
case 3: display();
break;
while(choice!=4);
}
Program 11:
Write a Program to convert an infix expression x^y/(5*z)+2 to its postfix expression.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
void main()
{
char infix[20],postfix[20];
if(symbol == '^' || symbol == '$' || symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' ||
symbol == '(' || symbol == ')')
return 1;
else return 0;
}
Program 12:
Write a program to evaluate a postfix expression 53+82-*
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression : ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(*e)
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s is : %d\n",exp,pop());
return 0;
}
Prograam 13:
Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} .After Creation
insert 45 and 19 into tree and delete 15 ,17 and 41 from tree. Display the tree on each
insertion and deletion operation.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *left;
struct node *right;
};
/*display function */
void disp(struct node *ptr, int level)
{
int i;
if(ptr!=NULL)
{
disp(ptr->right ,level+1);
for(i=0;i<level;i++)
printf(" ");
printf("%2d\n", ptr->info);
disp(ptr->left,level+1);
}
}
/*Create function */
void create(int item){
void main()
{
int item,ch,n,i;
while(1)
{
printf("\n Binary Search Tree Menu");
printf("\n ---------------------------\n");
printf("\n 1.Insert");
printf("\n 2. Delete");
printf("\n 3.Display");
printf("\n4. Exit");
printf("\n Enter the choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\n enter the number of nodes:");
scanf("%d" , &n);
for(i=0;i<n;i++){
printf("\n enter the data for the node :");
scanf("%d", &item);
create(item);
}
break;
case 2: printf("\n Enter an Item to be deleted:");
scanf("%d" , &item);
root = deletion(root,item);
disp(root,1);
break;
case 3:printf("\n The Binary Tree nodes are:\n\n\n\n");
disp(root, 1);
break;
case 4 : exit(1);
}
}
}
Program 14:
Write a program to create binary search tree with the elements{2,5,1,3,9,0,6} and perform
inorder , preorder and post order traversal.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
}
Program 15: Write a program to Sort the following elements using heap sort
{9,16,32,8,4,1,5,8,0}.
#include<stdio.h>
int temp;
void heapify(int arr[], int size, int i){
int largest = i; int left = 2*i + 1;
int right = 2*i + 2;
if (left < size && arr[left] >arr[largest]) largest = left;
if (right < size && arr[right] > arr[largest]) largest = right;
if (largest != i)
{
temp = arr[i]; arr[i]= arr[largest]; arr[largest] = temp;
heapify(arr, size, largest);
}
}
void heapSort(int arr[], int size)
{
int i;
for (i = size / 2 - 1; i >= 0; i--) heapify(arr, size, i);
for (i=size-1; i>=0; i--)
{
temp = arr[0]; arr[0]= arr[i]; arr[i] = temp; heapify(arr, i, 0);
}
}
void main(){
int arr[] = {9,16,32,8,4,1,5,8,0};
int i;
int size = sizeof(arr)/sizeof(arr[0]); heapSort(arr, size);
printf("The sorted elements are : \n\n"); for (i=0; i<size; ++i);
printf("%d\n",arr[i]);
}
Program 16:
Given S1={“Flowers”}; S2={“are beautiful”}; I.Find the length of S1 . II. Concatenate S1 and S2
.
III. Extract the substring “low” from S1. IV. Find “are ” in S2 and replace it with “is”.
#include <stdio.h>
while(s2[j]!='\0'){
result[i] = s2[j];
i++;
j++;
}
result[i] = '\0';
}
}
i++;
}
}
result[j]= '\0';
}
int main()
{
char S1[] = "Flowers";
char S2[] = "are beautiful";
char concatResult[50], substringresult[10],repalceResult[50];
void main(){
int matrix[MAX][MAX] = {0};
int vertices , edges, i,src,dest,choice;
//get the number of vertices and edges
printf("enter the number of vertices:");
scanf("%d", &vertices);
printf("enter the number of edges");
scanf("%d", &edges);
//input edges
for(i = 0;i<edges; i++){
printf("enter edges(source destination):");
scanf("%d %d" , &src, &dest);
matrix[src-1][dest-1] = 1;
if(choice == 2){
matrix[dest-1][src-1] = 1;
}
}
//display the adjacency matrix
displayMatrix(matrix, vertices);
}
Program 18:
Write a program to insert/retrieve an entry into hash from a hash table .with open addressing
using linear probing .
#include <stdio.h>
#include <stdlib.h>
int hashTable[TABLE_SIZE];
//HASH FUNCTION
int hashFunction(int key){
return key % TABLE_SIZE;
}
//insert function using linear probing
void insert(int key){
int index = hashFunction(key);
int originalIndex = index;
void display(){
int i;
printf("\n Hash Table:\n");
for(i = 0; i< TABLE_SIZE;i++){
if(hashTable[i] == EMPTY)
{
printf("Index %d : EMPTY\n" , i);
}
else
{
printf("Index %d : %d\n", i , hashTable[i]);
}
}
printf("\n");
}
int main(){
int num , key , i;
display();
else
return 0;
}