Skip to main content

Posts

Showing posts from April, 2019

AVL tree and other homeworks

AVL Tree: #include<stdio.h> #include<stdlib.h> // An AVL tree node struct Node {     int key;     struct Node *left;     struct Node *right;     int height; }; int max(int a, int b); // A utility function to get the height of the tree int height(struct Node *N) {     if (N == NULL)         return 0;     return N->height; } // A utility function to get maximum of two integers int max(int a, int b) {     return (a > b)? a : b; } /* Helper function that allocates a new node with the given key and     NULL left and right pointers. */ struct Node* newNode(int key) {     struct Node* node = (struct Node*)                         malloc(sizeof(struct Node));     node->key   = key;     node->left   = NULL;     node->right  = NULL;     node->height = 1;  // new node is initially added at leaf     return(node); } // A utility function to right rotate subtree rooted with y // See the diagram given above. struct Node *ri

Memory management

#include <stdio.h> #include <stdlib.h> int main() {         int* ptr;         int* ptr1;         int n; int i;int sum = 0;         n = 5;         printf("Number of elements: %d", n);         ptr = (int*)malloc(n * sizeof(int));         ptr1 = (int*)calloc(n, sizeof(int));         if (ptr == NULL || ptr1 == NULL)         {             printf("\nMemory not allocated.");         }         else         {                 printf("\n\nMemory successfully allocated using malloc.");                 for (i = 0; i < n; ++i)                 {                     ptr[i] = i + 1;                 }                 printf("\nThe elements of the first array (using malloc) are: ");                 for (i = 0; i < n; i++)                 {                     printf("%d ", ptr[i]);                 }                 printf("\nThe elements of the second array (using calloc") are: ");