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...

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)                 {         ...