Thread: Printing linked lists

  1. #1
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23

    Printing linked lists

    I'm almost finished with a program that uses linked lists to keep track of students and their courses.

    Were supposed to print out the course part of the output in a transcript like fashion, (two courses per row).

    So you'd have like:
    Code:
    CS2200                    CIS3995
    C programming             Data Structures
    A                         B
    The student records are printed out using a for loop. And the course records themselves are in a nested for loop. So far, I can get the courses to print underneath each other, but our teacher wants the courses printed as above.

    Any Ideas?
    Last edited by jcramer; 03-07-2004 at 10:48 PM.
    Viva la Tutorial
    What is the meaning of life? 42 of course!

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    you could do something like this...

    printf("%s\t%s\n",list->class,list->next->class);
    list = list->next->next

    just an idea.. of course you'd need to check to make sure the next node exists and all that. tell me if that works

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    *left, *right;
    
    for( left = list, right = left->next ? left->next : NULL ; left; )
    {
        printf("%d", left->number )
        if ( right ) printf("\tabbing\spacing%d", right->number );
        ...do the same for other values...
    
        left = right ? right->next : right;
    }
    That should suffice.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM