    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Difference Structures in C/C++</title>
        <link>https://members.accu.org/index.php/articles/908</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 11, #5 - Aug 1999</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c130/">115</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Difference Structures in C/C++</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 August 1999 13:15:32 +01:00 or Tue, 03 August 1999 13:15:32 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="section" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>Difference structures are a Prolog technique but they can be
adapted to C and C++ as follows: Imagine having a linked list, but
as well as a pointer to the head of the list you also have a
pointer to its last element. (When the list is empty or there is
only one item, the two pointers will be equal.) Code to maintain
such a list shouldn't be too difficult if you can write linked list
code, so I'll leave it as an exercise but I'll assume you're
using:</p>
<pre class="programlisting">
typedef struct Item {
  void* data;
  struct Item* next;
} Item;
typedef struct {
  Item* firstElement;
  Item* lastElement;
} List;
</pre>
<p>(In C++ you would of course use a class with methods and
possibly a template, but I won't do that here for the benefit of
the C programmers.)</p>
<p>With a pointer to the last element, you can do some powerful
things. For example, you can concatenate two lists very
quickly:</p>
<pre class="programlisting">
void concat(List* first, const List* second) {
  if (first-&gt;lastElement) first-&gt;lastElement-&gt;next=second-&gt;firstElement;
  if (second-&gt;lastElement) first-&gt;lastElement=second-&gt;lastElement;
}
</pre>
<p>(Note that no copy of the second list is made.) Or if you want a
queue:</p>
<pre class="programlisting">
void queueItem(List* addTo,void* d) {
  Item* i=malloc(sizeof(Item));
  i-&gt;data=d; i-&gt;next=NULL;
  if (addTo-&gt;lastElement) addTo-&gt;lastElement-&gt;next=i;
  else addTo-&gt;firstElement=i;
  addTo-&gt;lastElement=i;
}

void* getItem(List* getFrom) {
  void* ret=NULL;
  Item* i=getFrom-&gt;firstElement;
  if (i) {
    getFrom-&gt;firstElement=getFrom-&gt;firstElement-&gt;next;
    if(!(getFrom-&gt;firstElement)) getFrom-&gt;lastElement=NULL;
    ret=i-&gt;data;
    free(i);
  } return ret;
}
</pre>
<p>Some implementations of queues use doubly-linked lists, but the
above uses less memory. You can also rotate a list quickly (but
only in one direction with single links):</p>
<pre class="programlisting">
void rotate(List* l) {
  if(l-&gt;firstElement) {
    l-&gt;lastElement-&gt;next=l-&gt;firstElement;
    l-&gt;lastElement=l-&gt;firstElement;
    l-&gt;firstElement=l-&gt;firstElement-&gt;next;
    l-&gt;lastElement-&gt;next=NULL;
  }
}
</pre>
<p>When writing code like that you have to keep a clear head. The
order of assignments is crucial - if in doubt, simulate on paper.
Bearing in mind that one or more of the parameters might be an
empty list is also important (although it is reasonable to insist
that the parameters themselves will not be NULL - add asserts if
desired).</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
