Click here to Skip to main content
15,891,936 members
Articles / Desktop Programming / MFC
Article

Implementing Linked Lists with Double Pointers

Rate me:
Please Sign up or sign in to vote.
3.75/5 (4 votes)
3 Mar 2000 176.8K   27   15
Describes a linked list implementation that uses double pointers.

Introduction

While working on a project recently I found that some quite advanced programmers did not understand my linked list code. This document was my attempt to explain the method behind the madness.

More Information

The linked list code presented is unexceptional except for one fact: Most of the linked list worker functions work with double pointers (to nodes), whereas more conventional code typically uses single pointers. The double pointer system is used as it has the interesting behavior of trivializing a number of cases that usually have to be handled as special cases when dealing with a plain pointer to node.

I present the following "trivial" linked list node:

struct node {
  node* pNext;
}* pRootNode;

The node struct presented is an example of a very simple singly linked list. pRootNode is a node*, pointing to the root of a linked list of nodes. Each node in the linked list contains a pNext member - a pointer to the next element in the list. If pRootNode is NULL the list is empty, and a pNext member being NULL marks the end of the list.

If we were to make a function to add a node to the start of the list, it may look something like this if we used single rather than double pointers:

void AddNodeToStart(node* pNewNode)
{
  node* pOldStart = pRootNode;
  pRootNode = pNewNode;
  pNewNode->pNext = pOldStart;
}

Which is quite simple. The moment we need to either insert a node into a sorted list, or even merely at the end the problem becomes less trivial:

void AddNodeToEnd(node* pNewNode)
{
  if(pRootNode == NULL){
    pRootNode = pNewNode;
  } else {
    node* pScan = pRootNode;
    while(pScan->pNext != NULL) 
      pScan = pScan->pNext;
    pScan->pNext = pNewNode;
  }
}

A special case had to be inserted to deal with the "empty list" case. In the case of a sorted list, the comparison would be added to the "while" clause. Also, futher special case code would have to be added to deal with the new "sorted" item being inserted at the root.

Now, contrast the above two functions, with the following function that can add a new node anywhere in a linked list. This method uses double pointers:

void AddNodeSorted(node* pNewNode)
{
  node** ppScan = &pRootNode;
  while(*ppScan != NULL && compare(*ppScan,pNewNode))
    ppScan = &(*ppScan)->pNext;

  pNewNode->pNext = *ppScan;
  *ppScan = pNewNode;
}

Notice how the while loop does not need to account for the empty list as a special case.

I will explain how the various cases are handled:

  • When the list is empty, pRootNode will be NULL. ppScan however will be valid, pointing to pRootNode. *ppScan is thus NULL, the while loop will exit, and the node will be added. As ppScan points to pRootNode, the line "*ppScan = pNewNode;" will set pRootNode to be the new node, and pNewNode->pNext will be set to NULL.

  • When the list is not empty, and the new node must be added to the root of the sorted list, ppScan will be pointing to pRootNode. The compare function takes two node*s, the node to add (pNewNode), and the node to compare against (*ppScan). "compare" determines that pNewNode should be inserted before *ppScan, so terminates the while loop. ppScan of course points to pRootNode, so the new node is added to the root successfully.

  • When the list is not empty, and the new node must be added to the middle of the list, a similar situation to the previous case exists. compare will terminate the while loop, this time with ppScan pointing to the pNext member of the previous element of the linked list. This is overwritten, inserting the new node into the list, and the new nodes next pointer is set to the next element to complete the insertion.

  • When the list is not empty and the new node must be added to the end of the list, the compare function will not terminate the loop. *ppScan will become NULL, when the end of the list is reached and the while loop will exit. ppScan however is still valid, pointing to the pNext member of the last node in the linked list. It is simple to set *ppNext (pointer to the pNext member of the last node) to the new node, thus successfully adding the new node to the end of the list.

Please send any comments or bug reports to me via email. For any updates to this article, check my site here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood article Pin
padpub31-Dec-05 9:27
padpub31-Dec-05 9:27 
QuestionWhy Comlicated the base things???? Pin
istynet19-Oct-04 1:25
istynet19-Oct-04 1:25 
Generalcopied Pin
Anonymous5-Jul-04 3:37
Anonymous5-Jul-04 3:37 
GeneralRe: copied (not really) Pin
Niall Barr5-Jul-04 4:09
professionalNiall Barr5-Jul-04 4:09 
QuestionWhy write your own ? Pin
Christian Graus9-Dec-02 14:57
protectorChristian Graus9-Dec-02 14:57 
AnswerRe: Why write your own ? Pin
Matt Gullett9-Dec-02 16:18
Matt Gullett9-Dec-02 16:18 
AnswerRe: Why write your own ? Pin
Fayez Asar15-Dec-02 13:29
sussFayez Asar15-Dec-02 13:29 
GeneralRe: Why write your own ? Pin
Christian Graus15-Dec-02 14:52
protectorChristian Graus15-Dec-02 14:52 
General'**' use for change '*' Pin
sayhappy9-Dec-02 14:32
sayhappy9-Dec-02 14:32 
Generalcircular link lists Pin
Jon25-Sep-00 23:10
Jon25-Sep-00 23:10 
GeneralRe: circular link lists Pin
26-Jan-01 9:26
suss26-Jan-01 9:26 
GeneralRe: circular link lists Pin
Nynaeve18-Apr-07 5:38
Nynaeve18-Apr-07 5:38 
GeneralRe: circular link lists Pin
4-Jul-01 22:54
suss4-Jul-01 22:54 
Question** ? Pin
Member 305311-Jun-00 9:09
Member 305311-Jun-00 9:09 
AnswerRe: ** ? Pin
Wes Jones18-Aug-00 12:15
Wes Jones18-Aug-00 12:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.