Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <process.h>

struct Planet
{
   char name[20];
   double dist;
};

struct TreeNodeHdr
{
   struct Planet *planetptr;
   struct TreeNodeHdr *left, *right;
};
struct TreeNodeHdr *head = NULL;
FILE *input, *output;//create two objects of file;
int search(struct TreeNodeHdr *temp, struct Planet *p)
{
  if(temp==NULL)
   {
      return 0;
   }
  else
   {
      if(temp->planetptr->dist == p->dist)
    return 1;
      search(temp->left,p);
      search(temp->right,p);
   }
}

struct TreeNodeHdr* createTreeNode(struct TreeNodeHdr *t,struct Planet *p)
{
    struct TreeNodeHdr *newnode,*temp;
    if( search(t,p) ==1 )
     {
      printf("\n\n\t Already Exists !!");
      getch();
      return t;
     }
    else
     {
    newnode = (struct TreeNodeHdr *) malloc (sizeof(struct TreeNodeHdr));
    newnode->left = newnode->right = NULL;
    newnode->planetptr = p;
    if(t==NULL)
     {
        t = newnode;
     }
    else
     {
        temp = t;
        while(temp)
         {
           if(p->dist < temp->planetptr->dist)
         {
            if(temp->left == NULL)
             {
               temp->left = newnode;
               break;
             }
            else
               temp = temp->left;
         }
           else
         {
            if(temp->right == NULL)
             {
               temp->right = newnode;
               break;
             }
            else
               temp = temp->right;
         }//end left else
         }//end while
     }//end null else
     }//end search else
     return t;
}

void in_order(struct TreeNodeHdr *temp)
{
  if(temp==NULL)
   {
      return;
   }
  else
   {
      in_order(temp->left);
      printf("\n %s \t -> \t %lf", temp->planetptr->name, temp->planetptr->dist);
      in_order(temp->right);
   }
}
void reverse(struct TreeNodeHdr *temp)
{
  if(temp==NULL)
   {
      return;
   }
  else
   {
      reverse(temp->right);
      printf("\n %s \t -> \t %lf", temp->planetptr->name, temp->planetptr->dist);
      reverse(temp->left);
  }
}
int main()
{
   struct Planet *p;
   int choice;
   printf("\n Read  Details of 9 planets");
   input = fopen("PlanetData.dat", "r");//open PlanetData.dat file and read planet data for each planet from it;
   if (input == NULL)
   {
       printf("error: input\n");
       exit(-1);
   }

   do
    {
 //     clrscr();
      printf("\n1. Enter Planet");
      printf("\n2. Show Planets");
      printf("\n3. Show Planets In Reverse");
      printf("\n4. Exit");
      printf("\n\n Enter Your Choice: ");
      scanf("%d",&choice);
      switch(choice)
       {
     case 1:
         p = (struct Planet *) malloc(sizeof(struct Planet));
         printf("\n Enter Name of Planet: ");
         fscanf("%s",p->name);
         printf("\n Enter Distance From Sun: ");
         fscanf("%lf",&p->dist);
         head = createTreeNode(head, p);
         break;
     case 2:
         printf("\n\n The Planet List Is: \n");
         in_order(head);
         getch();
         break;
     case 3:
         printf("\n\n The Planet List Is: \n");
         reverse(head);
         getch();
         break;
     case 4: exit(0);
       }
    }while(1);
    fclose(input);
   return 0;
}
Posted

1 solution

i found it's answer

it should be like this

fscanf(input,"%s",p->name);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900