Problem with structures

This is a discussion on Problem with structures within the C++ Programming forums, part of the General Programming Boards category; I am having problem executing this code i wrote for creating a binary tree.the code is compiling well but its ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Post Problem with structures

    I am having problem executing this code i wrote for creating a binary tree.the code is compiling well but its stops running after taking the input of words.please help...
    insert
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<stddef.h>
    #define MAX 100
    struct tnode{
        char *word;
        int count;
        struct tnode *left;
        struct tnode *right;
    };
    
    struct tnode *additem(struct tnode *,char *);
    void printtree(struct tnode *);
    main()
    {struct tnode *root,*p1;
    char *p[10],words[10][MAX];
    int i,n,k;
    root=NULL;
    
    for(i=0;i<10;i++)
    {p[i]=words[i];
    gets(p[i]);
    }
    
    root=additem(root,p[0]);
    k=1;
    while(k<10)
    {if(isalpha(*(p[k]+0)))
       {
       p1=additem(root,p[k]);
        }
    }
    printtree(root);
    getch();
    }
    
    struct tnode *talloc(void);
    
    struct tnode *additem(struct tnode *p,char *w)
    {
        int cond;
        if(p==NULL)
        {
            p=talloc();
            p->word=w;
            p->count=1;
            p->left=NULL;
            p->right=NULL;
        }
        else if((cond=strcmp(p->word,w))==0)
        p->count++;
        else if(cond>0)
        p->left=additem(p->left,w);
        else
        p->right=additem(p->right,w);
        return p;
    }
    
    void printtree(struct tnode * tree) {
       if(tree->left) printtree(tree->left);
       printf("%4d %s\n",tree->count,tree->word);
       if(tree->right) printtree(tree->right);
    }
    struct tnode *talloc(void)
    {
        return (struct tnode *)malloc(sizeof(struct tnode));
    }

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I can't fix all the code but I can tell you where it stops and why.
    Code:
    while(k<10)
    {
      if(isalpha(*(p[k]+0)))
      {
        p1=additem(root,p[k]);
      }
    }
    you never change the value of k. Hence while never stops. Sorry I don't know more about trees yet or I'd try to help with why it isn't printing the words either.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    thnx man..i wasnt able to see the fault...now the program runs..

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,827
    The code is broken.
    It needs indentation.
    It needs to stop using gets: SourceForge.net: Gets - cpwiki
    It's leaking memory like crazy. Everything you malloc you must free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with structures and seg fault
    By grifan526 in forum C Programming
    Replies: 3
    Last Post: 04-15-2010, 05:29 PM
  2. Problem filling an array of structures
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 03-11-2010, 01:29 AM
  3. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  4. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  5. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM

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