<?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%3Ajpar277</id>
	<title>SE250:lab-4:jpar277 - 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%3Ajpar277"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:jpar277&amp;action=history"/>
	<updated>2026-04-29T02:23:34Z</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:jpar277&amp;diff=6077&amp;oldid=prev</id>
		<title>Mark: 1 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:jpar277&amp;diff=6077&amp;oldid=prev"/>
		<updated>2008-11-03T05:19:35Z</updated>

		<summary type="html">&lt;p&gt;1 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Lab Overview ==&lt;br /&gt;
Well, I think the tasks weren&amp;#039;t too difficult, maybe a little time consuming and brain racking - seriously, this Lab required so much thinking!&lt;br /&gt;
It was kind of John to give us all the test code - I actually didn&amp;#039;t find out until I got to the equals function XD!!&lt;br /&gt;
&lt;br /&gt;
== Length Function ==&lt;br /&gt;
 int length(Cons* list) {&lt;br /&gt;
 	int i = 0;&lt;br /&gt;
 	while (list != nil) {&lt;br /&gt;
 		i++;&lt;br /&gt;
 		list = list-&amp;gt;tail;&lt;br /&gt;
 	}&lt;br /&gt;
 	return i;&lt;br /&gt;
 }&lt;br /&gt;
Thats my code for the length function, not really anything to say about it except that I forgot about the list part lol&lt;br /&gt;
my first attempt was&lt;br /&gt;
 int length(Cons*) {&lt;br /&gt;
 	int i = 0;&lt;br /&gt;
 	while (Cons != nil) {&lt;br /&gt;
 		i++;&lt;br /&gt;
 		Cons = Cons-&amp;gt;tail;&lt;br /&gt;
 	}&lt;br /&gt;
 	return i;&lt;br /&gt;
 }&lt;br /&gt;
very very wrong!&lt;br /&gt;
&lt;br /&gt;
== First Element Function ==&lt;br /&gt;
 element_t first(Cons* list) {&lt;br /&gt;
 	return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
Rather obvious.&lt;br /&gt;
&lt;br /&gt;
== Second Element Function ==&lt;br /&gt;
 element_t second(Cons* list) {&lt;br /&gt;
 	list = list-&amp;gt;tail;&lt;br /&gt;
 	return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
We want to move to the second element which is why the line &amp;quot;list = list-&amp;gt;tail;&amp;quot; is there, that will move us to the next element.&lt;br /&gt;
I could&amp;#039;ve reduced it down to 1 line &amp;quot;return list-&amp;gt;tail-&amp;gt;head&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Third Element Function ==&lt;br /&gt;
 element_t third(Cons* list) {&lt;br /&gt;
 	int i = 0;&lt;br /&gt;
 	while (i != 2) {&lt;br /&gt;
 		list = list-&amp;gt;tail;&lt;br /&gt;
 		i++;&lt;br /&gt;
 	}&lt;br /&gt;
 	return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
We want to move to the third element this time, preparing myself for the &amp;quot;nth element function&amp;quot;, I decided to use a loop.&lt;br /&gt;
Again, I could&amp;#039;ve reduced it down to 1 line &amp;quot;return list-&amp;gt;tail-&amp;gt;tail-&amp;gt;head&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Fourth Element Function ==&lt;br /&gt;
 element_t fourth(Cons* list) {&lt;br /&gt;
 	int i = 0;&lt;br /&gt;
 	while (i != 3) {&lt;br /&gt;
 		list = list-&amp;gt;tail;&lt;br /&gt;
 		i++;&lt;br /&gt;
 	}&lt;br /&gt;
 	return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
Like the third element function, I used a loop. Could&amp;#039;ve reduced the code down to 1 line once more &amp;quot;return list-&amp;gt;tail-&amp;gt;-&amp;gt;tail-&amp;gt;head&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== nth Element Function ==&lt;br /&gt;
 element_t nth(int i, Cons* list) {&lt;br /&gt;
 	int j = 0;&lt;br /&gt;
 	while (j != i) {&lt;br /&gt;
 		list = list-&amp;gt;tail;&lt;br /&gt;
 		j++;&lt;br /&gt;
 	}&lt;br /&gt;
 	return list-&amp;gt;head;&lt;br /&gt;
 }&lt;br /&gt;
Using the loop structure in the previous first~fourth element functions, I created a similar loop using the algebraic expression i.&lt;br /&gt;
&lt;br /&gt;
== Equal Function ==&lt;br /&gt;
 int equal(Cons* list1, Cons* list2) {&lt;br /&gt;
 	int i = 0;&lt;br /&gt;
 	int j = 0;&lt;br /&gt;
 	i = length(list1);&lt;br /&gt;
 	if (i == length(list2)) {&lt;br /&gt;
 		while (j != i) {&lt;br /&gt;
 			if (list1-&amp;gt;head != list2-&amp;gt;head) {&lt;br /&gt;
 				return 0;&lt;br /&gt;
 			} else {&lt;br /&gt;
 				list1 = list1-&amp;gt;tail;&lt;br /&gt;
 				list2 = list2-&amp;gt;tail;&lt;br /&gt;
 				j++;			&lt;br /&gt;
 			}&lt;br /&gt;
 		}&lt;br /&gt;
 		return 1;&lt;br /&gt;
 	}&lt;br /&gt;
 	return 0;&lt;br /&gt;
 }&lt;br /&gt;
It took me a while to get my head around this one. Required so much thinking!&lt;br /&gt;
First I wanted to check if the lengths of the two lists were the same, if they&amp;#039;re not then theres no point going through them. Then we want to go through each element in both lists and compare them.&lt;br /&gt;
&lt;br /&gt;
== Find Function ==&lt;br /&gt;
 Cons* find(element_t i, Cons* list) {&lt;br /&gt;
 	int j = 0;&lt;br /&gt;
 	int k = 0;&lt;br /&gt;
 	j = length(list);&lt;br /&gt;
 	while (k != j) {&lt;br /&gt;
 		if (i == list-&amp;gt;head) {&lt;br /&gt;
 			return list;&lt;br /&gt;
 		}&lt;br /&gt;
 		list = list-&amp;gt;tail;&lt;br /&gt;
 		k++;&lt;br /&gt;
 	}&lt;br /&gt;
 	return list;&lt;br /&gt;
 }&lt;br /&gt;
Basically, the while loop will go through each element in the list and compare it with the element we&amp;#039;re looking for (i in my code). It will exit the loop when it reaches the end, obviously if its reached the end, it hasn&amp;#039;t found the element and because we&amp;#039;re at the end, it will return and empty list.&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>