DataReelDataReelC++ Transition Guide

Examples › General Examples

general_doubly_linked_list

C++11 or laterC++ standard library onlyPortable C++ (tested on Linux)

Legacy: examples_general/doubly_linked_list/testprog.cpp · Modern: examples_modern/general/doubly_linked_list/modern.cpp

Modern equivalent of the doubly linked list test. std::list is a doubly linked list that owns its nodes: no gxListNode is new'd, and nothing is freed by hand. gxList's MoveBefore()/MoveAfter()/MoveToFront()/ MoveToBack() are std::list::splice(), which relinks a node without copying it, so iterators to it stay valid. The memory test at the end of the legacy program (strings allocated with new[] that need DestroyArrayList()) becomes a std::list<std::string>, which frees everything in its destructor.

Legacy calls in this program

LegacyModern C++StandardNotes
gxList, gxListNodestd::list; splice() for MoveBefore()/MoveAfter()C++98The list owns its nodes: no DestroyList()/DestroyArrayList().
new / deleteautomatic objects, containers, std::unique_ptr / std::make_uniqueC++11 / C++14Ownership is in the type, and release() shows a hand-over.
strcpy(), strncpy(), memmove() into char arraysstd::string; std::array<char, N> + string_view::copy()C++11 / C++17

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_general/doubly_linked_list/testprog.cpp 194 lines
#include "gxdlcode.h"

#if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library
#include <iostream>
using namespace std; // Use unqualified names for Standard C++ library
#else // Use the old iostream library by default
#include <iostream.h>
#endif // __USE_ANSI_CPP__

#include <string.h>
#include "gxlist.h"

#ifdef __MSVC_DEBUG__
#include "leaktest.h"
#endif

void PausePrg()
{
  // Pause the program until enter is pressed
  cout << "\n";
  cout << "Press Enter to continue..." << "\n";
  cin.get();
}

void ScrollForward(gxList<char> &list)
{
  cout << "Walking through the list..." << "\n";
  gxListNode<char> *ptr = list.GetHead();
  while(ptr) {
    cout << ptr->data << ' ';
    ptr = ptr->next;
  } 
  cout << "\n";
}

void RewindList(gxList<char> &list)
{
  cout << "Walking through the list in a backward direction..." << "\n";
  gxListNode<char> *ptr = list.GetTail();

  // Walk through the list in a backward direction
  while(ptr) {
    cout << ptr->data << ' ';
    ptr = ptr->prev;
  } 

  cout << "\n";
}

