IZee Business School 2025
Data Structures Lab
1. Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear search and display its first
occurance.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,x;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&x);
for(i=0;i<n;++i)
{
if(a[i]==x)
break;
}
if(i<n)
printf("\nElement is found at position : %d\n",i+1);
else
printf("\nEntered element not found\n");
}
Output
Enter the size of the array : 5
Enter 5 elements to be stored in the array : 5 6 8 5 7
Enter the element to be searched : 5
Element is found at position : 1
Asst. Prof. Jincy Emmanuel Page 1
IZee Business School 2025
2. Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and display its first
occurance.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,low,high,mid,n,key;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n", n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(a[mid] < key)
low = mid + 1;
else if (a[mid] == key)
{
printf("\nElement found at position : %d\n",mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("\nEntered element not found\n");
}
Output
Enter the size of array : 8
Enter 8 elements to be stored in the array : 4 7 3 2 1 7 9 0
Enter the element to be searched : 7
Element found at position : 6
Asst. Prof. Jincy Emmanuel Page 2
IZee Business School 2025
3. Write a recursive program to find GCD of 4,6,8.
#include<stdio.h>
#include<conio.h>
int gcd(int m,int n)
{
if(n==0)
{
return(m);
}
else if(n>m)
{
return(gcd(n,m));
}
else
{
return(gcd(n,m%n));
}
}
void main()
{
int k,m,n;
clrscr();
printf("Enter three numbers : \n");
scanf("%d %d %d",&k,&m,&n);
printf("\nGCD of(%d,%d,%d) = %d\n",k,m,n,gcd(k,gcd(m,n)));
getch();
}
Output
Enter three numbers: 4 6 8
GCD of (4,6,8) = 2
Asst. Prof. Jincy Emmanuel Page 3
IZee Business School 2025
4. Given {5,3,1,6,0} order the numbers in ascending order using Bubble sort
Algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,swap;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j] > a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
}
printf("\nSorted Array : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
Output
Enter the size of array : 7
Enter 7 elements to be stored in the array : 7 8 6 5 4 3 6
Sorted Array : 3 4 5 6 6 7 8
Asst. Prof. Jincy Emmanuel Page 4
IZee Business School 2025
Or
# include<stdio.h>
# include<conio.h>
void BUBBLE_SORT(int a[], int n)
{
int pass,temp,j,i;
for(pass=1;pass<=n-1;pass++)
{
for(j=0;j<=n-pass-1;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\n Array after %d pass --->",pass);
for(i=0;i<n;i++)
printf("%3d",a[i]);
}
}
void main()
{
int a[7]={5,3,1,6,0,2,4};
int n=7;
clrscr();
printf("\n Input arrays : 5 3 1 6 0 2 4");
BUBBLE_SORT(a,n);
getch();
}
Output
Input arrays : 5 3 1 6 0 2 4
Array after 1 pass ---> 3 1 5 0 2 4 6
Array after 2 pass ---> 1 3 0 2 4 5 6
Array after 3 pass ---> 1 0 2 3 4 5 6
Array after 4 pass ---> 0 1 2 3 4 5 6
Array after 5 pass ---> 0 1 2 3 4 5 6
Array after 6 pass ---> 0 1 2 3 4 5 6
Asst. Prof. Jincy Emmanuel Page 5
IZee Business School 2025
5. Perform the Insertion sort on the input {7,5,8,1,16,48,3,7,0} and display the output
in descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,size,swap;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&size);
printf("\nEnter %d elements to be stored in the array : \n",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
for(i=1;i<(size);i++)
{
j=i;
while(j>0 && a[j]>a[j-1])
{
swap=a[j];
a[j]=a[j-1];
a[j-1]=swap;
j--;
}
}
printf("\nSorted array : ");
for(i=0;i<size;i++)
printf("%d ",a[i]);
printf("\n");
}
Output
Enter the size of the array : 4
Enter 4 elements to be stored in the array : 6 7 5 8
Sorted array : 8 7 6 5
Asst. Prof. Jincy Emmanuel Page 6
IZee Business School 2025
6. Perform the Selection sort on the input {7,5,8,1,16,48,3,7,0} and display the output
in descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,position,swap;
//clrscr();
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
position=i;
for(j=i+1;j<n;j++)
{
if(a[position]<a[j])
position=j;
}
if(position!=i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("\nSorted Array : ");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
}
Output
Enter the size of the array : 5
Enter 5 elements to be stored in the array : 7 8 6 5 9
Sorted Array : 9 8 7 6 5
Asst. Prof. Jincy Emmanuel Page 7
IZee Business School 2025
7. 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]);
Asst. Prof. Jincy Emmanuel Page 8
IZee Business School 2025
heapSort(arr, size);
printf("The sorted elements are : \n\n");
for (i=0; i<size; ++i)
printf("%d\n",arr[i]);
}
Output
The sorted elements are :
0
1
4
5
8
8
9
16
32
Asst. Prof. Jincy Emmanuel Page 9
IZee Business School 2025
8. Extract the substring “low” from S1 without using library functions.
#include<stdio.h>
void main()
{
char str[100], sstr[100];
int pos, l, c = 0;
printf("\n\nExtract a substring from a given string:\n");
printf("--------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
printf("Input the position to start extraction :");
scanf("%d", &pos);
printf("Input the length of substring :");
scanf("%d", &l);
while (c < l)
{
sstr[c] = str[pos+c-1];
c++;
}
sstr[c] = '\0';
printf("The substring retrieve from the string is : %s \n", sstr);
}
Output
Extract a substring from a given string:
--------------------------------------------
Input the string : flowers
Input the position to start extraction :2
Input the length of substring :3
The substring retrieve from the string is : low
Asst. Prof. Jincy Emmanuel Page 10
IZee Business School 2025
9. 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.
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void display()
{
int i;
if (front == - 1)
printf("\nQueue is empty \n");
else
{
}
printf("\nQueue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
//getch();
}
void insert_Q(int item)
{
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
}
if (front == - 1)
front = 0;
rear = rear + 1;
queue_array[rear] = item;
display();
}
void delete_Q()
{
if (front == - 1 || front > rear)
{
printf("\nQueue Underflow ");
return ;
Asst. Prof. Jincy Emmanuel Page 11
IZee Business School 2025
}
else
{
printf("\nElement deleted from queue is : %d", queue_array[front]);
front = front + 1;
}
display();
}
void main()
{
int choice;
//clrscr();
insert_Q(61);
insert_Q(16);
insert_Q(8);
insert_Q(27);
delete_Q();
delete_Q();
delete_Q();
//getch();
}
Output
Queue is : 61
Queue is : 61 16
Queue is : 61 16 8
Queue is : 61 16 8 27
Element deleted from queue is : 61
Queue is : 16 8 27
Element deleted from queue is : 16
Queue is : 8 27
Element deleted from queue is : 8
Queue is : 27
Asst. Prof. Jincy Emmanuel Page 12
IZee Business School 2025
10.Tower of hanoi c program using recursion
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("\nMove disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("\nMove disk %d from rod %c to rod %c", n, from_rod, to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
Output
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
Asst. Prof. Jincy Emmanuel Page 13
IZee Business School 2025
11.Adjacency Matrix Representation of a Graph
#include <stdio.h>
#define MAX_VERTICES 10
void createAdjMatrix(int adjMatrix[MAX_VERTICES][MAX_VERTICES], int vertices, int
edges) {
int i, j, src, dest;
// Initialize the adjacency matrix with zeros
for (i = 0; i < vertices; i++) {
for (j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}
// Input edges and update the adjacency matrix
for (i = 0; i < edges; i++) {
printf("Enter edge %d (source destination): ", i + 1);
scanf("%d %d", &src, &dest);
adjMatrix[src][dest] = 1;
adjMatrix[dest][src] = 1; // For undirected graph
}
}
void displayAdjMatrix(int adjMatrix[MAX_VERTICES][MAX_VERTICES], int vertices) {
int i, j;
printf("\nAdjacency Matrix:\n");
for (i = 0; i < vertices; i++) {
for (j = 0; j < vertices; j++) {
printf("%d ", adjMatrix[i][j]);
}
printf("\n");
}
}
int main() {
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
int vertices, edges;
Asst. Prof. Jincy Emmanuel Page 14
IZee Business School 2025
printf("Enter number of vertices: ");
scanf("%d", &vertices);
printf("Enter number of edges: ");
scanf("%d", &edges);
createAdjMatrix(adjMatrix, vertices, edges);
displayAdjMatrix(adjMatrix, vertices);
return 0;
}
Output
Enter number of vertices: 4
Enter number of edges: 3
Enter edge 1 (source destination): 0 1
Enter edge 2 (source destination): 2 3
Enter edge 3 (source destination): 1 2
Adjacency Matrix:
0100
1010
0101
0010
Asst. Prof. Jincy Emmanuel Page 15
IZee Business School 2025
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to push an element onto the stack
void push(Node** top, int data) {
Node* newNode = createNode(data);
newNode->next = *top;
*top = newNode;
printf("Pushed %d onto stack\n", data);
}
// Function to pop an element from the stack
int pop(Node** top) {
if (*top == NULL) {
printf("Stack Underflow\n");
return -1;
}
Node* temp = *top;
int poppedData = temp->data;
*top = (*top)->next;
free(temp);
return poppedData;
Asst. Prof. Jincy Emmanuel Page 16
IZee Business School 2025
// Function to peek at the top element of the stack
int peek(Node* top) {
if (top == NULL) {
printf("Stack is empty\n");
return -1;
}
return top->data;
}
// Function to check if the stack is empty
int isEmpty(Node* top) {
return top == NULL;
}
// Function to display the elements of the stack
void display(Node* top) {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
Node* temp = top;
printf("Stack elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Driver code
int main() {
Node* stack = NULL; // Initialize an empty stack
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
display(stack);
Asst. Prof. Jincy Emmanuel Page 17
IZee Business School 2025
printf("Popped: %d\n", pop(&stack));
display(stack);
printf("Top element is %d\n", peek(stack));
return 0;
}
Output
Pushed 10 onto stack
Pushed 20 onto stack
Pushed 30 onto stack
Stack elements: 30 20 10
Popped: 30
Stack elements: 20 10
Top element is 20
Asst. Prof. Jincy Emmanuel Page 18