0% found this document useful (0 votes)
20 views12 pages

Data Structure One To Ten

Dsa

Uploaded by

Barleen Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

Data Structure One To Ten

Dsa

Uploaded by

Barleen Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.Program for using Dynamic Functions (malloc(), calloc(), realloc() and free()) functions.

(i)malloc() function:
#include <stdio.h>
#include <stdlib.h> // malloc()
int main(){
int *ptr, num, i;
printf("Enter number of elements:");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int)); // type casted into integer data type
if (ptr == NULL){
printf("\nMemory not allocated.");
exit(0);
}
else{
printf("\nMemory successfully allocated using malloc.\n\n");
for (i = 0; i < num; ++i){
printf("Enter an element %d: ", i + 1);
scanf("%d", ptr + i);
}
printf("\nYou've entered: ");
for (i = 0; i < num; ++i) {
printf("%d, ", *(ptr + i));
}
}
return 0;
}

II calloc() funtion:
#include <stdio.h>
#include <stdlib.h> // calloc()
int main(){
int *ptr, num, i;
printf("Enter number of elements:");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int)); // type casted into integer data type
if (ptr == NULL){
printf("\nMemory not allocated.");
exit(0);
}
else{
printf("\nMemory successfully allocated using calloc.\n\n");
for (i = 0; i < num; ++i){
printf("Enter an element %d: ", i + 1);
scanf("%d", ptr + i);
}
printf("\nYou've entered: ");
for (i = 0; i < num; ++i) {
printf("%d, ", *(ptr + i));
}
}
return 0;
}

III realloc() funtion:


#include <stdio.h>
#include <stdlib.h> // realloc()
int main(){
int *ptr, num, i;
printf("Enter number of elements:");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if (ptr == NULL){
printf("\nMemory not allocated.");
exit(0);
}
else{
printf("Memory successfully allocated using calloc.\n\n");
for (i = 0; i < num; ++i){
printf("Enter the element %d: ", i + 1);
scanf("%d", ptr + i);
}
printf("\nYou've entered: ");
for (i = 0; i < num; ++i){
printf("%d, ", *(ptr + i));
}
printf("\n\nEnter the new size of the array:");
scanf("%d", &num);
ptr = (int *) realloc(ptr, num * sizeof(int));
printf("\nMemory successfully re-allocated using realloc\n\n.");
for (i = 0; i < num; ++i){
printf("Enter the element %d: ", i + 1);
scanf("%d", ptr + i);
}
printf("\nYou've entered: ");
for (i = 0; i < num; ++i) {
printf("%d, ", *(ptr + i));
}
free(ptr);
}
return 0;
}

IV free() funtion:
#include <stdio.h>
#include <stdlib.h> // free()
int main(){
int *ptrMalloc, *ptrCalloc, num, i;
printf("Enter number of elements:");
scanf("%d", &num);
ptrMalloc = (int*) malloc(num * sizeof(int));
ptrCalloc = (int*) calloc(num, sizeof(int));
if (ptrMalloc == NULL || ptrCalloc == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
free(ptrMalloc);
printf("Malloc Memory successfully freed.");
printf("\n\nMemory successfully allocated using calloc.");
free(ptrCalloc);
printf("\nCalloc Memory successfully freed.");
}
return 0;
}
2.Program to insert, delete and traverse an element from an array.

(i)Inserting an element into an array:


