I believe I am building my linked list correctly. Now I need help to write a function that will allow me to search two elements of the data and return the third. This program is suppose to accept 3 integers from the user the first one represents city #1 and the second represents city #2 and the third will represent the distance between the two. I have the program completed (not finalized) but need to write a function that will allow the user to enter 2 integers representing cities they previously entered into the linked list, search the list for those two values, then print what the distance was between those two cities. I need help with what to pass the function and how I would go about writing it. I have function for startscreen that I am not sending because it is irrelevant. Here is the code I have:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
void StartScreen(void);
typedef struct node NODE_t;
typedef NODE_t *LINK;
struct node
{
int City1;
int City2;
int Distance;
LINK next;
};
int main()
{
LINK head;
LINK current_node;
LINK new_node;
char Repeat;
int ContinueAddFlag = FALSE;
int SelCity1;
int SelCity2;
int TargetDistance;
StartScreen();
head = (LINK) malloc(sizeof(NODE_t));
head->next = NULL;
current_node = head;
do
{
new_node = (LINK) malloc(sizeof(NODE_t));
current_node->next = new_node;
current_node = new_node;
printf ("\n\nPlease enter 3 integers a b c: ");
scanf ("%d %d %d", &(new_node->City1), &(new_node->City2), &(new_node->Distance));
fflush(stdin);
printf("\nWould you like to enter 3 more integers? (y/n) ");
scanf("%c", &Repeat);
if (Repeat == 'y' || Repeat == 'Y')
ContinueAddFlag = TRUE;
else if (Repeat == 'n' || Repeat == 'N')
{
ContinueAddFlag = FALSE;
printf("\nWhat distance would you like to find? ");
scanf("%d %d", &SelCity1, &SelCity2);
TargetDistance = TraverseList(SelCity1, SelCity2);
printf("\nThe distance between cities %d and %d is %d.", SelCity1, SelCity2, TargetDistance);
}
}
while (ContinueAddFlag == TRUE);
current_node->next = head;
current_node = head;
do
{
printf ("memory location: %p\n", current_node->next);
current_node = current_node->next;
}
while ( !(current_node->next == head));
fflush(stdin);
getchar();
}