Thread: linked lists

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    19

    linked lists

    Beating my head against my desk trying to understand how linked lists work.
    (all those pointers pointing to pointers, then pointing to the same pointer that
    started the pointing....)
    The tutorial on this site helped.

    I wrote the code below just for practice.

    I chopped it to the down until it would compile.

    When I execute the program I get "Segmentation fault".

    I suspect the problem is in how I'm trying to get the program to print out the
    lists. ????

    Code:
    /*linked lists- here goes*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct list {
    	char name[80];
    	struct list *next;
    } *new, *head, *current, *marker;
    
    int main(void)
    {
    	new = NULL; /*set all pointers to NULL*/
    	head = NULL;	
    	current = NULL;
    	marker = NULL;
    	
    	/*add the first list element*/
    	
    	new = (struct list*)malloc(sizeof(struct list));
    	new -> next = head;
    	head = new;
    	strcpy(new -> name, "Amy");
    
    	/*add another element*/
    	
    
    	new = (struct list*)malloc(sizeof(struct list));
    	new -> next = marker -> next;
    	marker -> next = new;
    	strcpy(new -> name, "Samantha");
    
    	/*add a third element*/
    
    	new = (struct list*)malloc(sizeof(struct list));
    	new -> next = marker -> next;
    	marker -> next = new;
    	strcpy(new -> name, "Angela");
    
    	/*create the final element*/
    
    	current = head;
    	while (current -> next != NULL)
    	{
    		current = current -> next;
    	}
    
    	new = (struct list*)malloc(sizeof(struct list));
    	current -> next = new;
    	new -> next = NULL;
    	strcpy(new -> name, "Leanne");
    
    	/*print out all lists*/
    	current = head;
    	while (current != NULL)
    	{
    		printf("%s\n", current -> name);
    		current = current -> next;
    	}
    
    	printf("\n");
    	return(0);
    }
    Thanks

  2. #2
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    you should write a function to insert each node into the list. If you want to insert into the front of the list it isnt too hard. Heres some pseudocode.

    Code:
    void new_node(struct list ** pHead,  char name[])
    {
    struct list *pTemp;
    
    malloc space for pTemp and set the name field to name and the next field to the current Head pointer.
    
    /*Move head pointer*/
    (*pHead) = pTemp
    }
    Then to call use this
    Code:
    new_node(&pHead, name)
    That should do it.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should save yourself the headache and break this up into functions.
    Code:
    struct node *newnode( char *name )
    {
        struct node *n;
    
        n = malloc( sizeof( *n ) );
        if( n == NULL )
        {
            printf("malloc failure\n");
            return NULL;
        }
    
        strcpy( n->name, name );
        n->next = NULL;
    
        return n;
    }
    Then you do something like:
    Code:
    char *names[] =
        {
            "Amy",
            "Samantha",
            "Angela",
            "Leanne",
            NULL
        };
    
        int x;
    
        struct node *thelist = NULL, *n = NULL;
    
        for( x = 0; names[x] != NULL; x++ )
        {
            n = newnode( names[x] );
    
            if( n == NULL )
                break;
    
            n->next = thelist;
            thelist = n;
        }
    Now you've built a list. Something to think about anyway...

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

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    1
    Quote Originally Posted by crypto_quixote
    Beating my head against my desk trying to understand how linked lists work.
    (all those pointers pointing to pointers, then pointing to the same pointer that
    started the pointing....)
    The tutorial on this site helped.

    I wrote the code below just for practice.

    I chopped it to the down until it would compile.

    When I execute the program I get "Segmentation fault".

    I suspect the problem is in how I'm trying to get the program to print out the
    lists. ????

    Code:
    /*linked lists- here goes*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct list {
    	char name[80];
    	struct list *next;
    } *new, *head, *current, *marker;
    
    int main(void)
    {
    	new = NULL; /*set all pointers to NULL*/
    	head = NULL;	
    	current = NULL;
    	marker = NULL;
    	
    	/*add the first list element*/
    	
    	new = (struct list*)malloc(sizeof(struct list));
    	new -> next = head;
    	head = new;
    	strcpy(new -> name, "Amy");
    
    	/*add another element*/
    	
    
    	new = (struct list*)malloc(sizeof(struct list));
    	new -> next = marker -> next;
    	marker -> next = new;
    	strcpy(new -> name, "Samantha");
    
    	/*add a third element*/
    
    	new = (struct list*)malloc(sizeof(struct list));
    	new -> next = marker -> next;
    	marker -> next = new;
    	strcpy(new -> name, "Angela");
    
    	/*create the final element*/
    
    	current = head;
    	while (current -> next != NULL)
    	{
    		current = current -> next;
    	}
    
    	new = (struct list*)malloc(sizeof(struct list));
    	current -> next = new;
    	new -> next = NULL;
    	strcpy(new -> name, "Leanne");
    
    	/*print out all lists*/
    	current = head;
    	while (current != NULL)
    	{
    		printf("%s\n", current -> name);
    		current = current -> next;
    	}
    
    	printf("\n");
    	return(0);
    }
    Thanks
    What's the value of the variable 'marker' after you add "Amy" but before "Samantha"?

    new -> next = marker -> next;

    Also, as everyone suggested, break the program into functions.

    c-prog

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    19

    okay

    Okay,

    Thanks everyone.

    I'll try the functions route.

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