#include <stdio.h>
int main(){
int pos, i, n, value;
printf("Enter total number of elements in an array: ");
scanf("%d", &n);
int arr[n];
printf("\nEnter %d elements:\n", n);
for(i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
printf("\nEnter the position/index where you want to insert the element: ");
scanf("%d", &pos);
printf("Enter the value into that position: ");
scanf("%d", &value);
/* Working:
Array = 10 20 30 40 50
Index = 0 1 2 3 4
n=5
pos = 2nd
Iterations:
Index = 0 1 2 3 4 5
i = 4 10 20 30 40 50 50
i = 3 10 20 30 40 40 50
i = 2 10 20 30 30 40 50
i = 1 10 20 20 30 40 50
pos = 1 10 100 20 30 40 50 */
for(i = n - 1; i >= pos - 1; i--){
arr[i + 1] = arr[i]; }
arr[pos - 1] = value;
printf("\nFinal array after inserting the new value: ");
for(i = 0; i <= n; i++){
printf ("\narr[%d] = ", i);
printf("%d", arr[i]);
}
return 0;
}

ii Traversing into an array:


#include <stdio.h>
#include <conio.h>
int main(){
int arr[100], i, n;
printf("Enter total number of elements in an array: ");
scanf("%d", &n);
printf("\nEnter %d elements:\n", n);
for(i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
printf("\nTraversing the given array: ");
for(i = 0; i < n; i++){
printf ("\narr[%d] = ", i);
printf("%d", arr[i]);
}
return 0;
}

iii Deleting an element from an array:


#include <stdio.h>
#include <conio.h>
int main(){
int pos, i, n, value;
int ptr;
printf("Enter total number of elements in an array: ");
scanf("%d", &n);
int arr[n];
printf("\nEnter %d elements:\n", n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("\nEnter the position/index where you want to delete the element: ");
scanf("%d", &pos);
if (pos >= n + 1){
printf("\nDeletion is not possible in the array."); }
else{
/* Working:
Array = 10 15 20 30 40 50
Index = 0 1 2 3 4 5
n=6
pos = 2nd
So, pos = 2 - 1 = 1
Iterations:
Index = 0 1 2 3 4 5
i = 1 10 20 20 30 40 50
i = 2 10 20 30 30 40 50
i = 3 10 20 30 40 40 50
i = 4 10 20 30 40 50 50 */
for(i = pos - 1; i < n - 1; i++){
arr[i] = arr[i+1]; }
printf("\nFinal array after deleting the value:");
for (i = 0; i < n; i++){
printf ("\narr[%d] = ", i);
printf("%d", arr[i]);
}
}
return 0;
}

3. Program to merge one dimensional arrays.


# include <iostream>
using namespace std;
int main(){
int n1, n2;
cout<<"Enter total number of elements in 1st array: ";
cin>>n1;
int arr1[n1];
cout<<"\nEnter "<<n1<<" elements in 1st array:\n";
for(int i = 0; i < n1; i++){
cout<<"Enter element "<<i + 1<<" : ";
cin>>arr1[i];
}
cout<<"\nEnter total number of elements in 2nd array: ";
cin>>n2;
int arr2[n2];
cout<<"\nEnter "<<n2<<" elements in 2nd array:\n";
for(int i = 0; i < n2; i++){
cout<<"Enter element "<<i + 1<<" : ";
cin>>arr2[i];
}
int merged[n1 + n2];
// Copy elements from arr1 to merged
for (int i = 0; i < n1; i++){
merged[i] = arr1[i];
}
// Copy elements from arr2 to merged
for (int i = 0; i < n2; i++){
merged[n1 + i] = arr2[i];
}
// Display the merged array
cout<<"\nMerged Array: ";
for (int i = 0; i < n1 + n2; i++){
cout<<merged[i]<<" ";
}
return 0;
}

4.Program for addition and subtraction of two matrices.


#include <iostream>
using namespace std;
int main(){
int row1, col1, row2, col2;
cout<<"For Matrix A:";
cout<<"\nEnter number of rows: ";
cin>>row1;
cout<<"Enter number of columns: ";
cin>>col1;
cout<<"\nFor Matrix B:";
cout<<"\nEnter number of rows: ";
cin>>row2;
cout<<"Enter number of columns: ";
cin>>col2;
if (row1 == row2 and col1 == col2){
int mat1[row1][col1], mat2[row1][col1], sumMat[row1][col1], diffMat[row1][col1];
cout<<"\nFor Matrix A:\n";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"Enter element "<<i<<", "<<j<<": ";
cin>>mat1[i][j];
}
}
cout<<"\nFor Matrix B:\n";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"Enter element "<<i<<", "<<j<<": ";
cin>>mat2[i][j];
}
}
for(int i = 0; i < row1; i++){ // For Sum:
for(int j = 0; j < col1; j++){
sumMat[i][j] = mat1[i][j] + mat2[i][j];
}
}

for(int i = 0; i < row1; i++){ // For Difference:


for(int j = 0; j < col1; j++){
diffMat[i][j] = mat1[i][j] - mat2[i][j];
}
}
cout<<"\nMatrix A = ";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"\t"<<mat1[i][j];
}
cout<<"\n\t";
}
cout<<"\nMatrix B = ";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"\t"<<mat2[i][j];
}
cout<<"\n\t";
}
cout<<"\nMatrix A + Matrix B = ";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"\t"<<sumMat[i][j];
}
cout<<"\n\t\t";
}
cout<<"\nMatrix A - Matrix B = ";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"\t"<<diffMat[i][j];
}
cout<<"\n\t\t";
}
}
else{
cout<<"\nError ! Number of rows and columns must be equal for both the matrices.";
}
return 0;
}

