Thread: Searching Linked List

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Searching Linked List

    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();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef <something> Distance;
    typedef <something> Key;
    
    
    Distance findDistance( Key k1, Key k2 )
    {
        Key k3;
        Node *n1, *n2;
    
        for( n1 = firstNode; n1 != NULL; n1 = n1->next )
        {
            if( n1->key == key1 ) break;
        }
        if( n1 == NULL )
        {
            perror( "Key not found in the list.");
            return -1;
        }
    
        /*do the exact same thing for n2 */
    
        /* compute the distance however you do, then return it */
        return getDistance( n1, n2 );
    }
    There is enough code/pseudo-code to get you through.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM