C: Print specific fields from a structure

In summary, a structure VEHICLE was defined to store data about vehicles, and a binary search tree was used to store the data with the producer having the highest priority and chassis number having the lowest priority as the key. The code includes functions for reading data, adding data to the tree, and searching by key. To print data about vehicles from a specific producer, the code suggests searching for the first vehicle from that producer and then iterating forwards until a different producer is encountered.
  • #1
gruba
206
1

Homework Statement


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).

Homework Equations


3. The Attempt at a Solution [/B]
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:
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?
 
Physics news on Phys.org
  • #2
Start searching for the first (possible) vehicle from this specific producer, then go forwards until you hit a different producer?
 

1. How do I print a specific field from a structure in C?

To print a specific field from a structure in C, you can use the dot (.) operator to access the field and the printf() function to print its value. For example, if the structure is named "student" and the field you want to print is "name", you can use printf("%s", student.name); to print the value of the "name" field.

2. Can I print multiple fields from a structure in a single statement?

Yes, you can print multiple fields from a structure in a single printf() statement by using multiple format specifiers. For example, printf("Name: %s, Age: %d", student.name, student.age); will print both the "name" and "age" fields from the "student" structure.

3. How do I print a specific field from a nested structure in C?

To print a specific field from a nested structure in C, you can use the dot (.) operator to access the field at each level of the nested structure. For example, if you have a structure named "address" nested within the "student" structure, you can use printf("%s", student.address.street); to print the value of the "street" field from the "address" structure.

4. What happens if I try to print a field that doesn't exist in the structure?

If you try to print a field that doesn't exist in the structure, your program will likely produce a compile-time error. This is because the field you are trying to access does not exist within the structure, and the compiler will not be able to find it. It is important to ensure that the field you are trying to print actually exists in the structure before attempting to access it.

5. Can I print a specific field from a structure using a pointer?

Yes, you can print a specific field from a structure using a pointer by using the arrow (->) operator instead of the dot (.) operator. This is because a pointer to a structure is essentially a reference to the structure, so you can use it to access the fields within the structure. For example, if you have a pointer named "ptr" to a structure named "student", you can use printf("%s", ptr->name); to print the value of the "name" field from the "student" structure.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
Back
Top