<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>You are not welcome in my world! &#187; c</title>
	<atom:link href="http://blog.ayberkyilmaz.net/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.ayberkyilmaz.net</link>
	<description>things that matter</description>
	<lastBuildDate>Fri, 18 Feb 2011 23:34:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>ADTs : Stacks</title>
		<link>http://blog.ayberkyilmaz.net/2010/adts-stacks-140</link>
		<comments>http://blog.ayberkyilmaz.net/2010/adts-stacks-140#comments</comments>
		<pubDate>Fri, 22 Oct 2010 20:35:27 +0000</pubDate>
		<dc:creator>Ayberk Yılmaz</dc:creator>
				<category><![CDATA[Teknik]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[stack]]></category>

		<guid isPermaLink="false">http://blog.ayberkyilmaz.net/?p=140</guid>
		<description><![CDATA[As an exercise, i will design and implement my own stack ADT. First of all we need to build necessary structures. The Head structure will contain a counter and a pointer to stack nodes. The node structure will contain a data pointer and a node pointer. typedef struct node2 &#123; void *dataPtr; struct node2 *nodePtr; [...]]]></description>
			<content:encoded><![CDATA[<p>As an exercise, i will design and implement my own stack ADT.</p>
<p>First of all we need to build necessary structures.</p>
<p>The Head structure will contain a counter and a pointer to stack nodes.</p>
<p>The node structure will contain a data pointer and a node pointer.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> node2
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>dataPtr<span style="color: #339933;">;</span>
   <span style="color: #993333;">struct</span> node2 <span style="color: #339933;">*</span>nodePtr<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> NODE<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">int</span> count<span style="color: #339933;">;</span>
   NODE <span style="color: #339933;">*</span>top<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> STACK<span style="color: #339933;">;</span></pre></div></div>

<p>We have constructed two structures: STACK structure will be the head of the stack. The head structure has a NODE pointer to the top of the stack and an integer to hold the number of the nodes. NODE structure will hold the given data. And of course, it has a link to another node.</p>
<p>Since we have constructed the structures, we can start to implement the operations.</p>
<p>Let me talk about the operations that i will implement. I am going to implement create, push and pop operations. Push adds data to the stack and pop retrieves data. As you know, all the operations affect the top of the stack.</p>
<p>In order to create a stack, we need to allocate the necessary memory and then we must update the head node:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">STACK <span style="color: #339933;">*</span>createStack<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   STACK<span style="color: #339933;">*</span> stack<span style="color: #339933;">;</span>
&nbsp;
   stack <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>STACK<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>STACK<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stack<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      stack<span style="color: #339933;">-&gt;</span>count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
      stack<span style="color: #339933;">-&gt;</span>top <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> stack<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This one is easy to explain. We simply allocate memory for the head node and if allocation is done successfully, we simply set the counter as zero and pointer as NULL due to having no items at the stack.</p>
<p>After successfully creating the stack, the next goal is adding data to the stack. It is a little bit more trivial but still easy to understand. I will not explain the code, not now at least.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// it will return 1 if the operation is successful, 0 otherwise</span>
<span style="color: #666666; font-style: italic;">// thanks to the generic pointer, any type of data can be pushed</span>
<span style="color: #993333;">int</span> pushStack<span style="color: #009900;">&#40;</span>STACK<span style="color: #339933;">*</span> stack<span style="color: #339933;">,</span> <span style="color: #993333;">void</span><span style="color: #339933;">*</span> data<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// allocate memory for the new node</span>
    NODE<span style="color: #339933;">*</span> node<span style="color: #339933;">;</span>
    node <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NODE<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>NODE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// check if allocations is done</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>node<span style="color: #009900;">&#41;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>   
&nbsp;
   <span style="color: #666666; font-style: italic;">// update the node</span>
   node<span style="color: #339933;">-&gt;</span>nodePtr <span style="color: #339933;">=</span> stack<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>top<span style="color: #339933;">;</span>
   node<span style="color: #339933;">-&gt;</span>dataPtr <span style="color: #339933;">=</span> data<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// update the head</span>
   stack<span style="color: #339933;">-&gt;</span>top <span style="color: #339933;">=</span> node<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#40;</span>stack<span style="color: #339933;">-&gt;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">++;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now it is time to retrieve data.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>popStack<span style="color: #009900;">&#40;</span>STACK<span style="color: #339933;">*</span> stack<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>dataOut<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//again, we love you generic pointer</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// empty stack cannot be popped</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stack<span style="color: #339933;">-&gt;</span>count<span style="color: #339933;">==</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
     dataOut <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span>
   <span style="color: #009900;">&#123;</span>
      dataOut <span style="color: #339933;">=</span> stack<span style="color: #339933;">-&gt;</span>head<span style="color: #339933;">-&gt;</span>dataPtr<span style="color: #339933;">;</span>
      NODE<span style="color: #339933;">*</span> temp <span style="color: #339933;">=</span> stack<span style="color: #339933;">-&gt;</span>top<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// we dont want no leaks</span>
      stack<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>top <span style="color: #339933;">=</span> stack<span style="color: #339933;">-&gt;</span>top<span style="color: #339933;">&gt;</span>nodePtr<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#40;</span>stack<span style="color: #339933;">-&gt;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">--;</span>
      free<span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// freedom &lt;3</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">return</span> dataOut<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I know i have not explained neither the code nor the ADT itself well enough. I will try to update this post -most probably i will not- but the whole point of this post is implementing stacks on my own and recapping it. </p>
<p>I have written all the code on wordpress and have not compiled. If you try any errors or if you have any questions, please feel free to ask or inform me. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ayberkyilmaz.net/2010/adts-stacks-140/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Char to Int Conversion in C</title>
		<link>http://blog.ayberkyilmaz.net/2010/char-to-int-conversion-in-c-101</link>
		<comments>http://blog.ayberkyilmaz.net/2010/char-to-int-conversion-in-c-101#comments</comments>
		<pubDate>Thu, 01 Jul 2010 21:01:44 +0000</pubDate>
		<dc:creator>Ayberk Yılmaz</dc:creator>
				<category><![CDATA[Teknik]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[char to int]]></category>
		<category><![CDATA[char to int conversion]]></category>

		<guid isPermaLink="false">http://blog.ayberkyilmaz.net/?p=101</guid>
		<description><![CDATA[The programming thing has a lot of tricks and as a programmer, I like them very very much. The latest trick that i have come across is converting a char digit to its integer value. As you know, if you write something like this: char num[11] = "0123456789"; int s = (int)num[0]; printf("%d\n",s); It will [...]]]></description>
			<content:encoded><![CDATA[<p>The programming thing has a lot of tricks and as a programmer, I like them very very much.</p>
<p>The latest trick that i have come across is converting a char digit to its integer value.</p>
<p>As you know, if you write something like this:</p>
<p><code>char num[11] = "0123456789";<br />
int s = (int)num[0];<br />
printf("%d\n",s);</code></p>
<p>It will not print 0, but instead it will print the <a href="http://www.asciitable.com">ASCII</a> value of &#8217;0&#8242;.</p>
<p>While looking for a quick solution, I have seen this perfect trick at stackoverflow:</p>
<p><code>char num[11] = "0123456789";<br />
int s = num[0] - '0';<br />
printf("%d\n",s);</code></p>
<p>Since all the digits are consecutive at the ASCII Table, -supposing all the elements are digits- substracting the ASCII value of &#8217;0&#8242; from that digit&#8217;s ASCII value gives you the correct integer value of the digit.</p>
<p>All that I can say is: FASCINATING!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ayberkyilmaz.net/2010/char-to-int-conversion-in-c-101/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

