C cannot have static structure members

    by nikoo28

    In C, a structure cannot have static members, but in C++ a structure can have static members.

    For example, following program causes compilation error in C, but works in C++.

    #include<stdio.h>
    
    struct test
    {
        static int i;  // Error in C, but works in C++.
    };
    
    int main(void)
    {
        struct test t;
    
        return 0;
    }
    

    Feel free to add your opinion.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Symbol Table Implementations

    by nikoo28
    2 minutes read

    Symbol Tables can be implemented in many ways and here are some of them. Unordered Array Implementation With this method, just maintaining an array is enough. It needs O(n) time for searching, insertion and deletion in the worst case. Ordered [Sorted] Array Implementation In this we maintain a sorted array of keys and values. Store in sorted order by key keys[i] = ith largest key values[i] = value associated with ith largest key Since the elements are sorted and stored in arrays, we can use simple binary search for finding …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Symbol Tables

    by nikoo28
    2 minutes read

    Since childhood, we have all used a dictionary, and many of us have used a word processor (say Microsoft WORD) which comes with a spell checker. The spell checker is also a dictionary but limited. There are many real time examples for dictionaries and few of them are:- Spelling checker The data dictionary found in database management applications Symbol tables generated by loaders, assemblers, and compilers Routing tables in networking companies (DNS Lookup) In computer science, we generally use the term symbol table rather than dictionary, when referring to the …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the LCA (Least Common Ancestor) of two nodes in the tree. Input: Sample Tree (Pointer to node 1 is given). Find LCA of node 5 and 4 Output: Answer = 3 A simple recursive approach can be used to find the LCA (Least common ancestor). #include<stdio.h> #include<malloc.h> struct binaryTreeNode{ int data; struct binaryTreeNode * left; struct binaryTreeNode * right; }; struct binaryTreeNode * LCA(struct binaryTreeNode * root, struct binaryTreeNode * a, struct binaryTreeNode * b) { struct binaryTreeNode * left, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given 2 binary trees, determine if they are mirror of each other Input: 2 tree pointers are given. (Root pointer) Output: TRUE We can again apply the recursive sub-structure property of the tree here. The algorithm can be implemented as:- #include<stdio.h> #include<malloc.h> struct binaryTreeNode{ int data; struct binaryTreeNode * left; struct binaryTreeNode * right; }; int areTreeMirrors(struct binaryTreeNode * root1, struct binaryTreeNode * root2) { // Terminating condition if(root1 == NULL && root2 == NULL) return 1; //If one of them is NULL, then they are definitely not mirrors …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the sum of all its elements without using recursion. Input: Sample Tree (Pointer to node 1 is given). Output: Sum = 15 We discussed how to find the sum of all elements of a binary tree using recursion in this post. Given a binary tree, find the sum of all elements. If recursion seems tough to you , you can always go by the iterative approach and use Level Order Traversal of binary tree. While performing a level order traversal, …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More