Thread: Simple Doubly-Linked List question.

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Simple Doubly-Linked List question.

    Hey guys,
    my list is NULL in the beginning.
    do I have to make a special case for when inserting the first element of the doubly linked list? I am having trouble with pointing to the previous element when adding the first element.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*prototypes*/
    int push(struct node**, int);
    int printList(struct node*);
    
    /*doubly linked-list structure*/
    struct node {
    	int data;
    	struct node* nextPtr;
    	struct node* previousPtr;
    	struct node* childPtr; /*will point to another list*/
    };
    
    /*main*/
    int main(void){
    	struct node* head = NULL;
    	int m = 5;
    
    	push(&head, m);
    	printList(head);
    	
    	free(head);       /*hehe, ya I know*/
    	return 0;
    }
    int push(struct node** head, int data){
    	struct node* current = *head;
    	struct node* newNode;
    	if( !(newNode = malloc(sizeof(struct node))) )
    		return -1;
    
    	newNode->data = data;
    	newNode->nextPtr = *head;
    	newNode->previousPtr = NULL;
    
    	/* I tried (*head)->previousPtr = newNode here but the program crashes. 
    	 *I think its because *head is NULL here*/
    
    	*head = newNode;
    
    	return 0;
    }
    int printList(struct node* head){
    	while(head != NULL){
    		printf("%d ", head->data);
    		head = head->nextPtr;
    	}
    	printf("\n");
    	return 0;
    }
    So yeah, my question is do I need to make a special case for inserting the first element in a doubly linked list, or is there a way around it?
    Thanks!

    PS -- I havent done all the error handling etc yet. I usually do that at the end.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    This tutorial is a good read.
    Yes, you'll need a special case. But there're other ways also(such as dummy head,etc).

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The "special case" could be as simple as testing if head == NULL. If it does, then you create the head -- obviously, head->prev should then be NULL.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Cool, thank you.
    I've been at it for a while and I am overcomplicating things.

    Thanks again.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	free(head);       /*hehe, ya I know*/
    Actually you don't know it might be leaking memory if there's more than one element in list.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by Bayint Naung View Post
    Code:
    	free(head);       /*hehe, ya I know*/
    Actually you don't know it might be leaking memory if there's more than one element in list.
    wait do i have to do "walk" through the linked list and free each node individually?
    Code:
    	while(head){
    		temp = head;
    		head = head->nextPtr;
    		free(temp);
    	}
    Like that ?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yep, that'll free it all up nicely.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    One of my professors taught me a trick that will save you MANY MANY headaches developing data structures.
    The FIRST thing you should do is map out the special cases in your code:
    Code:
    if (a) { }
    else if (b) { }
    else if (c) { }
    else { }
    The next step is to tackle the easy cases.
    Finally, you should develop the most complicated case.
    Most people dive into inserting/deleting by writing the complicated case then thinking why the H**L it doesn't work half of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubly Linked list - sorting
    By mrsirpoopsalot in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2009, 01:31 AM
  2. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM