Data Structure One To Ten
Data Structure One To Ten
(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;
}
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.
// 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;
}
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;
}