C: Print specific fields from a structure

Click For Summary
SUMMARY

The discussion focuses on implementing a binary search tree (BST) to store and manage a structure named VEHICLE, which includes fields for producer, model, and chassis number. The key for storing vehicles prioritizes the producer, followed by the model and chassis number. The provided code includes functions for creating nodes, adding vehicles to the BST, and searching for specific vehicles. A key challenge discussed is implementing a function to print all vehicles from a specified producer.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with data structures, specifically binary search trees
  • Knowledge of string manipulation functions in C, such as strcmp
  • Experience with dynamic memory allocation using malloc
NEXT STEPS
  • Implement a function to traverse the BST and print vehicles by producer
  • Learn about memory management in C to handle dynamic structures effectively
  • Explore advanced tree traversal techniques, such as in-order and pre-order traversals
  • Investigate error handling in C, particularly for memory allocation and input validation
USEFUL FOR

C programmers, computer science students, and software developers interested in data structures and algorithms, particularly those working with binary search trees and vehicle management systems.

gruba
Messages
203
Reaction score
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
Start searching for the first (possible) vehicle from this specific producer, then go forwards until you hit a different producer?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K