1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Rep Power

    C: Enumeration in a struct

    Define enumeration E with labels folder and file.
    Define struct folder that represents node of a tree with fields:
    name, (E)identifier, and left-right child.
    Define struct file which represents node of a tree without children and has fields:
    Store a folder in a tree by folder name.
    Display the read data of a tree.

    Is this correct declaration of an enumeration and structs:
    typedef enum
    typedef struct node
        char *str;
        TYPE ident;
        struct node *left,*right;
    typedef struct
        char *file_name;
        TYPE ident;
        char *file_content;
    How to store this data structure to binary tree?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    I named the file indicated by your code as e.c and then compiled with gcc via make to an object

    cc -Wall -g -c -o e.o e.c

    and there were no errors! You could have done that. The start of your program looks ok.

    As to storing it in a binary tree you ought to choose some feature to order the tree. Otherwise were you to, for instance, just stick everything on the right you'd have nothing more than a memory hungry linked list. Gosh, what happens if you load a binary tree with a pre-sorted list? Well, it's better in some ways perhaps than a random singly linked list. If you want extra credit implement a balanced binary tree, for instance the red-black tree.

    Pattern matching - Rosetta Code
    The j entry, which I wrote, has a reference.
    Last edited by b49P23TIvg; January 17th, 2016 at 07:57 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Rep Power
    An enum and a struct are totally different concepts, fulfilling different purposes.

    An enum lets you declare a series of identifiers for use in your code. The compiler replaces them with numbers for you. It's often useful for making your code more readable and maintainable, because you can use descriptive names without the performance penalty of string comparisons. It can also make the code less bug-prone because you don't have to keep writing in specific numbers everywhere, which could go wrong if a number changes.

    A struct is a data structure. At its simplest, it contains zero or more pieces of data (variables or objects), grouped together so they can be stored, processed, or passed as a single unit. You can usually have multiple copies (or instances) of it. A struct can be a lot more complex though. It's actually exactly the same as a class, except that members are public by default instead of private. Like a class, a struct can have member functions and template parameters and so on.

    One of the vital difference between structs and enums is that an enum doesn't exist at run-time. It's only for your benefit when you're read/writing the code. However, instances of structs (and classes) certainly can exist in memory at runtime.

    From a coding standpoint, each identifier in an enum doesn't have its own type. Every member within a struct must have a type.

    Learn C++ here https://hackr.io/tutorials/learn-c-c-plus-plus

IMN logo majestic logo threadwatch logo seochat tools logo