<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-4%3Astsa027</id>
	<title>SE250:lab-4:stsa027 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-4%3Astsa027"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:stsa027&amp;action=history"/>
	<updated>2026-04-29T06:00:40Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE250:lab-4:stsa027&amp;diff=6259&amp;oldid=prev</id>
		<title>Mark: 7 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:stsa027&amp;diff=6259&amp;oldid=prev"/>
		<updated>2008-11-03T05:19:38Z</updated>

		<summary type="html">&lt;p&gt;7 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;===Writing the int length(Cons*) function:===&lt;br /&gt;
&lt;br /&gt;
I reused the code already provided in the for loop in LL.c, and simply added an extra variable.  I initially called the variable length, same as the function name and was told this was not a good idea.  So, it was changed to len.&lt;br /&gt;
&lt;br /&gt;
I then copied and paseted the test code provided by the lecturer into the main function, and it was a success.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Code used:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int length(Cons* list) {&lt;br /&gt;
    int len = 0;&lt;br /&gt;
    for( ; list != nil ; list = list-&amp;gt; tail)&lt;br /&gt;
	 len++;&lt;br /&gt;
    return len;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Writing the element_t first to fourth (Cons*) function:===&lt;br /&gt;
&lt;br /&gt;
Even for the first one, I was slightly confused about what to do.  The lecturer gave me the advice of NOT using a for loop as I was attempting, since all you have to do was return the head of the first element of the list.  He also said that since nil is a valid cons cell in inself, with a head of 0 and a tail pointing to another nil cons cell, the following&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    if (list == nil)                            //redundant&lt;br /&gt;
	return 0;                               //redundant&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
is not really required.&lt;br /&gt;
&lt;br /&gt;
For writing the second function, I first tried this:&lt;br /&gt;
&lt;br /&gt;
   element_t second(Cons* list) {&lt;br /&gt;
    if (list == nil)&lt;br /&gt;
	return 0;&lt;br /&gt;
    &lt;br /&gt;
    return &lt;br /&gt;
    element_t a;&lt;br /&gt;
    for( ; list != nil ; list = list-&amp;gt;tail)    &lt;br /&gt;
	a = list-&amp;gt;head;&lt;br /&gt;
    return a;&lt;br /&gt;
&lt;br /&gt;
which worked, but is a rather complicated way of doing things, as I was told by the tutor.  Instead, I was advised to use this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    return list-&amp;gt;tail-&amp;gt;head;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A similar thing was done with returning third and forth element_t.&lt;br /&gt;
&lt;br /&gt;
===Writing the element_t nth(int i, Cons*) function:===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Writing the Cons* find(element_t, Cons*) function:===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Writing the Cons* copy_list(Cons* xs, Cons* xs) function:===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Cons* copy_list (Cons* xs){&lt;br /&gt;
  Cons* copy = nil;&lt;br /&gt;
  Cons* xs_orig = xs;&lt;br /&gt;
  int j = 0;&lt;br /&gt;
  int i = (length(xs_orig))-1;&lt;br /&gt;
&lt;br /&gt;
  while(length(copy)&amp;lt;length(xs_orig)){&lt;br /&gt;
    while(j!=i){&lt;br /&gt;
      j++;&lt;br /&gt;
      xs = xs-&amp;gt;tail;&lt;br /&gt;
    }&lt;br /&gt;
    copy = cons((xs-&amp;gt;head), copy);&lt;br /&gt;
    i--;&lt;br /&gt;
    xs = xs_orig;&lt;br /&gt;
    j=0;&lt;br /&gt;
  }&lt;br /&gt;
  return copy;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Writing the Cons* nappend(Cons* xs, Cons* xs) function:===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Cons* nappend(Cons* xs, Cons* ys){&lt;br /&gt;
    Cons* both = xs;&lt;br /&gt;
&lt;br /&gt;
    if (xs == nil)&lt;br /&gt;
	return ys;&lt;br /&gt;
    while(both-&amp;gt;tail!=nil){&lt;br /&gt;
        both = both-&amp;gt;tail;&lt;br /&gt;
    }&lt;br /&gt;
    both-&amp;gt;tail = ys;&lt;br /&gt;
    return xs;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Writing the Cons* reverse(Cons* xs, Cons* xs) function:===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Cons* reverse(Cons* xs){&lt;br /&gt;
    Cons* rev = nil;&lt;br /&gt;
    for (; xs!=nil; xs = xs-&amp;gt;tail){&lt;br /&gt;
	rev = cons((xs-&amp;gt;head), rev);      //points rev to a new cons cell using the next xs-&amp;gt;head, and original address of rev&lt;br /&gt;
    }&lt;br /&gt;
    return rev;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Writing the Cons* nreverse(Cons* xs, Cons* xs) function:===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Cons* nreverse(Cons* xs){&lt;br /&gt;
    int i = length(xs);&lt;br /&gt;
    Cons* copy = xs;&lt;br /&gt;
    Cons* copyi = xs;&lt;br /&gt;
    while(xs-&amp;gt;tail!=nil)&lt;br /&gt;
	xs = xs-&amp;gt;tail;                  //makes xs point to the last element of the array list which has a tail that points to null&lt;br /&gt;
    while (copy-&amp;gt;tail!=nil){&lt;br /&gt;
	copyi = copy;                   //copyi is arranged to be pointing to the element before copy&lt;br /&gt;
	copy = copy-&amp;gt;tail;              //copy points to the last element (which has a tail that points to null)&lt;br /&gt;
	if(copy-&amp;gt;tail==nil){&lt;br /&gt;
	    copy-&amp;gt;tail = copyi;&lt;br /&gt;
	    copyi-&amp;gt;tail = nil;&lt;br /&gt;
	    break;&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
    return xs;&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>