int main()
{
#ifdef __MSVC_DEBUG__
  InitLeakTest();
#endif

  gxList<char> list;
  gxListNode<char> *ptr;

  // Construct some list nodes
  cout << "Loading data elements into linked list..." << "\n";
  gxListNode<char> *na = new gxListNode<char>;
  gxListNode<char> *nb = new gxListNode<char>;
  gxListNode<char> *nc = new gxListNode<char>;
  gxListNode<char> *nd = new gxListNode<char>;
  gxListNode<char> *n1 = new gxListNode<char>;
  gxListNode<char> *n2 = new gxListNode<char>;
  gxListNode<char> *n3 = new gxListNode<char>;
  gxListNode<char> *n4 = new gxListNode<char>;

  // Statically allocated list data
  const unsigned num_blocks = 9;
  char buf[num_blocks];
  unsigned i;

  // Load some values into the buffer starting with ASCII character 'A'
  for(i = 0; i < num_blocks; i++) buf[i] = (char)(65+i); 
  
  na->data = buf[0];
  nb->data = buf[1];
  nc->data = buf[2];
  nd->data = buf[3];

  // Load some list elements
  list.InsertAfter(list.GetHead(), na);
  list.InsertAfter(na, nb);
  list.InsertAfter(nb, nc);
  list.InsertAfter(nc, nd);
  
  n1->data = buf[4];
  n2->data = buf[5];
  n3->data = buf[6];
  n4->data = buf[7];
  
  // Testing insert functions
  list.InsertBefore(na, n1);
  list.InsertBefore(nc, n2);
  list.InsertAfter(nb, n3);
  list.InsertAfter(nd, n4);

  // Reordering the list
  list.MoveToBack(na);
  list.MoveAfter(na, nc);
  list.MoveBefore(nc, nb);
  list.MoveAfter(nc, nd);
  list.MoveAfter(nd, n2);
  list.MoveBefore(n2, n1);
  list.MoveAfter(n2, n3);
  list.MoveAfter(n3, n4);
  
  ScrollForward(list);
  RewindList(list);

  PausePrg();

  cout << "Adding another item to the list..." << "\n";
  ptr = list.Add(buf[8]);
  ScrollForward(list);

  PausePrg();

  cout << "Moving list item to the front of the list" << "\n";
  list.MoveToFront(ptr);
  ScrollForward(list);

  cout << "Moving item to the back of the list..." << "\n";
  list.MoveToBack(ptr);
  ScrollForward(list);
  
  PausePrg();
  
  cout << "Removing the head of the list..." << "\n";
  ptr = list.GetHead();
  list.Remove(ptr);
  ScrollForward(list);

  cout << "Removing the tail of the list..." << "\n";
  ptr = list.GetTail();
  list.Remove(ptr);
  ScrollForward(list);

  // NOTE: A gxList::ClearList() call is used here to free the memory
  // allocated for the node since the node data was statically allocated.
  list.ClearList();
  
  PausePrg();

  cout << "Testing for memory leaks..." << "\n";
  gxList<char *> dlist;
  const char *test_str = "The quick brown fox jumps over the lazy dog"; 
  unsigned len = strlen(test_str);
  const unsigned num_to_allocate = 10 * 100;
  const unsigned mem_bufs = \
    (sizeof(gxListNode<char *>) + len) * num_to_allocate;
  cout << "Allocating " << mem_bufs << " bytes..." << "\n";
  
  unsigned count1 = 0;
  unsigned count2 = 0;
  for(i = 0; i < num_to_allocate; i++) {
    char *sbuf = new char[len+1]; // Allocate memory for the string 
    count1 += sizeof(sbuf);
    if(!sbuf) {
      cout << "Memory allocation error at " << (count1+count2) << " bytes"
	   << "\n";
      break;
    }
    memmove(sbuf, test_str, len);
    sbuf[len] = 0; // Null terminate the string
    count2 += sizeof(gxListNode<char *>);
    if(!dlist.Add(sbuf))  {
      cout << "Memory allocation error at " << (count1+count2) << " bytes"
	   << "\n";
      delete[] sbuf; // Not in the list, so free it here
      break;
    }
  }
  cout << "Done" << "\n";
  PausePrg();

  cout << "Releasing memory allocated back to the heap..." << "\n";
  // 10/24/2026: The strings were allocated with new[], so free them with
  // DestroyArrayList(). DestroyList() uses a scalar delete and is only
  // for data allocated with a single-object new.
  dlist.DestroyArrayList();

  PausePrg();
  
  cout << "Exiting..." << "\n";
  cout << "\n";
  return 0;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Modern C++ (C++11)

examples_modern/general/doubly_linked_list/modern.cpp 68 lines
#include <iostream>
#include <iterator>
#include <list>
#include <string>

void scroll_forward(const std::list<char> &list)
{
  std::cout << "Walking through the list...\n";
  for(char c : list) std::cout << c << ' ';
  std::cout << "\n";
}

void rewind_list(const std::list<char> &list)
{
  std::cout << "Walking through the list in a backward direction...\n";
  for(auto it = list.rbegin(); it != list.rend(); ++it) std::cout << *it << ' ';
  std::cout << "\n";
}

int main()
{
  std::list<char> list;
  std::cout << "Loading data elements into linked list...\n";
  auto na = list.insert(list.end(), 'A');  // InsertAfter() at the end
  auto nb = list.insert(list.end(), 'B');
  auto nc = list.insert(list.end(), 'C');
  auto nd = list.insert(list.end(), 'D');
  list.insert(na, 'E');                    // InsertBefore(na, n1)
  list.insert(nc, 'F');                    // InsertBefore(nc, n2)
  list.insert(std::next(nb), 'G');         // InsertAfter(nb, n3)
  list.insert(std::next(nd), 'H');         // InsertAfter(nd, n4)
  scroll_forward(list);

  std::cout << "Reordering the list with splice()...\n";
  list.splice(list.end(), list, na);              // MoveToBack(na)
  list.splice(std::next(na), list, nc);           // MoveAfter(na, nc)
  list.splice(nc, list, nb);                      // MoveBefore(nc, nb)
  scroll_forward(list);
  rewind_list(list);

  std::cout << "Adding another item to the list...\n";
  auto ptr = list.insert(list.end(), 'I');        // Add()
  scroll_forward(list);
  std::cout << "Moving list item to the front of the list\n";
  list.splice(list.begin(), list, ptr);           // MoveToFront()
  scroll_forward(list);
  std::cout << "Moving item to the back of the list...\n";
  list.splice(list.end(), list, ptr);             // MoveToBack()
  scroll_forward(list);

  std::cout << "Removing the head and the tail of the list...\n";
  list.pop_front();
  list.pop_back();
  scroll_forward(list);
  list.clear();                                   // ClearList()

  std::cout << "Allocating 1000 strings...\n";
  std::list<std::string> dlist;
  for(int i = 0; i < 1000; i++)
    dlist.emplace_back("The quick brown fox jumps over the lazy dog");
  std::cout << "List holds " << dlist.size() << " strings\n";
  std::cout << "Releasing memory: nothing to do, ~list() frees it\n";
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=11
cmake --build build-modern --target general_doubly_linked_list
./build-modern/general_doubly_linked_list
# or without CMake:
g++ -std=c++11 -pthread -o general_doubly_linked_list examples_modern/general/doubly_linked_list/modern.cpp

Output of the modern program

From run_modern_tests.sh on RHEL 10, GCC 14, C++20.

Loading data elements into linked list...
Walking through the list...
E A B G F C D H 
Reordering the list with splice()...
Walking through the list...
E G F D H A B C 
Walking through the list in a backward direction...
C B A H D F G E 
Adding another item to the list...
Walking through the list...
E G F D H A B C I 
Moving list item to the front of the list
Walking through the list...
I E G F D H A B C 
Moving item to the back of the list...
Walking through the list...
E G F D H A B C I 
Removing the head and the tail of the list...
Walking through the list...
G F D H A B C 
Allocating 1000 strings...
List holds 1000 strings
Releasing memory: nothing to do, ~list() frees it