Template:LL.c

From Marks Wiki
Jump to navigation Jump to search

<html> <head>

 <title>LL.c</title>

</head> <body bgcolor="#ffffff" text="#000000">

<a name="line1"> 1</a> <font color="#444444">/*
<a name="line2"> 2</a>   File: LL.c
<a name="line3"> 3</a>   Date: 1 April 2008

<a name="line4"> 4</a>   Author: John Hamer
<a name="line5"> 5</a>   Purpose: Starter code for SOFTENG 250 lab 4, 2008
<a name="line6"> 6</a> */</font>
<a name="line7"> 7</a> 
<a name="line8"> 8</a> <font color="0000ff"><strong>#include <font color="#008000"><assert.h></font></strong></font>

<a name="line9"> 9</a> <font color="0000ff"><strong>#include <font color="#008000"><stdlib.h></font></strong></font>
<a name="line10">10</a> <font color="0000ff"><strong>#include <font color="#008000"><stdio.h></font></strong></font>
<a name="line11">11</a> 
<a name="line12">12</a> <strong>typedef</strong> <strong>int</strong> <font color="#2040a0">element_t</font><font color="4444FF">;</font>

<a name="line13">13</a> 
<a name="line14">14</a> <strong>typedef</strong>
<a name="line15">15</a> <strong>struct</strong> <font color="#2040a0">Cons</font> <font color="4444FF"><strong>{</strong></font>
<a name="line16">16</a>   <font color="#2040a0">element_t</font> <font color="#2040a0">head</font><font color="4444FF">;</font>

<a name="line17">17</a>   <strong>struct</strong> <font color="#2040a0">Cons</font><font color="4444FF">*</font> <font color="#2040a0">tail</font><font color="4444FF">;</font>
<a name="line18">18</a> <font color="4444FF"><strong>}</strong></font> <font color="#2040a0">Cons</font><font color="4444FF">;</font>

<a name="line19">19</a> 
<a name="line20">20</a> <strong>static</strong> <font color="#2040a0">Cons</font> <font color="#2040a0">NIL</font> <font color="4444FF">=</font> <font color="4444FF"><strong>{</strong></font> <font color="#FF0000">0</font>, <font color="4444FF">&</font><font color="#2040a0">NIL</font> <font color="4444FF"><strong>}</strong></font><font color="4444FF">;</font>

<a name="line21">21</a> <font color="#2040a0">Cons</font><font color="4444FF">*</font> <font color="#2040a0">nil</font> <font color="4444FF">=</font> <font color="4444FF">&</font><font color="#2040a0">NIL</font><font color="4444FF">;</font>
<a name="line22">22</a> 
<a name="line23">23</a> 
<a name="line24">24</a> <font color="#2040a0">Cons</font><font color="4444FF">*</font> <font color="#2040a0">cons</font><font color="4444FF">(</font> <font color="#2040a0">element_t</font> <font color="#2040a0">elt</font>, <font color="#2040a0">Cons</font><font color="4444FF">*</font> <font color="#2040a0">tail</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>

<a name="line25">25</a>   <font color="#2040a0">Cons</font><font color="4444FF">*</font> <font color="#2040a0">cell</font> <font color="4444FF">=</font> <font color="#2040a0">malloc</font><font color="4444FF">(</font> <strong>sizeof</strong><font color="4444FF">(</font><font color="#2040a0">Cons</font><font color="4444FF">)</font> <font color="4444FF">)</font><font color="4444FF">;</font>

<a name="line26">26</a>   <font color="#2040a0">assert</font><font color="4444FF">(</font> <font color="#2040a0">cell</font> <font color="4444FF">!</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">)</font><font color="4444FF">;</font>
<a name="line27">27</a>   <font color="#2040a0">cell</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">head</font> <font color="4444FF">=</font> <font color="#2040a0">elt</font><font color="4444FF">;</font>

<a name="line28">28</a>   <font color="#2040a0">cell</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">tail</font> <font color="4444FF">=</font> <font color="#2040a0">tail</font><font color="4444FF">;</font>
<a name="line29">29</a>   <strong>return</strong> <font color="#2040a0">cell</font><font color="4444FF">;</font>

<a name="line30">30</a> <font color="4444FF"><strong>}</strong></font>
<a name="line31">31</a> 
<a name="line32">32</a> <strong>void</strong> <font color="#2040a0">print_list</font><font color="4444FF">(</font> <font color="#2040a0">Cons</font><font color="4444FF">*</font> <font color="#2040a0">list</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>

<a name="line33">33</a>   <strong>char</strong><font color="4444FF">*</font> <font color="#2040a0">sep</font> <font color="4444FF">=</font> <font color="#008000">""</font><font color="4444FF">;</font>
<a name="line34">34</a>   <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"List["</font> <font color="4444FF">)</font><font color="4444FF">;</font>

<a name="line35">35</a>   <strong>for</strong><font color="4444FF">(</font> <font color="4444FF">;</font> <font color="#2040a0">list</font> <font color="4444FF">!</font><font color="4444FF">=</font> <font color="#2040a0">nil</font><font color="4444FF">;</font> <font color="#2040a0">list</font> <font color="4444FF">=</font> <font color="#2040a0">list</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">tail</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>

<a name="line36">36</a>     <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"%s%d"</font>, <font color="#2040a0">sep</font>, <font color="#2040a0">list</font><font color="4444FF">-</font><font color="4444FF">></font><font color="#2040a0">head</font> <font color="4444FF">)</font><font color="4444FF">;</font>
<a name="line37">37</a>     <font color="#2040a0">sep</font> <font color="4444FF">=</font> <font color="#008000">","</font><font color="4444FF">;</font>

<a name="line38">38</a>   <font color="4444FF"><strong>}</strong></font>
<a name="line39">39</a>   <font color="#2040a0">printf</font><font color="4444FF">(</font> <font color="#008000">"]"</font> <font color="4444FF">)</font><font color="4444FF">;</font>
<a name="line40">40</a> <font color="4444FF"><strong>}</strong></font>

<a name="line41">41</a> 
<a name="line42">42</a> 
<a name="line43">43</a> <strong>int</strong> <font color="#2040a0">main</font><font color="4444FF">(</font> <font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>
<a name="line44">44</a>   <font color="#2040a0">print_list</font><font color="4444FF">(</font> <font color="#2040a0">cons</font><font color="4444FF">(</font><font color="#FF0000">1</font>,<font color="#2040a0">cons</font><font color="4444FF">(</font><font color="#FF0000">2</font>,<font color="#2040a0">cons</font><font color="4444FF">(</font><font color="#FF0000">3</font>,<font color="#2040a0">nil</font><font color="4444FF">)</font><font color="4444FF">)</font><font color="4444FF">)</font> <font color="4444FF">)</font><font color="4444FF">;</font> <font color="#444444">/* expect: List[1,2,3] */</font>

<a name="line45">45</a>   <strong>return</strong> <font color="#FF0000">0</font><font color="4444FF">;</font>
<a name="line46">46</a> <font color="4444FF"><strong>}</strong></font>
<a name="line47">47</a> 
<a name="line48">48</a> <font color="#444444">/*
<a name="line49">49</a> Local Variables:

<a name="line50">50</a> compile-command: "gcc -Wall LL.c -o LL && ./LL"
<a name="line51">51</a> End:
<a name="line52">52</a> */</font>


syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1 </body> </html>