Tree Problem

This is a discussion on Tree Problem within the C Programming forums, part of the General Programming Boards category; > char d; You're on the right track here Look at previous posts, and what a single phone number was ...

  1. #31
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > char d;
    You're on the right track here
    Look at previous posts, and what a single phone number was stored in.

    Then make your element store one of those.

  2. #32
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    so i should go back to storing my telephone numbers as arrays in a list structure? would that not defy the point of a list?

  3. #33
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    No - like this
    Code:
    typedef struct linked_list 
    {
        char phone[20];
        struct linked_list *next;
    } Element;

  4. #34
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    ok ive done that , where else am i going wrong?

    here is my code: (i have included the errors my compiler is giving me by commenting them into the relevent places) :

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct tree
    {
        char name[20];
        char *Element;
        struct tree *left;
        struct tree *right;
    }Tree;  
    
    typedef struct linked_list 
    {
        char d[20];
        struct linked_list *next;
    } Element;
    
    Element* insert( char h, Element *t )
    {
    	Element* y = calloc( 1, sizeof( Element ) );
    	y->d = h;              //ERROR: INCOPATIBLE TYPES IN ASSIGNMENT AT TOP LEVEL
        if( t ) t->next = y;  
        return y;
    } 
    
    Tree *makenode( char *in, char *in2, Tree *l, Tree *r)
    {
        Tree *t=malloc(sizeof(Tree));
        t->left =l;
        t->right =r;
        strncpy(t->name,in,19);
        /*Statement to copy pointer to linked list of phone numbers into tree (someone help)*/
        return t;
    }    
    
    Tree *insert( Tree *root, char *what, char *what2) //ERROR:CONFLICTING TYPES FOR INSERT
    {
        if(root==NULL)
        {root=makenode(what,Element,NULL,NULL);}   //PARSE ERROR BEFORE ELEMENT
        else if(strcmp(what, root->name)<0)
        {root->left =insert(root->left,what,Element);} //PARSE ERROR BEFORE ELEMENT
        else{root->right =insert(root->right,what,Element); //PARSE ERROR BEFORE ELEMENT
    }
    return root;
    }     
    
    char *search(Tree *root, char *what)
    {
        if(root==NULL){return "NOT FOUND";}
        else if(strcmp(what,root->name)==0) {return Element;} //PARSE ERROR BEFORE ELEMENT
        else if(strcmp(what,root->name)<0) {return search(root->left,what);}
        else{return search(root->right,what);}
    }
    
    int main(void)
    {
        int i;
        char s[20];
        char d;
        Tree *tree =NULL;
        Element* head = NULL;
        Element* tail = NULL;
        Element* next;
       do{
            scanf("%s",s);
    	if(strcmp(s,".")==0)
    	{break;}
    	scanf("%s",&d);
    	for(i=0;i<20;i++)
        {s[i]=toupper(s[i]);}
       
        if(strcmp(s,".")!=0);
            {
              tree=insert(tree, s,*Element); //PARSE ERROR BEFORE ELEMENT
              tail = insert( d, tail ); //WARNING: PASSING ARG1 of 'insert' makes pointer without
                  if( !head )              //cast
                  {head = tail; } 
            }
        }while(strcmp(s,".")!=0);
    	do{
    		printf("Type a name name please ");
            scanf("%s",s);
            for(i=0;i<20;i++)
            {s[i]=toupper(s[i]);}
            if(strcmp(s,".")!=0)
            {     
        for( ; head ; head = next ) {
        printf( "%c", head->d );
        next = head->next;
        free( head );}
            }
          }while(strcmp(s,".")!=0);
    return 0;
    }
    thank you

  5. #35
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if you read your code and then read the error, you should be able to figure these out for yourself:
    Code:
    typedef struct linked_list 
    {
        char d[20];
        struct linked_list *next;
    } Element;
    
    Element* insert( char h, Element *t )
    {
    	Element* y = calloc( 1, sizeof( Element ) );
    	y->d = h;              //ERROR: INCOPATIBLE TYPES IN ASSIGNMENT AT TOP LEVEL
    See? The first one is an array (d), and the second one is a single character (h). You need to assign that character to a specific spot in the array. Not just to the array name (which is treated as a pointer to the array in this case).
    Code:
    Element* insert( char h, Element *t )
    {
        ...stuff...
    }
    
    ...
    
    Tree *insert( Tree *root, char *what, char *what2) //ERROR:CONFLICTING TYPES FOR INSERT
    {
        ...stuff...
    }
    You are trying to make two functions called the same thing. You can't do that in C. Fix those, then try your code again.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #36
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    i'm still not sure how i should change my insertlist function , this is what ive done, but its wrong, "subscripted value is neither array nor pointer"

    Code:
     Element* insertlist( char h, Element *t )
    {
    	int i;
        Element* y = calloc( 1, sizeof( Element ) );
    	for(i=0;i<20;i++)
    	{int b;
        b=0;
        y->d = h[b];
        b++;             
        if( t ) t->next = y;}  
        return y;
    }
    any help is appreciated. thanks

  7. #37
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't understand:

    h is one single character.

    That means it is a single letter or number, etc. You don't subscript it. You simply assign that one single character to one single spot in the array.

    If it is supposed to be more than one single character, then you'll need to pass an array of characters to the function instead:
    Code:
    void foo( char bar[] )
    {
        char baz[10];
        ...copy the array bar into baz...
    }
    
    int main( void )
    {
        char array[] = "Hello!";
        foo( array );
    
        return 0;
    }
    Quick example of what I'm talking about. What you are doing is passing one character, not an array.

    Quzah.
    Hope is the first step on the road to disappointment.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undirected graph and Binary Tree problem
    By arafat21 in forum C Programming
    Replies: 3
    Last Post: 05-02-2008, 06:52 AM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. AVL tree balancing problem
    By sweets in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2004, 01:17 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21