<?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%3Ahpan027</id>
	<title>SE250:lab-4:hpan027 - 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%3Ahpan027"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:hpan027&amp;action=history"/>
	<updated>2026-04-28T12:13:21Z</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:hpan027&amp;diff=6069&amp;oldid=prev</id>
		<title>Mark: 4 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:hpan027&amp;diff=6069&amp;oldid=prev"/>
		<updated>2008-11-03T05:19:34Z</updated>

		<summary type="html">&lt;p&gt;4 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Straightforward coding lab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Length ==&lt;br /&gt;
 int length( Cons* list) {&lt;br /&gt;
     int i;&lt;br /&gt;
     for ( i = 0; list != nil ; i++) {&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return i;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Kept a counter and went through the list until the end, pretty simple&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== First/Second/Third/Fourth ==&lt;br /&gt;
&lt;br /&gt;
 element_t first( Cons* list ) {&lt;br /&gt;
     return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Well...&lt;br /&gt;
&lt;br /&gt;
 element_t second( Cons* list ) {&lt;br /&gt;
     int i;&lt;br /&gt;
     for ( i = 0; i &amp;lt; 1; i++) {&lt;br /&gt;
 	if (list == nil)&lt;br /&gt;
 	    return 0;&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*This time pass over the first element&lt;br /&gt;
&lt;br /&gt;
 element_t third( Cons* list ) {&lt;br /&gt;
     int i;&lt;br /&gt;
     for ( i = 0; i &amp;lt; 2; i++) {&lt;br /&gt;
 	if (list == nil)&lt;br /&gt;
 	    return 0;&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 element_t fourth( Cons* list ) {&lt;br /&gt;
     int i;&lt;br /&gt;
     for ( i = 0; i &amp;lt; 3; i++) {&lt;br /&gt;
 	if (list == nil)&lt;br /&gt;
 	    return 0;&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*And so on&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Nth ==&lt;br /&gt;
&lt;br /&gt;
 element_t nth( int i, Cons* list ) {&lt;br /&gt;
     for ( ; i &amp;gt; 0; i--) {&lt;br /&gt;
 	if (list == nil)&lt;br /&gt;
 	    return 0;&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Same code, except no need for a counter&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Equal ==&lt;br /&gt;
&lt;br /&gt;
 int equal( Cons* first, Cons* second ) {&lt;br /&gt;
     while ( first-&amp;gt;head == second-&amp;gt;head ) {&lt;br /&gt;
 	if (first-&amp;gt;tail == nil &amp;amp;&amp;amp; second-&amp;gt;tail == nil)&lt;br /&gt;
 	    return 1;&lt;br /&gt;
 	first = first-&amp;gt;tail;&lt;br /&gt;
 	second = second-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Again, not much to say. Go through each list until the elements are different or until the end is reached&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Find ==&lt;br /&gt;
&lt;br /&gt;
 Cons* find( element_t value, Cons* list) {&lt;br /&gt;
     while (list-&amp;gt;head != value) {&lt;br /&gt;
 	if (list-&amp;gt;tail == nil)&lt;br /&gt;
 	    return nil;&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
     }&lt;br /&gt;
     return list;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Instructions weren&amp;#039;t too clear as to what happens if a list has multiple elements of same value - assumed to be the first that comes up&lt;br /&gt;
*Code goes through list until the value is found, and return the cell at which it was found&lt;br /&gt;
*Otherwise return nil&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Copy list ==&lt;br /&gt;
&lt;br /&gt;
 Cons* copy_list( Cons* list ) {&lt;br /&gt;
     Cons* copied = nil;&lt;br /&gt;
     int size = length(list);&lt;br /&gt;
     int i;&lt;br /&gt;
 &lt;br /&gt;
     for ( i = 1; i &amp;lt; size + 1 ; i++ ) {&lt;br /&gt;
 	copied = cons(nth(size-i,list), copied);&lt;br /&gt;
     }&lt;br /&gt;
     return copied;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*This was a bit hard to do&lt;br /&gt;
*At first I pulled out every element in the list and stored into an array and then remade the list using cons (seeing as you have to start with the last value in the list when using cons)&lt;br /&gt;
*But then I realised I wrote an nth function and rewrote the code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Append ==&lt;br /&gt;
&lt;br /&gt;
 Cons* append( Cons* xs, Cons* ys ) {&lt;br /&gt;
     int size = length(xs);&lt;br /&gt;
     int i;&lt;br /&gt;
 &lt;br /&gt;
     for ( i = 1 ; i &amp;lt; size + 1; i++ ) &lt;br /&gt;
 	ys = cons( nth( size-i, xs ), ys );&lt;br /&gt;
 &lt;br /&gt;
     return ys;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Pretty much identical code to above&lt;br /&gt;
*The instruction says xs/ys is not to be modified - however, seeing as these are simply local copies of the original lists, I don&amp;#039;t really see how they can be modified&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Nappend ==&lt;br /&gt;
&lt;br /&gt;
*I wasn&amp;#039;t sure how to do this without passing the function the address of xs seeing as any changes done within the function is lost once you are back in the main programme&lt;br /&gt;
*This would have required me to change the function to &lt;br /&gt;
 Cons* nappend( Cons** xs, Cons* ys ) &lt;br /&gt;
and I wasn&amp;#039;t sure if this was what he had in mind&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reverse ==&lt;br /&gt;
&lt;br /&gt;
 Cons* reverse( Cons* xs ) {&lt;br /&gt;
     Cons* reversed = nil;&lt;br /&gt;
     int size = length(xs);&lt;br /&gt;
     int i = 0;&lt;br /&gt;
 &lt;br /&gt;
     for ( i = 0 ; i &amp;lt; size; i++ )&lt;br /&gt;
 	reversed = cons( nth( i, xs), reversed );&lt;br /&gt;
     &lt;br /&gt;
     return reversed;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
*Like append, copylist etc except without having to start from the end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Nreverse ==&lt;br /&gt;
&lt;br /&gt;
*Same problem here as nappend - I&amp;#039;m not sure how I can modify the list without passing the function the address of the list&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>