5.Program for implementing multiplication of two matrices.


#include <iostream>
using namespace std;
int main(){
int row1, col1, row2, col2;
cout<<"For Matrix A:";
cout<<"\nEnter number of rows: ";
cin>>row1;
cout<<"Enter number of columns: ";
cin>>col1;
cout<<"\nFor Matrix B:";
cout<<"\nEnter number of rows: ";
cin>>row2;
cout<<"Enter number of columns: ";
cin>>col2;
if (col1 == row2){
int mat1[row1][col1], mat2[row2][col2], mulMat[row1][col2];
cout<<"\nFor Matrix A:\n";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"Enter element "<<i<<", "<<j<<": ";
cin>>mat1[i][j];
}
}
cout<<"\nFor Matrix B:\n";
for(int i = 0; i < row2; i++){
for(int j = 0; j < col2; j++){
cout<<"Enter element "<<i<<", "<<j<<": ";
cin>>mat2[i][j];
}
}

// For Multiplication:
for(int i = 0; i < row1; i++){
for(int j = 0; j < col2; j++){
mulMat[i][j] = 0;
for(int k = 0; k < row2; k++){
mulMat[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
cout<<"\nMatrix A = ";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
cout<<"\t"<<mat1[i][j];
}
cout<<"\n\t";
}
cout<<"\nMatrix B = ";
for(int i = 0; i < row2; i++){
for(int j = 0; j < col2; j++){
cout<<"\t"<<mat2[i][j];
}
cout<<"\n\t";
}
printArray(mulMat, row1, col1);
cout<<"\nMatrix A * Matrix B = ";
for(int i = 0; i < row1; i++){
for(int j = 0; j < col2; j++){
cout<<"\t"<<mulMat[i][j];
}
cout<<"\n\t\t";
}
}
else{
cout<<"\nError ! Number of columns of 1st matrix must be equal to the number of rows of the 2nd matrix.";
}
return 0;
}

6.Implement linear search using one - and two - dimensional array.


