Data Structures Lab
Data Structures Lab
LAB RECORD
NAME : S.SALMAN
RRN : 242812605014
BONAFIDE CERTIFICATE
---------------------------------------
Signature of LAB In-Charge
INDEX
Page
Ex.No. Date Program Title Sign
No.
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);
enqueue(40);
enqueue(50);
enqueue(60);
displayQ();
printf(“Front = %d, Rear = %d\n”, FRONT, REAR);
return 0;
}
Output
Result
Thus the output is successfully complied
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
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
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
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
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;
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
int main(){
while(will)
{
printf("Enter the array size: ");
scanf("%d",&count);
case 3:
Sorted = BubbleSort(number, count);
printf("\nSorted Array (using Bubble Sort)\n");
break;
default:
break;
}
Result
Thus the output is successfully complied
Aim
Data structures program to find BFS
Program
#include <iostream>
#include<vector>
#include<queue>
#include<stack>
edge(adj,2,4);
cout<<"BFS traversal is"<<" ";
Output
Result
Thus the output is successfully complied
edge(adj,2,4);
cout<<"BFS traversal is"<<" ";
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"<<" ";
Output
Result
Thus the output is successfully complied
Aim
Data structures program to find linear search
Program
#include <iostream>
using namespace std;
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
Result