Thread: navigating in a linked list

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

    navigating in a linked list

    Hi,

    I am having trouble making navigation in a linked list work.
    I am tying to make it go to the first, last, next and previous record from a text file.

    Do you know how to do this? could you give me a source code example?

    thank you

    kendals
    Kendals

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You have to build the list from the text file first. Read the data from the file and place it in nodes. After that it's a simple matter of sending your iteration pointer through the list. Assuming it's a double linked list
    Code:
    /* Find first */
    while ( iter->prev != NULL )
      iter = iter->prev;
    
    /* Find last */
    while ( iter->next != NULL )
      iter = iter->next;
    
    /* Find next */
    iter = iter->next;
    
    /* Find previous */
    iter = iter->prev;
    A single linked list is a little more complex, to find the first you assign the iterator to the head pointer and to find the previous you will have to maintain a whisker variable. When your whisker finds the data you access the iterator and that will be the previous node.
    Code:
    /* Find first */
    iter = head;
    
    /* Find last */
    while ( iter->next != NULL )
      iter = iter->next;
    
    /* Find next */
    iter = iter->next;
    
    /* Find previous */
    while ( whisker->data != key ) {
      iter = iter->next;
      whisker = iter->next;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    text file

    How do you build the list from the text file first and Read the data from the file and place it in nodes?

    could you tell me and give me an example.

    thanks

    kendals
    Kendals

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Once you have the file open, just read a line and parse that line into the data for the linked list. Consider a list that has a simple string as the data member of a node:
    Code:
    char buffer[BUFSIZ];
    while ( fgets ( buffer, sizeof buffer, INFILE ) != NULL ) {
      iter->next = malloc ( sizeof ( struct node ) );
      iter = iter->next;
      strncpy ( iter->data, buffer, strlen ( buffer ) );
    }
    This code should read a line from the file, then place it in a new node until it reaches the end of the file or there is an error.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    reply

    Hi,

    When i make my menu in the menu functions do i put those next and previous codes in?

    I have an idea of a menu i can do can you tell me if this is ok?

    do i make it like:

    MENU

    1) view records
    2) make reports
    3) exit program

    From here im not sure how to make this display, like how do i put the next and previous on the screen. Do i need to have them at the bottom like a bottom navigation using gotoxy?

    like this:

    record displayed

    ------------------------------------------
    (1)Next (2) Back (3) First (4) Last

    Is this ok? or is there a better way?

    Thanks
    Kendals

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    How about a separate menu for View Records. When the user selects that option a new menu will pop up.

    Please select an option:
    1) Go to the first record
    2) Go to the previous record
    3) Go to the next recod
    4) Go to the last record

    Beneath the menu would be the information for the current record so that the user knows which way to go.

    Or more along the lines of your idea
    Record #
    -------
    /* Data for the record here */

    -------
    Go to:
    (1) First (2) Previous (3) Next (4) Last

    -Prelude
    Last edited by Prelude; 02-23-2002 at 06:25 PM.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    reply

    thanks, i never heard of that way before, how can you do this?
    Kendals

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I've found a switch statement to work best for a menu such as this. If the switch reads a view case then you clear the screen and write the new menu. Go into the nested case and do your processing of the new menu.
    Code:
    switch ( selection ) {
      case 1:
        clearScr();
        printViewMenu();
        /* Get Input */
        switch ( input ) {
          case 1: 
            /* Work */
            break;
          case 2:
            /* Work */
            break;
        .
        .
        .
    -Prelude
    Last edited by Prelude; 02-23-2002 at 06:37 PM.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    reply

    Hi,

    Thanks, Im thinking of putting a help section in the menu so it will help the user, you think i should make the help file in txt and make printf dump the text on to the screen. Is that ok or you know a better way?

    thanks
    Kendals

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is that ok
    I don't see why not

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    30

    thank you

    Hi prelude

    Thanks for your help

    p.s what if the text is too big scan you scroll down the page? how you do that?
    Kendals

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    In console mode it should automatically scroll. Some systems won't let you scroll back up, so you solve that problem by only printing out a few lines at a time and use getchar() to let the user read at their leisure and continue when they want.
    Code:
    while ( !end of data ) {
      Print a few lines
      printf ( "Press enter to continue\n" );
      getchar();
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM