#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    How does one abstract data in C?


    I am trying to write a binary tree, I am not having any trouble with the tree, but right now I have it using public variables. How would I abstract it so that the user can have as much tree's as she wants? It would be easy using a class, but this is C. This is the insert code so far. After inserting all the values.. herbz has Jane's name, how would I go back to the top of the tree though? to print it all out for example.

    Code:
    //main.c
    #include <stdio.h> 
    #include "benz.h" 
    
    int main() {
    	node *herbz=NULL; //herbz will be our tree. 
    
    	//Let's insert some values. 
    	herbz=insert(herbz, "Biff", 1); 
    	herbz=insert(herbz, "Eddie", 2); 
    	herbz=insert(herbz, "Joe", 3); 
    	herbz=insert(herbz, "Jake", 4); 
    	herbz=insert(herbz, "Jane", 5); 
    	return 0; 
    }
    
    
    
    //benz.h
    #ifndef _BENZ_DOT_H_
    #define _BENZ_DOT_H_
    
    //Includes. 
    #include <stdlib.h> //malloc/free 
    #include <string.h> //strcpy()
    
    //Declares. 
    struct _node {
    	char *name; 
    	int age; 
    	struct _node *left, *right; 
    };
    
    typedef struct _node node; 
    
    //Prototypes. 
    char *dUbBz(char *dUn); 
    node *insert(node *Tree, char *name, int age); 
    #endif
    
    
    
    //benz.c
    #include "benz.h"
    
    char *dUbBz(char *dUn) {
    	char *abc=(char*)malloc((strlen(dUn)+1)*sizeof(char)); 
    	if(!abc) return NULL; 
    
    	strcpy(abc, dUn); 
    	return dUn; 
    }
    
    node *insert(node *Tree, char *name, int age) {
    	if(!Tree) {
    		Tree=(node*)malloc(sizeof(node)); 
    		if(!Tree) return NULL; 
    
    		//Insert info. 
    		Tree->name=dUbBz(name); 
    		Tree->age=age; 
    		Tree->left=NULL;
    		Tree->right=NULL; 
    	} else {
    		if(age >= Tree->age) 
    			return insert(Tree->right, name, age); 
    		else 
    			return insert(Tree->left, name, age); 
    	}
    	return Tree; 
    } //insert
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    how about saving a copy of the root node, which seems like it will be 'Biff'.
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I tried that, well here is the code I tried:

    Code:
    	node *root; 
    	//Let's insert some values. 
    	herbz=insert(herbz, "Biff", 1); 
    	root=herbz; 
    	herbz=insert(herbz, "Eddie", 2); 
    	herbz=insert(herbz, "Joe", 3); 
    	herbz=insert(herbz, "Jake", 4); 
    	herbz=insert(herbz, "Jane", 5);
    then after inserting all the values,

    printf("%s %d\n", root->right->name, root->right->age);

    should be Eddie, no? It just crashes.

    But is there any other way so that the user only needs 'herbz' and no other variables? (Like to go to the top of the tree from inside the function).
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    you're passing the node pointers by value instead of reference. thus a copy is being made and the original pointer is untouched. instead:
    Code:
    node *insert(node **Tree, char *name, int age);
    and then
    Code:
    herbz=insert( &herbz, "Biff", 1);
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    To answer your question more generally:

    Implementing an Object Oriented Design in C is not impossible or difficult. The difference between doing it in C and C++ is that C does nor directly support OOP, so a little more effort is required.

    C++ classes have an implicit 'this' pointer that is passed to each member function. In C you have to do this explicitly. A class in C is implemented as a structure and a set of functions, each of the functions takes a pointer to the structure, so that it deals with instance data rather than class data. You also have to do construction and destruction explicitly. To provide the scope restrictions that C++ classes provide, such classes in C should be implemented in separate compilation units (i.e. a separate source file). Private static member data can be implemented as static data within the classes source module. Static member functions simply do not require the explicit 'this' pointer.

    There are many more techniques to implementing OOD in C. Try this atricle: Implementing object-oriented designs in ANSI-standard C

    Clifford
  10. #6
  11. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Thanks infamous41md. Will play with that soon.



    Thanks for the link, I didn't really want to try to implement OOP in C, but I just wanted a way to allow the user to have as much binary tree's as he/she wants, by including only 1 header.

IMN logo majestic logo threadwatch logo seochat tools logo