<?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>GeeksPlanet.net &#187; Stack</title>
	<atom:link href="http://geeksplanet.net/tag/stack/feed/" rel="self" type="application/rss+xml" />
	<link>http://geeksplanet.net</link>
	<description>C, C++, JAVA, PERL programming for Dummies</description>
	<lastBuildDate>Wed, 24 Feb 2010 11:38:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>simple Implementation of stack using a linked list</title>
		<link>http://geeksplanet.net/2008/12/data-structures/simple-implementation-of-stack-using-a-linked-list/</link>
		<comments>http://geeksplanet.net/2008/12/data-structures/simple-implementation-of-stack-using-a-linked-list/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 09:13:37 +0000</pubDate>
		<dc:creator>Satish Gandham</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Single linked list]]></category>
		<category><![CDATA[Stack]]></category>

		<guid isPermaLink="false">http://geeksplanet.net/?p=63</guid>
		<description><![CDATA[Stacks are linear data structures. This means that their contexts are stored in what looks like a line (although vertically). This linear property, however, is not sufficient to discriminate a stack from other linear data structures. For example, an array is a sort of linear data structure in which you can access any element directly. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Stacks</strong> are linear data structures. This means that their contexts are stored in what looks like a line (although vertically). This linear property, however, is not sufficient to discriminate a stack from other linear data structures. For example, an array is a sort of linear data structure in which you can access any element directly. In contrast, in a stack, you can only access the element at its top. In a stack <em>Last</em> thing <em>In</em> is the <em>First</em> thing <em>Out</em>.  Thus, we say that a stack enforces <strong>LIFO</strong> order.</p>
<p style="text-align: center;"><a href="http://geeksplanet.net/wp-content/uploads/2008/12/stack.gif"><img class="size-full wp-image-72 aligncenter" title="Stacks | Data Structures" src="http://geeksplanet.net/wp-content/uploads/2008/12/stack.gif" alt="" width="500" height="250" /></a></p>
<p>One disadvantage of <a href="http://geeksplanet.net/2008/09/c-programming/implementaion-of-stack-as-an-array-in-c-language/">implementing stack using an array</a> is wastage of space. Most of the times most of the array is left unused. A better way to implement stack is by using a <a href="http://geeksplanet.net/2008/11/data-structures/implementation-of-singly-linked-list-in-c-plus-plus/">linked list</a>. By using a single linked list to implement a stack there is no wastage of space.</p>
<div id="attachment_62" class="wp-caption aligncenter" style="width: 510px"><a href="http://geeksplanet.net/wp-content/uploads/2008/12/linked-list.gif"><img class="size-full wp-image-62" title="linked-list" src="http://geeksplanet.net/wp-content/uploads/2008/12/linked-list.gif" alt="Implementation of stack using a linked list" width="500" height="150" /></a><p class="wp-caption-text">Implementation of stack using a linked list</p></div>
<pre>
<pre class="brush: cpp; title: ; notranslate">
/*
A simple implementation of linked list in C plus plus (CPP/C++)
*/
/*
Compiler used: g++
*/
#include&lt;iostream&gt;
using namespace std;
struct node{
	int data;
	node *next;
	};
//This function pushes data(node) to the stack
node * push(node*,node*);
//This function pops data (node) and returns pointer to the poped node
node * pop(node*);
//This function shows the stack
void show_stack(node*);
///These two global variables will help us track the start and end of the list
node *f,*l;
main()
{
	int i,ch;
	f=l=NULL;
	cout&lt;&lt;&quot;\t 1 to push&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;\t 2 to pop&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;\t 3 to show stack&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;\t 0 to exit&quot;&lt;&lt;endl;
	while(1)
	{
		cout&lt;&lt;&quot;Enter your choice:&quot;;
		cin&gt;&gt;ch;
		switch(ch)
		{
		case 1:
			{
			if(f==NULL)
			l=f=push(f,l);
			else
			l=push(f,l);
			break;
			}
		case 2:
			{
			cout&lt;&lt;&quot;popped:&quot;&lt;&lt;pop(f)-&gt;data&lt;&lt;endl;
			break;
			}

		case 3:
			{
			show_stack(f);
			break;
			}
		case 0:
			break;
		default:
			cout&lt;&lt;&quot;Please enter a proper choice&quot;&lt;&lt;endl;break;
		}
		if(ch==0)
		break;
	}
}
node * push(node *f,node *l)
{
	node *n;
	n=new node;
	cout&lt;&lt;&quot;Enter data:&quot;;
	cin&gt;&gt;n-&gt;data;
	n-&gt;next=NULL;
	if(f==NULL)
	{f=l=n;return f;}
	else
	{
		l-&gt;next=n;
		l=n;return l;
	}

}
void show_stack(node *f)
{
	cout&lt;&lt;&quot;showing data&quot;&lt;&lt;endl;
	node *guest=f;
	while(guest!=NULL)
	{
		cout&lt;&lt;&quot;\t&quot;&lt;&lt;guest-&gt;data&lt;&lt;endl;
		guest=guest-&gt;next;
	}
}
node * pop(node *f)
{
	node *guest,*lb;
	guest=lb=f;
	while(guest-&gt;next!=NULL)
	{
		lb=guest;
		guest=guest-&gt;next;
	}
	lb-&gt;next=NULL;
	l=lb;
	return guest;
}
		</pre>
</pre>
<blockquote><p><strong>Download the program:</strong> <a href="http://geeksplanet.net/wp-content/uploads/2008/12/linked-list-as-a-stack.cpp">Implementation of stack using a single linked list</a></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://geeksplanet.net/2008/12/data-structures/simple-implementation-of-stack-using-a-linked-list/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

