1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C: Print specific fields from a structure

  1. Dec 29, 2015 #1
    1. The problem statement, all variables and given/known data
    Define a structure VEHICLE which contains producer, model and chassis number. Store the content of a structure in binary search tree such that the key for storing contains all fields of a structure (producer has the highest priority, and chassis number has the lowest). Print data about all vehicles from the specific producer (which is read after all input content).

    2. Relevant equations
    3. The attempt at a solution

    I have defined the following:
    -Data structure for vehicle and binary search tree
    -Functions for reading data, adding data to binary tree, searching by key.
    Code:
    Code (Text):

    typedef struct
    {
      char prod[100];
      char mod[100];
      char chs[100];
    }VEHICLE;

    typedef struct node
    {
      VEHICLE ve;
      struct node *left,*right;
    }NODE;

    NODE *form_node(VEHICLE *ve)
    {
      NODE *node=(NODE *)malloc(sizeof(NODE));
      node->left=node->right=0;
      node->ve=*ve;
      return node;
    }

    NODE *add_node(NODE *root,VEHICLE *ve)
    {
      if(root==0)
      return form_node(ve);
      if(strcmp(ve->prod,root->ve.prod)<0 ||
      ((strcmp(ve->prod,root->ve.prod)==0) &&
      strcmp(ve->mod,root->ve.mod)<0) ||
      (strcmp(ve->prod,root->ve.prod)==0 &&
      strcmp(ve->mod,root->ve.mod)==0 &&
      strcmp(ve->chs,root->ve.chs)<0))
      root->left=add_node(root->left,ve);
      else
      root->right=add_node(root->right,ve);
      return root;
    }

    NODE *searching(NODE *root,VEHICLE *ve)
    {
      if(root == 0)
      return NULL;
      else if(strcmp(ve->prod,root->ve.prod)==0 &&
      strcmp(ve->mod,root->ve.mod)==0 &&
      strcmp(ve->chs,root->ve.chs)==0)
      return root;
      else if(strcmp(root->ve.prod,ve->prod)>0 &&
      strcmp(root->ve.mod,ve->mod)>0 &&
      strcmp(root->ve.chs,ve->chs)>0)
      return searching(root->left,ve);
      else
      return searching(root->left,ve);
    }

    void read(VEHICLE *ve)
    {
      printf("producer:");
      scanf("%s",ve->prod);
      printf("model:");
      scanf("%s",ve->mod);
      printf("chassis number:");
      scanf("%s",ve->chs);
    }
     
    How to implement a function that prints data about vehicles from specified input producer?
     
  2. jcsd
  3. Dec 29, 2015 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Start searching for the first (possible) vehicle from this specific producer, then go forwards until you hit a different producer?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C: Print specific fields from a structure
Loading...