<?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%3Ahals016</id>
	<title>SE250:lab-4:hals016 - 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%3Ahals016"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-4:hals016&amp;action=history"/>
	<updated>2026-04-29T07:25:32Z</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:hals016&amp;diff=6059&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:hals016&amp;diff=6059&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;Lab 4&lt;br /&gt;
&lt;br /&gt;
I started coding the length method by writing a for loop similiar to the one in the print_list method. The difference is that there needed to be a counter to keep track of all the elements.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int length(Cons* list) {&lt;br /&gt;
  int i;&lt;br /&gt;
  if (list-&amp;gt;tail == 0)&lt;br /&gt;
    return 0;&lt;br /&gt;
  else {&lt;br /&gt;
    for (i = 0; list != nil; list = list-&amp;gt;tail ) {&lt;br /&gt;
      i++;&lt;br /&gt;
    }&lt;br /&gt;
    return i;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When writing code to check the first element in the cons cell, I first made a condition that if the &amp;#039;list-&amp;gt;==nil&amp;#039; then 0 would be returned. Else &amp;#039;list-&amp;gt;head&amp;#039; would be returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
element_t first(Cons* list) {&lt;br /&gt;
  if (list-&amp;gt;tail == nil)&lt;br /&gt;
    return 0;&lt;br /&gt;
  else&lt;br /&gt;
    return list-&amp;gt;head;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Doing the second element was harder because it required a loop, I used the same loop and conditions for the third and fourth element but with the condition incremented slightly for each method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
element_t second(Cons* list) {&lt;br /&gt;
  int i;&lt;br /&gt;
  if (list-&amp;gt;tail == nil)&lt;br /&gt;
    return 0;&lt;br /&gt;
  for (i = 0; i&amp;lt;1; list = list-&amp;gt;tail ) {&lt;br /&gt;
    i++;&lt;br /&gt;
  }&lt;br /&gt;
  if (!(list-&amp;gt;head==0))&lt;br /&gt;
    return list-&amp;gt;head;&lt;br /&gt;
  else&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code for the nth element was straightforward because a template of element at second can be used with the for loop using the variable that indicats which element wants to be viewed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
element_t nth(int n,Cons* list) {&lt;br /&gt;
  int i;&lt;br /&gt;
  if (list-&amp;gt;tail == nil &amp;amp;&amp;amp; n&amp;gt;0 )&lt;br /&gt;
    return 0;&lt;br /&gt;
  else {&lt;br /&gt;
    for (i = 0; i&amp;lt;(n-1); list = list-&amp;gt;tail ) {&lt;br /&gt;
      i++;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  if (!(list-&amp;gt;head==0))&lt;br /&gt;
    return list-&amp;gt;head;&lt;br /&gt;
  else&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>