BST struct prob

This is a discussion on BST struct prob within the C Programming forums, part of the General Programming Boards category; this is a code for insert in bst that i have written but while inserting third element in the tree ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    BST struct prob

    this is a code for insert in bst that i have written
    but while inserting third element in the tree the program crashes
    and i found out the error is in red statement if i remove it the program runs as normal
    can someone help me in removing it and letting me understand what is this error.
    Code:
    struct def
    
    
    struct tree
    {
           int data,color;
           struct tree *left, *right,*root;
           
    };
    
    typedef struct tree* RB;
    
    below is erroneous insert routine
    
    int rb_insert(int val,RB *node,RB top)
    {
               RB temp;
               if(*node==NULL)
               {
                  temp=(RB)malloc(sizeof(RB));
                  temp->root=top;
                  temp->left=NULL;
                  temp->right=NULL;
                  temp->data=val;
                  *node=temp;
                  return;
               }
               
               if((*node)->data>=val)
               {
                   rb_insert(val,&(*node)->left,*node);
               }
               else
               {
                   rb_insert(val,&(*node)->right,*node);
               }
    return ;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ketav View Post
    Code:
    struct def
    
    
    struct tree
    {
           int data,color;
           struct tree *left, *right,*root;
           
    };
    
    typedef struct tree* RB;
    
    below is erroneous insert routine
    
    int rb_insert(int val,RB *node,RB top)
    {
               RB temp;
               if(*node==NULL)
               {
                  temp=(RB)malloc(sizeof(RB));
                  temp->root=top;
                  temp->left=NULL;
                  temp->right=NULL;
                  temp->data=val;
                  *node=temp;
                  return;
               }
               
               if((*node)->data>=val)
               {
                   rb_insert(val,&(*node)->left,*node);
               }
               else
               {
                   rb_insert(val,&(*node)->right,*node);
               }
    return ;
    }
    It looks like top needs to be an address.
    Either pass in &top or define it as a pointer: *top

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you have compiler error/warning, post it. Which compiler, version,etc would probably be helpful too.
    Crash.. details..? segfault? or ..?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    WOW
    now i stopped working on that and used cpp instead that code works fine.
    but now i was implementing very simple hash table with chaining.
    in this code again the program crashed after inserting third element(similar to bst crash problem)
    here is the code

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<strings.h>
    struct data
    {
        char str[10];
        struct data* link;
    };
    typedef struct data* node;
    struct table
    {
       struct data* link;
    }table[137];
    int hash(char *,int);
    int chc(void);
        void insert(int,char *);
    void search(int,char *);
    int main()
    {
    
        char s[10];
        while(1)
        {
            switch(chc())
            {
                case 1:
                    printf("\nEnter any string :");
                    scanf("%s",s);
                    insert(hash(s,strlen(s)),s);
                break;
                case 2:
                break;
                case 3:
                    printf("\nEnter any string :");
                    scanf("%s",s);
                    search(hash(s,strlen(s)),s); 
                    getch();
                break;
                case 9:
                exit(0);
                break;
    
                default:
                break;
    
            }
        }
    
    
    }
    int chc()
    {
    
        int a;
        system("cls");
        printf("1:Make new entry\n2:Delete any key\n3:Search any key\n9:Exit\nEnter Your Choice: ");
        scanf("%d",&a);
        return a;
    }
    
    int hash(char *hm,int len)
    {
        int i,has;
    
        for(i=0;i<len;i++)
        {
            has+=(*hm++)*i;
        }
    
        has=has%137;
        printf("%d",has);
        getch();
        return has;
    }
    
    void insert(int key,char *str)
    {
      node scroll,temp;
         if(table[key].link)
         {
               scroll=table[key].link;                   
               while(scroll->link)
               {
                  scroll=scroll->link;                       
               }
               temp=malloc(sizeof(node));
               scroll->link=temp;
               strcpy(temp->str,str);
               temp->link=NULL;             
         }
         else
         {
               temp=malloc(sizeof(node));
               table[key].link=temp;
               strcpy(temp->str,str);
               printf("%s,",temp->str);
               temp->link=NULL;             
         }
    }     
    void search(int key,char *str)
    {
         node scroll;
         if(table[key].link==NULL)
         {
                                  printf("No such element");
         }
         
         else
         {
                 scroll=table[key].link;
                 while(scroll!=NULL)
                 {
                       if(!strcmp(str,scroll->str))
                       {
                                                   printf("\nwow it works");
                                                   break;
                       }
                       scroll=scroll->link;
                                    
                 }    
         }
     }


    for example i ran it and pressed 1 to enter new element and then repeated the same procedure three times and program crashed.
    i ran it on codeblocks and captured following every time error code is same.
    and it comes after third entry.
    this same prob was there in bst.


    PS: i am running on amd can that be a prob ?>?
    1:Make new entry
    2:Delete any key
    3:Search any key
    9:Exit
    Enter Your Choice: 1

    Enter any string :keasd
    0keasd,
    1:Make new entry
    2:Delete any key
    3:Search any key
    9:Exit
    Enter Your Choice: 1

    Enter any string :askajdj
    44askajdj,
    1:Make new entry
    2:Delete any key
    3:Search any key
    9:Exit
    Enter Your Choice: 1

    Enter any string :adskajsd
    1
    Process returned -1073741819 (0xC0000005) execution time : 8.406 s
    Press any key to continue.
    Last edited by ketav; 10-19-2010 at 07:11 AM. Reason: disabled smileys for 2:Delete :D

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    i am getting real mad now
    i debugged it using devcpp it never crashes :????????????

    when i ran it normally it crashed after entering third variable.
    Can someone run this code and tell me whether it works or not ???

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    It looks like top needs to be an address.
    Either pass in &top or define it as a pointer: *top
    Code:
    typedef struct tree* RB;
    so i guess it is *top in the code;

    and in that code i played with structure if i remove any one element from
    Code:
    struct tree
    {
           int data,color;
           struct tree *left, *right,*root;
           
    };
    same code works well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 10:12 AM
  3. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  4. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM

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