CENTRE FOR DISTANCE AND ONLINE EDUCATION
LAB RECORD
NAME : S.SALMAN
RRN : 242812605014
LAB : Data structures lab
CENTRE FOR DISTANCE AND ONLINE EDUCATION
DEPARTMENT OF COMPUTER APPLICATIONS
CAE 6128–Data structures Lab
(I SEMESTER)
CENTRE FOR DISTANCE AND ONLINE EDUCATION
BONAFIDE CERTIFICATE
This is a Certified Record Book of RRN: 242812605014 submitted for
the Semester End Practical Examinations held on 28-02-2025, for the
CAE 6123-Data structures Lab during 2024-2025.
---------------------------------------
Signature of LAB In-Charge
INDEX
Page
Ex.No. Date Program Title Sign
No.
1 Data Structures program to find size of the variable
Data structures program to find deallocating of the
2 memory
3 data structures program to find size of the queue
4 data structures program to find dequeue of the queue
5 data structure program to find selection sort
6 data structure program to find insertion sort
7 data structures program to find bubble sort
8 data structure program to find Binary search
9 data structure program to find Binary tree operations
10 data structures program to find BFS
11 data structures program to find DFS
12 data structure program to find Linear search
13 data structures to find post order traversal
1. Write a Data Structures program to find size of the variable
Aim
Data Structures program to find size of the variable
Program
#include<stdio.h>
int main()
{
Int intType;
Float floatType;
double doubleType;
char charType;
printf(“Size of int: %zu bytes\n”, sizeof(intType));
printf(“Size of float: %zu bytes\n”, sizeof(floatType));
printf(“Size of double: %zu bytes\n”, sizeof(doubleType));
printf(“Size of char: %zu byte\n”, sizeof(charType));
return 0;
}
Output:
Result
Thus the output is successfully complied
2.Write a data structures program to find deallocating of the memory
Aim:
Data structures program to find deallocating of the memory
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Output
Result
Thus the output is successfully complied
3 Write a data structures program to find size of the queue
Aim
Data structures program to find size of the queue
Program
#include<stdio.h>
int QUEUE[size];
int FRONT = -1;
int REAR = -1;
int i;
int is QueueEmpty()
{
if(FRONT == REAR)
return 1;
return -1;
}
Void dequeue()
{
if(isQueueEmpty() == 1)
printf(“Queue is Empty.\n”);
else
{
printf(“Dequeued element = %d\n”,QUEUE[FRONT+1]);
FRONT ++;
}
}
int isQueueFull()
{
if(REAR == size)
return 1;
return -1;
}
Void enqueue(int val)
{
If(isQueueFull() == 1)
printf(“Queue is Full\n”);
else
{
QUEUE [++REAR] = val;
REAR ++;
}
}
void displayQ()
{
for(i=0;i<= REAR;i++)
{
printf(“%d->”, QUEUE[i]);}
printf(“End of Queue\n”);
}
int main()
{
printf(“Add items 10,20 and 30\n”);
enqueue(10);
enqueue(20);
enqueue(30);
displayQ();
printf(“Front = %d, Rear = %d\n”, FRONT,REAR);
printf(“Delete two items from queue\n”);
enqueue(10);
enqueue(20);
enqueue(30);
displayQ();
printf(“Front = %d, Rear = %d\n”, FRONT,REAR);
printf(“Delete two items from queue\n”);
dequeue();
dequeue();
displayQ();
printf(“Front = %d, Rear = %d\n”, FRONT,REAR);
printf(“Add items 40,50 and 60\n”);
enqueue(40);
enqueue(50);
enqueue(60);
displayQ();
printf(“Front = %d, Rear = %d\n”, FRONT, REAR);
printf(“Delete five items from Queue\n”);
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
return 0;
}
Output
Result
Thus the output is successfully complied
4. Write a data structures program to find dequeue of the queue
Aim
Data structures program to find dequeue of the queue
Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL, *rear = NULL;
void enqueue(int val)
{
struct node *newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = val;
newNode->next =NULL;
if(front == NULL && rear == NULL)
front = rear = newNode;
else
{
rear->next =newNode;
rear->newNode;
}
}
void dequeue()
{
Struct node *temp;
if(front == NULL)
printf(“Queue is Empty, unable to perform dequeue\n”);
else
{
temp->front;
front->front->next;
rear->NULL;
free(temp);
}
}
void printlist()
{
struct node *temp= front;
While(temp)
{
printf(“%d->”,temp->data);
temp = temp->next;
}
printf(“NULL\n”);
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
printf(“Queue :”);
printList();
dequeue();
printf(“After dequeue the new Queue :”);
dequeue();
printf(“After dequeue the new Queue :”);
printList();
return 0;
}
Output
Result
Thus the output is successfully complied
5 .Write a data structure program to find selection sort
Aim
Data structures Program to find selection sort
Program
#include<stdio.h>
#include<stdlib.h>
int * selection sort(int a[], int n)
{
int i, j, temp;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Return a;
}
Output
Result
Thus the output is successfully complied
6. Write a data structure program to find insertion sort
Aim
Data structure program to find insertion sort
Program
#include<stdio.h>
#include<stdlib.h>
int * selection sort(int a[], int n)
{
int i, j, temp;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Return a;
}
Int * InsertionSort(int a[], int n)
{
int i, j, temp;
for(i=1;i<n-1;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1}=a[j];
j=j-1;
}
a[j+1]=temp;
}
Return a;
}
Output
Result
Thus the output is successfully complied
7. Write a data structures program to find bubble sort
Aim
Data structures program To find bubble sort
Program
Int * BubbleSort(int a[], int n)
{
int i, j, temp;
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(a[j] >a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
Output
Result
Thus the output is successfully complied
8. Write a data structure program to find Binary search
Aim
Data structure program to find binary search
Program
#include <stdio.h>
int main()
{
int c,first,last,middle,n,search,array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(c=0;c<=n;c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;
while(first <= last){
if(array[middle] <search)
first=middle+1;
else if (array[middle]== search){
printf("%d found at the location %d\n",search,middle+1);
break;
}
else
last=middle-1;
middle=(first + last)/2;
}
if(first>last)
printf("Not found!%d isnt present in the list\n",search);
return 0;
}
}
Output
Result
Thus the output is successfully complied
9. Write a data structure program to find Binary tree operations
Aim
Data structures program to find binary tree operations
Program
#include<stdio.h>
#include<stdlib.h>
int * SelectionSort(int a[], int n) //Returns array address
{
int i, j, temp;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
int * InsertionSort(int a[], int n)
{
int i, j, temp;
for(i=1;i<n;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
return a;
}
int * BubbleSort(int a[], int n)
{
int i, j, temp;
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (a[j] > a[j+1]) /* For decreasing order use '<' instead of '>' */
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
int main(){
int *Sorted, i, j, count, number[25], op;
int will = 1;
printf("\t\t\tIMPLEMENTATION OF SORTING ALGORITHMS\n");
printf("\t\t\t\t***************\n");
while(will)
{
printf("Enter the array size: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// Loop to get the elements stored in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);
printf("\nWhich sort do you want to perform (select 1-3)? ");
printf("\n1. Selection Sort\n2. Insertion Sort\n3. Bubble Sort\n");
scanf("%d",&op);
switch(op){
case 1:
Sorted = SelectionSort(number,count);
printf("\nSorted Array (using Selection Sort)\n");
break;
case 2:
Sorted = InsertionSort(number,count);
printf("\nSorted Array (using Insertion Sort)\n");
break;
case 3:
Sorted = BubbleSort(number, count);
printf("\nSorted Array (using Bubble Sort)\n");
break;
default:
break;
}
printf("\n\nSorted elements: ");
for(i=0;i<count;i++)
printf(" %d",Sorted[i]);
printf("\nDo you want to try another sorting method (0/1)?");
scanf("%d",&will);
}
return 0;
}
Output
Result
Thus the output is successfully complied
10. Write a data structures to find BFS
Aim
Data structures program to find BFS
Program
#include <iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
//add the edge in graph
void edge(vector<int>adj[],int u,int v){
adj[u].push_back(v);
}
void bfs(int s,vector<int>adj[],bool visit[]){
queue<int>q;//queue in STL
q.push(s);
visit[s]=true;
while(!q.empty()){
int u=q.front();
cout<<u<<" ";
q.pop();
for(int i=0;i<adj[u].size();i++){
if(!visit[adj[u][i]]){
q.push(adj[u][i]);
visit[adj[u][i]]=true;
}
}
}
}
edge(adj,2,4);
cout<<"BFS traversal is"<<" ";
Output
Result
Thus the output is successfully complied
11. Write a data structures to find DFS
Aim
Data structures program to find DFS
Program
#include <iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
//add the edge in graph
void edge(vector<int>adj[],int u,int v){
adj[u].push_back(v);
}
void bfs(int s,vector<int>adj[],bool visit[]){
queue<int>q;//queue in STL
q.push(s);
visit[s]=true;
while(!q.empty()){
int u=q.front();
cout<<u<<" ";
q.pop();
for(int i=0;i<adj[u].size();i++){
if(!visit[adj[u][i]]){
q.push(adj[u][i]);
visit[adj[u][i]]=true;
}
}
}
}
edge(adj,2,4);
cout<<"BFS traversal is"<<" ";
void dfs(int s,vector<int>adj[],bool visit[]){
stack<int>stk;//stack in STL
stk.push(s);
visit[s]=true;
while(!stk.empty()){
int u=stk.top();
cout<<u<<" ";
stk.pop();
//loop for traverse
for(int i=0;i<adj[u].size();i++){
if(!visit[adj[u][i]]){
stk.push(adj[u][i]);
visit[adj[u][i]]=true;
}
}
}
}
int main(){
vector<int>adj[5];
bool visit[5];//array to check visit or not of a node
//initially all node are unvisited
for(int i=0;i<5;i++){
visit[i]=false;
}
//input for edges
edge(adj,0,2);
edge(adj,0,1);
edge(adj,1,3);
edge(adj,2,0);
edge(adj,2,3);
edge(adj,2,4);
cout<<"BFS traversal is"<<" ";
//call bfs funtion
bfs(0,adj,visit); //1 is a starting point
cout<<endl;
//again initialise all node unvisited for dfs
for(int i=0;i<5;i++){
visit[i]=false;
}
cout<<"DFS traversal is"<<" ";
//call dfs function
dfs(0,adj,visit);//1 is a starting point
}
Output
Result
Thus the output is successfully complied
12. Write a data structure program to find linear search
Aim
Data structures program to find linear search
Program
#include <iostream>
using namespace std;
int LinearSearch(int arr[], int low, int high, int x)
{
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if x is present at mid
if (arr[mid] == x)
return mid;
// If x greater, ignore left half
if (arr[mid] < x)
low = mid + 1;
// If x is smaller, ignore right half
else
high = mid - 1;
}
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = LinearSearch(arr, 0, n - 1, x);
if(result == -1) cout << "Element is not present in array";
else cout << "Element is present at index " << result;
return 0;
}
Output
Result
Thus the output is successfully complied
13. Write a data structures program to find post order traversal
Aim
Data structures program to find post order traversal
Program
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int data)
{
// Allocate memory for the new node
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
// Initialize node data and children pointers
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void postorderTraversal(struct Node* root)
{
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}
int main()
{
struct Node* root = NULL;
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
printf("Postorder traversal of the binary tree is:\n");
postorderTraversal(root);
printf("\n");
return 0;
}
Output
Result
Thus the output is successfully complied