Thread: help with linked lists

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    help with linked lists

    Hi guys, im stuck with this linked list problem, i just want to make 3 nodes all with values in them declared in main and just print out its values, but im confused on how to link them together especially with the *head and *tail.
    I can implement it with out the *head and *tail pointers but with them included its more confusing!

    thanks

    Code:
    #include <stdio.h>
    
    typedef int data_t;
    typedef struct node node_t;
    
    struct node {
    	data_t data;
    	node_t *next;
    };
    
    typedef struct {
    	node_t *head;
    	node_t *tail;
    } sequence_t;
    
    void print_list( sequence_t * );
    
    int main( void )
    {
    	sequence_t nodes;
    	node_t num1, num2, num3, *start;
    
    	num1.data = 2;
    	num2.data = 10;
    	num3.data = 20;
    
    	return 0;
    
    }
    
    
    void print_list( sequence_t *seq )
    {
    	while ( print != NULL ) {
    		printf( "head: %d\n", seq->head->data );
    	}
    	seq=seq->head->next;
    }
    Last edited by kurz7; 05-21-2003 at 04:37 AM.
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    How about
    Code:
    nodes.head = &num1;
    num1.next = &num2;
    num2.next = &num3;
    num3.next = NULL;
    nodes.tail = &num3;
    
    ....
    
    void print_list( sequence_t *seq )
    {
      node_t * ptr;
    
      for (ptr = seq->head; ptr != NULL; ptr = ptr->next) 
      {
        printf( "data: %d\n", ptr->data );
      }
    }
    it makes tail a bit redundant as is but I'm sure you can fix that if you want to...
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    dont i have to pass in something into the print_list function?
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void insertNode( Node *n )
    {
        if( n->value < head->value )
        {
            n->next = head;
            head->prev = n;
            head = n;
        }
        else
        if( n->value > tail->value )
        {
            n->prev = tail;
            tail->next = n;
            tail = n;
        }
        else
        ...loop through and insert...
    }
    This code assumes:
    a) A double linked list. Infinately easier than single linked lists.
    b) Global pointers for 'head' and 'tail'.
    c) That you're sorting by some value and you want it sorted from least to greatest.

    Otherwise, modify it to suit your needs. Basicly, if prepending, you make the new node point to whatever is currently the head. Then you make the head be the new node.

    If appending, do the opposite.

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

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    thanks guys

    ill have to look through the doubly linked list

    cheers
    there are only 10 people in the world, those who know binary and those who dont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 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