#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; ++i) {
if (arr[i] == key) {
return i; // Return the index where the key is found
}
}
return -1; // Return -1 if the key is not found
}
int main() {
int n, key, search;
cout<<"Enter total number of elements in an array: ";
cin>>n;
int arr[n];
cout<<"\n";
for(int i = 0; i < n; i++){
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
cout<<"\nEnter the value you want to search in the array: ";
cin>>key;
search = linearSearch(arr, n, key);
if (search != -1){
cout<<"\nElement found at index: "<<search;
}
else{
cout<<"\nElement not found !";
}
return 0;
}

7.Program for implementing selection sort.


#include <iostream>
using namespace std;
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] with the minimum element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
void printArray(int arr[], int n){
for (int i = 0; i < n; i++)
cout<<arr[i]<<" ";
}
int main() {
int n;
cout<<"Enter total number of elements in an array: ";
cin>>n;
int arr[n];
cout<<"\n";
for(int i = 0; i < n; i++){
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
// Print the original array
cout<<"\nOriginal Array: ";
printArray(arr, n);
selectionSort(arr, n);

// Print the sorted array


cout<<"\n\nSorted Array: ";
printArray(arr, n);
return 0;
}

8. Program for implementing insertion sort.


#include <iostream>
#include <math.h>
using namespace std;
void insertionSort(int arr[], int n){
int i, key, j;
for(i = 1; i < n; i++){
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n){
for (int i = 0; i < n; i++)
cout<<arr[i]<<" ";
}
int main(){
int n;
cout<<"Enter total number of elements in an array: ";
cin>>n;
int arr[n];
cout<<"\n";
for(int i = 0; i < n; i++){
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
cout<<"\nOriginal Array: ";

printArray(arr, n);
insertionSort(arr, n);
cout<<"\n\nSorted Array: ";
printArray(arr, n);
return 0;
}
9. Program for implementing quick sort.
// Smaller < Pivot < Larger
// Divide and Conquer Approach
#include <iostream>
using namespace std;
void swap(int arr[] , int pos1, int pos2){
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
int partition(int arr[], int low, int high, int pivot){
int i = low;
int j = low;
while(i <= high){
if(arr[i] > pivot){
i++; }
else{
swap(arr,i,j);
i++;
j++; }
}
return j-1;
}
void quickSort(int arr[], int low, int high){
if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high, pivot);
quickSort(arr, low, pos - 1);
quickSort(arr, pos + 1, high);
}
}
void printArray(int arr[], int n){
for (int i = 0; i < n; i++){
cout<<arr[i]<<" "; }

int main(){
int n ;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
cout<<"\n";
for( int i = 0 ; i < n; i++){
cout<<"Enter element "<<i + 1<<": ";
cin>> arr[i];
}
cout<<"\nOriginal Array: ";
printArray(arr, n);
quickSort(arr, 0 , n-1);
cout<<"\n\nSorted Array: ";
printArray(arr, n);
return 0;
}

10. Program for implementing merge sort.


#include <iostream>
using namespace std;
// function to merge the two half into a sorted data:
void Merge(int *a, int low, int high, int mid){
// already sorted low to mid and mid+1 to high
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
// Merge the two parts into temp[].
while(i <= mid && j <= high){
if (a[i] < a[j]){
temp[k] = a[i];
k++;
i++; }
else{
temp[k] = a[j];
k++;
j++; }
}
// insert into temp[] -> i to mid:
while(i <= mid){
temp[k] = a[i];
k++;
i++; }
// insert into temp[] -> j to high:
while(j <= high){
temp[k] = a[j];
k++;
j++; }
for (i = low; i <= high; i++){
a[i] = temp[i - low]; } // assign sorted data stored in temp[] to a[]
}
// function to split array into two parts:
void MergeSort(int *a, int low, int high){
int mid;
if (low < high){
mid = (low + high) / 2;

// Split the data into two half:


MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid); } // Merge to get sorted output
}
// function to print any array:
void printArray(int arr[], int n){
for (int i = 0; i < n; i++){
cout<<arr[i]<<" "; }
}
int main(){
int n ;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
cout<<"\n";
for( int i = 0 ; i < n; i++){
cout<<"Enter element "<<i + 1<<": ";
cin>> arr[i]; }
cout<<"\nOriginal Array: ";
printArray(arr, n);
MergeSort(arr, 0, n-1);
cout<<"\n\nSorted Array: ";
printArray(arr, n);
return 0;
}

You might also like