Reputation: 41
I have created the data needed for my node(car).
struct data {
char carReg[10];
char make[20], model[20], colour[20];
int numPrevOwners;
bool reserved;
float reserveAmount;
};
I've also created the template for the node and the declared the global variables.
struct node {
struct data *element;
struct node *next;
};
struct node *front = NULL;
struct node *last = NULL;
In my viewSpecific() method I want the user to enter in a unique carReg and it will then find the node in which this is stored in and display it.
This is what I have so far:
void viewSpecific() {
char result[10];
printf("Please enter the registration of the car yiew wish to view.");
scanf("%s", &result);
struct node *current, *previous;
bool notFound = true;
printf("\n");
if (isEmpty())
printf("Error - there are no nodes in the list\n");
else {
I'm not quite sure what to do after this.
Upvotes: 0
Views: 45
Reputation: 108986
Check if the carReg entered by the user is the same as the current node's carReg. If it isn't advance to the next node. If you reach the end of the list, it's because the carReg was not found.
/* basically, something like */
current = front;
while (current != NULL) {
if (strcmp(current->element->carReg, userCarReg) == 0) /* found */ break;
current = current->next;
}
if (current == NULL) {
printf("carReg not found\n");
} else {
printf("carReg: %s\n", current->element->carReg);
printf("make: %s\n", current->element->make);
printf("previous owners: %d\n", current->element->numPrevOwners);
}
Upvotes: 1