Double linked lists

This is a discussion on Double linked lists within the C Programming forums, part of the General Programming Boards category; Yes. Now that will make your code more readable Which is always a good thing. Now, as for your question ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Yes. Now that will make your code more readable
    Which is always a good thing.

    Now, as for your question about the linked lists...? What was it again?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    How to link the prev pointer to each node so all nodes are linked by an *next and *prev

    I can implement *next fine, that's easy, but it doesn't work too well when I try to use the *prev pointer.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The prev pointer works pretty much like the next pointer, but points the other way around, so when you add something to a dlist, you link the prev-pointer of the new node to the previous node, and the next node of the previous node to the new node, for example.

    The first nodes prev pointer is NULL [or whatever you use as a end-marker], and the last nodes next pointer is NULL.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Along the lines for a few scenarios of working with double-linked lists, here's a little source snippet from me to demonstrate how a linked list can work:

    Code:
    struct CLinkedListBeginOrEnd
    {
    	CLinkedList* pBegin;
    	CLinkedList* pEnd;
    };
    
    CLinkedList* pPrevious;
    CLinkedList* pNext;
    CLinkedListBeginOrEnd* pBeginOrEnd;
    
    void InsertAfter(CLinkedList* pAdd, CLinkedList* pAfter)
    {
    	CLinkedList* pNextNode = pAfter->pNext;
    	CLinkedList* pPrevNode = pAfter;
    	pPrevNode->pNext = pAdd;
    	if (pNextNode) pNextNode->pPrevious = pAdd;
    	pPrevious = pPrevNode;
    	pNext = pNextNode;
    	if (pNextNode == NULL) // We're the last node
    		pBeginOrEnd->pEnd = this;
    }
    This was actual C++ code converted to C code, but I hope you may still find it relevant to understand how a double linked list works.
    Remember: Don't copy. Understand how the code works and you'll get the hang of how a double-linked list works. Then make your own.
    Last edited by Elysia; 03-13-2008 at 06:07 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks Elysia and matsp, I will review it, the steps are what I need to review, I can code it easily after that. Thanks again !

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 04:25 AM
  2. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:30 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. Replies: 1
    Last Post: 03-21-2002, 05:10 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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