Circular doubly linked list with dummy header

This is a discussion on Circular doubly linked list with dummy header within the C Programming forums, part of the General Programming Boards category; Code: /*Create and return a doubly linked list (circularly linked) with dummy header node and 2 data nodes (values are ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Circular doubly linked list with dummy header

    Code:
    /*Create and return a doubly linked list (circularly linked) 
    with dummy header node and 2 data nodes (values are 1 and 2) */
    NodePtr SetupListWithDummyAnd2DataNodes()
    {	NodePtr pA = (NodePtr)malloc(sizeof(struct node));
    	NodePtr pB = (NodePtr)malloc(sizeof(struct node));
    	NodePtr dummy = (NodePtr)malloc(sizeof(struct node));
    
    	pA->data = 1;
    	pB->data = 2;
    
    	pA->Rnext = pB->Lnext; 
    	pA->Lnext = dummy;
    
    	pB->Rnext = dummy;
    	pB->Lnext = pA->Rnext;
    
    	dummy->Rnext = pA->Lnext;
    	dummy->Lnext = pB->Rnext;
    
    	return dummy;
    }
    
    void main()
    {     SetupListWithDummyAnd2DataNodes();
           //Nil return when print the node out
    }
    Nil return of the function SetupListWithDummyAnd2DataNodes()... why?? Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably because main doesn't return void. Or it could be because that's horrible code. It's ugly to read, and it's a lousy implementation. Oh, and you're one of those people who for some reason thinks it's a good idea to typedef pointers.


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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    pA->Rnext = pB->Lnext;
    The above line copies the pointer contained on pB->Lnext to the pointer pA->Rnext. When this line is executed, the pointer pB->Lnext is uninitialized, containing any random address. That means that pA->Rnext will also contain the same random address.

    To further complicate matters, two lines down pB->Lnext is set to the value of pA->Rnext, which is just a useless because neither pointer has been initialized to any valid address.

    Code:
    pA->Rnext = pB->Lnext;
    pB->Lnext = pA->Rnext;
    This whole mess can be corrected by setting the Lnext and Rnext pointers to valid addresses of struct node objects. If you want pA->Rnext to point to pB, then do that
    Code:
    pA->Rnext = pB;
    Last edited by Ancient Dragon; 10-30-2005 at 04:14 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    Oh, and you're one of those people who for some reason thinks it's a good idea to typedef pointers.
    Code:
    struct node
    {
           int data;
           struct node *next;
    };
    
    typedef struct node * nodeptr;
    Why is the above code a bad idea, besides the obvious pitfall:-

    Code:
    nodeptr p;
    p=malloc(sizeof(nodeptr));
    If you think that it reduces readability I beg to disagree, I think the contrary.
    Thanks,
    Sahil

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, I never have to go searching through whatever header file you've burried your typedef in to see what p really is, if I see this line instead:
    Code:
    struct foo *p;
    Secondly, I find it to be very ugly. It's probably one of the reasons I've never felt the desire to do Windows programming. They have this need to typedef EVERYTHING. So annoying, and very ugly code.

    I suppose it's all well and good if you like to typedef them. It's your code after all. Just don't expect me to read it unless I'm being paid.


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

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    It would be really nice if you read linked list library this code of mine, though I dont think I could pay you
    Thanks,
    Sahil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly Linked List - Understanding...
    By hi-0ctane in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 05:27 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21