<?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>C-C++ Programming Encyclopedia</title>
	<atom:link href="http://www.c-cplusplus.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.c-cplusplus.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Sep 2010 06:19:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Program to find files with duplicate names using binary search tree</title>
		<link>http://www.c-cplusplus.com/program-to-find-files-with-duplicate-names-using-binary-search-tree</link>
		<comments>http://www.c-cplusplus.com/program-to-find-files-with-duplicate-names-using-binary-search-tree#comments</comments>
		<pubDate>Thu, 02 Sep 2010 11:04:47 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Program to find files with duplicate names using binary search tree]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=209</guid>
		<description><![CDATA[When the hard disk is new the files and directories are properly organized. As the hard disk grows old, some laxity sets in and one tends to create files with duplicate names in different directories.  Write a program to locate these duplicate filenames.







C Program to find files with duplicate names using binary search tree

#include [...]]]></description>
			<content:encoded><![CDATA[<p>When the hard disk is new the files and directories are properly organized. As the hard disk grows old, some laxity sets in and one tends to create files with duplicate names in different directories.  Write a program to locate these duplicate filenames.<br />
</p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>
C Program to find files with duplicate names using <a href="http://www.c-cplusplus.com">binary search tree</a><br />
<br />
#include  < stdio.h ><br />
#include  < conio.h ><br />
#include  < dos.h ><br />
#include  < dir.h ><br />
#include  < string.h ><br />
#include  < alloc.h ><br />
<br />
struct btreenode<br />
{<br />
	struct btreenode *leftchild ;</p>
<p>	char data[13] ; /* file name */</p>
<p>	char *loc ; /* location of filename */</p>
<p>	struct btreenode *rightchild ;</p>
<p>} *bt = NULL ;</p>
<p>void disktree( ) ;</p>
<p>int insert ( struct btreenode  **, char*, char* ) ;</p>
<p>void main( )<br />
<span id="more-209"></span><br />
{<br />
	char current_dir[32] ;</p>
<p>	clrscr( ) ;</p>
<p>	getcwd ( current_dir, 32 ) ;</p>
<p>	chdir ( &#8220;\\&#8221; ) ;</p>
<p>	disktree( ) ;</p>
<p>	chdir ( current_dir ) ;</p>
<p>    getch( ) ;<br />
}</p>
<p>void disktree( )<br />
{<br />
	struct ffblk file ;</p>
<p>	int flag ;</p>
<p>	char loc[80] ;</p>
<p>	getcwd ( loc, 80 ) ;<br />
	flag =  findfirst ( &#8220;*.*&#8221;, &#038;file, FA_NORMAL | FA_RDONLY | FA_HIDDEN | </p>
<p>					FA_SYSTEM | FA_LABEL | FA_DIREC | FA_ARCH ) ;</p>
<p>	while ( flag == 0 )</p>
<p>	{</p>
<p>		if ( file.ff_name[0] != &#8216;.&#8217; )</p>
<p>		{</p>
<p>			if ( file.ff_attrib == FA_DIREC &#038;&#038; file.ff_fsize == 0 )<br />
			{<br />
				chdir ( file.ff_name ) ;</p>
<p>				disktree( ) ;</p>
<p>				chdir ( loc ) ;<br />
			}<br />
			else<br />
				insert ( &#038;bt, loc, file.ff_name ) ;<br />
		}<br />
		flag = findnext ( &#038;file ) ;<br />
	}<br />
}</p>
<p><strong>/* inserts a new node in a binary search tree */</strong><br />
<br />
int insert ( struct btreenode  **sr, char* l, char* f )<br />
{<br />
	char *p ;</p>
<p>	int flag ;</p>
<p>	if ( *sr == NULL )<br />
	{<br />
		*sr = ( struct btreenode * ) malloc ( sizeof ( struct btreenode ) ) ;</p>
<p>		if ( *sr == NULL )<br />
		{<br />
			printf ( &#8220;\nOut of memory.&#8221; ) ;</p>
<p>			exit ( 1 ) ;<br />
		}</p>
<p>		( *sr ) &#8211; >  leftchild = NULL ;</p>
<p>		( *sr ) &#8211; >  rightchild = NULL ;</p>
<p>		strcpy ( ( *sr ) &#8211; >  data, f ) ;</p>
<p>		p = ( char * ) malloc ( ( strlen ( l ) + 1 ) ) ;</p>
<p>		if ( p == NULL )<br />
		{<br />
			printf ( &#8220;\nOut of memory.&#8221; ) ;</p>
<p>			exit ( 1 ) ;<br />
		}</p>
<p>		strcpy ( p, l ) ;</p>
<p>		( *sr ) &#8211; >  loc = p ;<br />
	}<br />
	else<br />
	{<br />
		flag = strcmp ( ( *sr ) &#8211; >  data, f ) ;</p>
<p>		if ( flag == 0 )<br />
		{<br />
			printf ( &#8220;org: %s&#8221;, ( *sr ) &#8211; >  loc ) ;</p>
<p>			if ( strlen ( ( *sr ) &#8211; >  loc )  >  4 )</p>
<p>				printf ( &#8220;\\&#8221; ) ;</p>
<p>			printf ( &#8220;%s\n&#8221;, ( *sr ) &#8211; >  data ) ;</p>
<p>			printf (&#8220;dup: %s&#8221;, l ) ;</p>
<p>			if ( strlen ( l )  >  4 )</p>
<p>				printf ( &#8220;\\&#8221; ) ;</p>
<p>			printf ( &#8220;%s\n\n&#8221;, f ) ;<br />
		}</p>
<p>		else if ( flag  <  0 )</p>
<p>			insert ( &#038;( ( *sr ) - >  leftchild ), l, f ) ;</p>
<p>		else</p>
<p>			insert ( &#038;( ( *sr ) &#8211; >  rightchild ), l, f ) ;</p>
<p>	}</p>
<p>	return ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/program-to-find-files-with-duplicate-names-using-binary-search-tree/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Input-Restricted Deque Program Using Arrays</title>
		<link>http://www.c-cplusplus.com/input-restricted-deque-program-using-array</link>
		<comments>http://www.c-cplusplus.com/input-restricted-deque-program-using-array#comments</comments>
		<pubDate>Wed, 01 Sep 2010 10:45:04 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Input-Restricted Deque Program Using Array]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=205</guid>
		<description><![CDATA[In an input restricted deque the insertion of elements is at one end only, but the deletion of elements can be done at both the ends of a queue. Write a program that demonstrates an input-restricted deque.

Here&#8217;s an Input-Restricted Deque Program Using Array

#include  < stdio.h > 
#include  < conio.h > 
#include  < [...]]]></description>
			<content:encoded><![CDATA[<p>In an input restricted deque the insertion of elements is at one end only, but the deletion of elements can be done at both the ends of a queue. Write a program that demonstrates an input-restricted deque.<br />
<br />
Here&#8217;s an <a href="http://www.c-cplusplus.com">Input-Restricted Deque Program Using Array</a><br />
<span id="more-205"></span><br />
#include  < stdio.h > </p>
<p>#include  < conio.h > </p>
<p>#include  < alloc.h ><br />
<br />
#define MAX 10<br />
<br />
struct dqueue</p>
<p>{<br />
	int arr[MAX] ;</p>
<p>	int front, rear ;<br />
} ;</p>
<p>void initdqueue ( struct dqueue * ) ;</p>
<p>void addqatend ( struct dqueue *, int item ) ;</p>
<p>int delqatbeg ( struct dqueue * ) ;</p>
<p>int delqatend ( struct dqueue * ) ;</p>
<p>void display ( struct dqueue ) ;</p>
<p>int count ( struct dqueue ) ;</p>
<p>void main( )</p>
<p>{<br />
	struct dqueue dq ;</p>
<p>    int i, n ;</p>
<p>    clrscr( ) ;</p>
<p>    initdqueue ( &#038;dq ) ;</p>
<p>	addqatend ( &#038;dq, 11 ) ;</p>
<p>	addqatend ( &#038;dq, 12 ) ;</p>
<p>	addqatend ( &#038;dq, 13 ) ;</p>
<p>	addqatend ( &#038;dq, 14 ) ;</p>
<p>	addqatend ( &#038;dq, 15 ) ;</p>
<p>	addqatend ( &#038;dq, 16 ) ;</p>
<p>	addqatend ( &#038;dq, 17 ) ;</p>
<p>	addqatend ( &#038;dq, 18 ) ;</p>
<p>	addqatend ( &#038;dq, 19 ) ;</p>
<p>	addqatend ( &#038;dq, 20 ) ;</p>
<p>    display ( dq ) ;</p>
<p>	n = count ( dq ) ;</p>
<p>	printf ( &#8220;\nTotal elements: %d&#8221;, n ) ;</p>
<p>	i = delqatbeg ( &#038;dq ) ;</p>
<p>	printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	i = delqatbeg ( &#038;dq ) ;</p>
<p>	printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	i = delqatend ( &#038;dq ) ;</p>
<p>	printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	i =	 delqatend ( &#038;dq ) ;</p>
<p>	printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	n = count ( dq ) ;</p>
<p>	printf ( &#8220;\nElements Left: %d&#8221;, n ) ;</p>
<p>	display ( dq ) ;</p>
<p>	addqatend ( &#038;dq, 19 ) ;</p>
<p>	addqatend ( &#038;dq, 20 ) ;</p>
<p>	addqatend ( &#038;dq, 21 ) ;</p>
<p>	addqatend ( &#038;dq, 22 ) ;</p>
<p>	addqatend ( &#038;dq, 23 ) ;</p>
<p>	addqatend ( &#038;dq, 24 ) ;</p>
<p>	display ( dq ) ;</p>
<p>    getch( ) ;<br />
}<br />
<strong><br />
/* initializes elements of structure */</strong></p>
<p>void initdqueue ( struct dqueue *p )</p>
<p>{<br />
    int i ;</p>
<p>	p &#8211; >  front = p &#8211; >  rear = -1 ;</p>
<p>	for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>		p - >  arr[i] = 0 ;<br />
}<br />
<strong><br />
/* adds item at the end of dqueue */</strong></p>
<p>void addqatend ( struct dqueue *p, int item )<br />
{<br />
    int i, k ;</p>
<p>	if ( p &#8211; >  front == 0 &#038;&#038; p &#8211; >  rear == MAX )<br />
	{<br />
	   printf ( &#8220;\nQueue is full.\n&#8221; ) ;<br />
	   return ;<br />
	}</p>
<p>	if ( p &#8211; >  rear == -1 &#038;&#038; p &#8211; >  front == -1 )<br />
	{<br />
		p &#8211; >  rear = p &#8211; >  front = 0 ;</p>
<p>		p &#8211; >  arr[p - >  rear] = item ;</p>
<p>		( p &#8211; >  rear )++ ;</p>
<p>		return ;<br />
	}</p>
<p>	if ( p &#8211; >  rear == MAX )<br />
	{<br />
		for ( i = k = p &#8211; >  front &#8211; 1 ; i  <  p - >  rear ; i++ )<br />
		{<br />
			k = i ;<br />
			if ( k == MAX &#8211; 1 )<br />
				p &#8211; >  arr[k] = 0 ;<br />
			else<br />
				p &#8211; >  arr[k] = p &#8211; >  arr[i + 1] ;<br />
		}<br />
		( p &#8211; >  rear )&#8211; ;<br />
		( p &#8211; >  front )&#8211; ;<br />
	}<br />
	p &#8211; >  arr[p - >  rear] = item ;<br />
	( p &#8211; >  rear )++ ;<br />
}</p>
<p><strong>/* deletes item from beginning of dequeue */</strong></p>
<p>int delqatbeg ( struct dqueue *p )<br />
{<br />
    int item ;</p>
<p>	if ( p &#8211; >  front == -1 &#038;&#038; p &#8211; >  rear == -1 )<br />
	{<br />
		printf ( &#8220;\nQueue is empty.\n&#8221; ) ;<br />
		return 0 ;<br />
	}</p>
<p>	item = p &#8211; >  arr[p - >  front] ;<br />
	p &#8211; >  arr[p - >  front] = 0 ;<br />
	( p &#8211; >  front )++ ;</p>
<p>	if ( p &#8211; >  front == MAX )<br />
		p &#8211; >  front = -1 ;</p>
<p>	return item ;<br />
}</p>
<p><strong>/* deletes item from end of dqueue */</strong></p>
<p>int delqatend ( struct dqueue *p )</p>
<p>{<br />
    int item ;</p>
<p>	if ( p &#8211; >  front == -1 &#038;&#038; p &#8211; >  rear == -1 )</p>
<p>	{<br />
		printf ( &#8220;\nQueue is empty.\n&#8221; ) ;</p>
<p>		return 0 ;<br />
	}</p>
<p>	( p &#8211; >  rear )&#8211; ;</p>
<p>	item = p &#8211; >  arr[p - >  rear] ;</p>
<p>	p &#8211; >  arr[p - >  rear] = 0 ;</p>
<p>	if ( p &#8211; >  rear == 0 )</p>
<p>		p &#8211; >  rear = -1 ;</p>
<p>	return item ;<br />
}</p>
<p><strong>/* displays the queue */</strong></p>
<p>void display ( struct dqueue dq )</p>
<p>{<br />
    int i ;</p>
<p>    printf ( &#8220;\n front &#8211; >  &#8221; ) ;</p>
<p>	for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>		printf ( "%d\t", dq.arr[i] ) ;</p>
<p>	printf ( "  < - rear" ) ;<br />
}</p>
<p><strong>/* counts the number of items in dqueue */</strong></p>
<p>int count ( struct dqueue dq )<br />
{<br />
	int c, i ;</p>
<p>	for ( i = c = 0 ; i  <  MAX ; i++ )</p>
<p>	{<br />
		if ( dq.arr[i] != 0 )</p>
<p>			c++ ;</p>
<p>	}</p>
<p>	return c ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/input-restricted-deque-program-using-array/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Program On Deque That Implements A Linked List</title>
		<link>http://www.c-cplusplus.com/program-on-deque-that-implements-a-linked-list</link>
		<comments>http://www.c-cplusplus.com/program-on-deque-that-implements-a-linked-list#comments</comments>
		<pubDate>Tue, 31 Aug 2010 10:37:39 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Linked Lists]]></category>
		<category><![CDATA[Program on deque that implements a linked list]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=200</guid>
		<description><![CDATA[A deque is a data structure that allows both deletion as well as insertion of elements to be done at both ends. Here&#8217;s a handy C program to implement deque using linked list.








Here&#8217;s an Program on deque that implements a linked list.

#include  < stdio.h >
#include  < conio.h >
#include  < alloc.h >

struct node
{
	int [...]]]></description>
			<content:encoded><![CDATA[<p>A deque is a data structure that allows both deletion as well as insertion of elements to be done at both ends. Here&#8217;s a handy C program to <a href="http://www.c-cplusplus.com">implement deque using linked list</a>.<br />
</p>
<p><span id="more-200"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>
Here&#8217;s an Program on <a href="http://www.c-cplusplus.com">deque that implements a linked list</a>.<br />
<br />
#include  < stdio.h ><br />
#include  < conio.h ><br />
#include  < alloc.h ><br />
<br />
struct node</p>
<p>{<br />
	int data ;</p>
<p>	struct node *link ;</p>
<p>} ;</p>
<p>struct dqueue</p>
<p>{<br />
	struct node *front ;</p>
<p>	struct node *rear ;<br />
} ;</p>
<p>void initdqueue ( struct dqueue * ) ;</p>
<p>void addqatend ( struct dqueue *, int item ) ;</p>
<p>void addqatbeg ( struct dqueue *, int item ) ;</p>
<p>int delqatbeg ( struct dqueue * ) ;</p>
<p>int delqatend ( struct dqueue * ) ;</p>
<p>void display ( struct dqueue ) ;</p>
<p>int count ( struct dqueue ) ;</p>
<p>void deldqueue ( struct dqueue * ) ;</p>
<p>void main( )<br />
{<br />
	struct dqueue dq ;</p>
<p>    int i, n ;</p>
<p>    clrscr( ) ;</p>
<p>    initdqueue ( &#038;dq ) ;</p>
<p>	addqatend ( &#038;dq, 11 ) ;</p>
<p>	addqatbeg ( &#038;dq, 10 ) ;</p>
<p>	addqatend ( &#038;dq, 12 ) ;</p>
<p>	addqatbeg ( &#038;dq, 9 ) ;</p>
<p>	addqatend ( &#038;dq, 13 ) ;</p>
<p>	addqatbeg ( &#038;dq, 8 ) ;</p>
<p>	addqatend ( &#038;dq, 14 ) ;</p>
<p>	addqatbeg ( &#038;dq, 7 ) ;</p>
<p>	display ( dq ) ;</p>
<p>	n = count ( dq ) ;</p>
<p>	printf ( &#8220;\nTotal elements: %d&#8221;, n ) ;</p>
<p>	i = delqatbeg ( &#038;dq ) ;</p>
<p>	printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	i = delqatbeg ( &#038;dq ) ;</p>
<p>    printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	i = delqatbeg ( &#038;dq ) ;</p>
<p>    printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	i =	 delqatend ( &#038;dq ) ;</p>
<p>    printf ( &#8220;\nItem extracted = %d&#8221;, i ) ;</p>
<p>	display ( dq ) ;</p>
<p>	n = count ( dq ) ;</p>
<p>	printf ( &#8220;\nElements Left: %d&#8221;, n ) ;</p>
<p>    deldqueue ( &#038;dq ) ;</p>
<p>    getch( ) ;<br />
}<br />
<br />
<strong>/* initializes elements of structure */</strong><br />
<br />
void initdqueue ( struct dqueue *p )<br />
{<br />
	p &#8211; >   front = p &#8211; >  rear = NULL ;<br />
}</p>
<p><strong>/* adds item at the end of dqueue */</strong><br />
<br />
void addqatend ( struct dqueue *p, int item )<br />
{<br />
	struct node *temp ;</p>
<p>	temp = ( struct node * ) malloc ( sizeof ( struct node )  );</p>
<p>	temp &#8211; >  data = item ;</p>
<p>	temp &#8211; >  link = NULL ;</p>
<p>	if ( p &#8211; >  front == NULL )</p>
<p>		p &#8211; >  front = temp ;</p>
<p>	else</p>
<p>		p &#8211; >  rear &#8211; >  link = temp ;</p>
<p>	p &#8211; >  rear = temp ;<br />
}</p>
<p><strong>/* adds item at begining of dqueue */</strong><br />
<br />
void addqatbeg ( struct dqueue *p, int item )<br />
{<br />
	struct node *temp ;</p>
<p>	int *q ;</p>
<p>	temp = ( struct node * ) malloc ( sizeof ( struct node )  );</p>
<p>	temp &#8211; >  data = item ;</p>
<p>	temp &#8211; >  link = NULL ;</p>
<p>	if ( p &#8211; >  front == NULL )</p>
<p>		p &#8211; >  front = p &#8211; >  rear = temp ;</p>
<p>	else</p>
<p>	{</p>
<p>		temp &#8211; >  link = p &#8211; >  front ;</p>
<p>		p &#8211; >  front = temp ;<br />
	}<br />
}<br />
<strong>/* deletes item from begining of dqueue */</strong><br />
<br />
int delqatbeg ( struct dqueue *p )<br />
{<br />
	struct node *temp = p &#8211; >  front ;</p>
<p>	int item ;</p>
<p>	if ( temp == NULL )</p>
<p>	{</p>
<p>		printf ( &#8220;\nQueue is empty.&#8221; ) ;</p>
<p>		return 0 ;<br />
	}</p>
<p>	else</p>
<p>	{<br />
		temp = p &#8211; >  front ;</p>
<p>		item = temp &#8211; >  data ;</p>
<p>		p &#8211; >  front = temp &#8211; >  link ;</p>
<p>		free ( temp ) ;</p>
<p>		if ( temp == NULL )</p>
<p>			p &#8211; >  rear = NULL ;</p>
<p>		return ( item ) ;<br />
	}<br />
}</p>
<p><strong>/* deletes item from end of dqueue */</strong><br />
<br />
int delqatend ( struct dqueue *p )<br />
{<br />
	struct node *temp , *rleft, *q ;</p>
<p>	int item ;</p>
<p>	temp = p &#8211; >  front ;</p>
<p>	if ( p &#8211; >  rear == NULL )</p>
<p>	{<br />
		printf ( &#8220;\nQueue is empty.&#8221; ) ;</p>
<p>		return 0 ;<br />
	}</p>
<p>	else</p>
<p>	{<br />
		while ( temp != p &#8211; >  rear )</p>
<p>		{<br />
			rleft = temp ;</p>
<p>			temp = temp &#8211; >  link ;<br />
		}<br />
		q = p &#8211; >  rear ;</p>
<p>		item = q &#8211; >  data ;</p>
<p>		free ( q ) ;</p>
<p>		p &#8211; >  rear = rleft ;</p>
<p>		p &#8211; >  rear &#8211; >  link = NULL ;</p>
<p>		if ( p &#8211; >  rear == NULL )</p>
<p>			p &#8211; >  front = NULL ;</p>
<p>		return ( item ) ;<br />
	}<br />
}<br />
<strong>/* displays the queue */</strong><br />
<br />
void display ( struct dqueue dq )<br />
{<br />
	struct node *temp = dq.front ;</p>
<p>	printf ( &#8220;\nfront &#8211; >  &#8221; ) ;</p>
<p>	while ( temp != NULL )<br />
	{<br />
		if ( temp &#8211; >  link == NULL )<br />
		{<br />
			printf ( &#8220;\t%d&#8221;, temp &#8211; >  data ) ;</p>
<p>			printf ( &#8221;  < - rear" ) ;<br />
		}<br />
		else<br />
			printf ( "\t%d", temp - >  data ) ;</p>
<p>		temp = temp &#8211; >  link ;<br />
	}<br />
	printf ( &#8220;\n&#8221; ) ;<br />
}</p>
<p><strong>/* counts the number of items in dqueue */</strong><br />
<br />
int count ( struct dqueue dq )<br />
{<br />
	int c = 0 ;<br />
	struct node *temp = dq.front ;</p>
<p>	while ( temp != NULL )<br />
	{<br />
		temp = temp &#8211; >  link ;<br />
		c++ ;<br />
	}</p>
<p>	return c ;<br />
}</p>
<p><strong>/* deletes the queue */</strong><br />
<br />
void deldqueue ( struct dqueue *p )<br />
{<br />
    struct node *temp ;</p>
<p>	if ( p &#8211; >  front == NULL )<br />
		return ;</p>
<p>	while ( p &#8211; >  front != NULL )<br />
	{<br />
		temp = p &#8211; >  front ;<br />
		p &#8211; >  front = p &#8211; >  front &#8211; >  link ;<br />
		free ( temp ) ;<br />
	}<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/program-on-deque-that-implements-a-linked-list/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Car Garage Simulation Using De-Queue</title>
		<link>http://www.c-cplusplus.com/car-garage-simulation-using-de-queue</link>
		<comments>http://www.c-cplusplus.com/car-garage-simulation-using-de-queue#comments</comments>
		<pubDate>Mon, 30 Aug 2010 10:26:18 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Linked Lists]]></category>
		<category><![CDATA[Car Garage Simulation Using De-Queue]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=194</guid>
		<description><![CDATA[Here&#8217;s an amazing C program that implements a car parking system.

Problem Statement: Suppose Fundu Parking Garage contains 10 parking lanes, each with a capacity to hold 10 cars at a time. As each car arrives/departs, the values A/D (representing arrival /departure) is entered along with the car registration number. If a car is departing the [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an amazing C program that implements a car parking system.<br />
<br />
<strong>Problem Statement:</strong> Suppose Fundu Parking Garage contains 10 parking lanes, each with a capacity to hold 10 cars at a time. As each car arrives/departs, the values A/D (representing arrival /departure) is entered along with the car registration number. If a car is departing the data should get updated. If a new car is arriving then on the screen a message should be displayed indicating suitable parking slot for the car. Cars arrive at the south end of the garage and leave from the north end. If a customer arrives to pick up a car that is not the northern most, all cars to the north of the car are moved out, the car is driven out, and the other cars are restored in the same order that they were in originally. Whenever a car leaves, all cars to the south are moved forward so that at all times all the empty spaces are in the south part of the garage. </p>
<p><span id="more-194"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>
Here&#8217;s an example of <a href="http://www.c-cplusplus.com">Car Ggarage Simulation using De-Queue</a>
<ol>
 (link list implementation)<br />
<br />
#include  < stdio.h > </p>
<p>#include  < conio.h > </p>
<p>#include  < alloc.h > </p>
<p>#include  < string.h ><br />
<br />
#define TOP 1</p>
<p>#define BOT 2<br />
<br />
struct node</p>
<p>{</p>
<p>	char plate [15] ;</p>
<p>	struct node *link ;</p>
<p>} *front[5], *rear[5] ;</p>
<p>char plate[15], temp[15] ;</p>
<p>int i ;</p>
<p>void add_dq ( struct node**, struct node**, int, char* ) ;</p>
<p>char* del_dq ( struct node**, struct node**, int ) ;</p>
<p>void push ( struct node**, char* ) ;</p>
<p>char* pop ( struct node** ) ;</p>
<p>void q_display ( struct node * ) ;</p>
<p>void main( )<br />
{<br />
	char ad ;</p>
<p>	int s, lane = -1, min, lc ;</p>
<p>	clrscr( );</p>
<p>	while ( 1 )<br />
	{<br />
		for ( i = 0 ; i  <  5 ; i++ )<br />
		{<br />
			printf( "lane %d: ", i ) ;</p>
<p>			q_display ( front[i] ) ;<br />
		}</p>
<p>		printf( "\nArrival/Departure/Quit? ( A/D/Q ): " ) ;</p>
<p>		ad = getch( ) ;</p>
<p>		if ( toupper ( ad ) == 'Q' )</p>
<p>			exit ( 1 ) ;</p>
<p>		printf ( "\nEnter license plate num:" ) ;</p>
<p>		gets ( plate ) ;</p>
<p>		ad = toupper ( ad ) ;</p>
<p>		if ( ad == 'A' )  /* arrival of car */<br />
		{<br />
			lane = -1 ;  /* assume no lane is available */</p>
<p>			min = 10 ;</p>
<p>			for ( i = 0 ; i  <  5 ; i++ )</p>
<p>			{</p>
<p>				s = count ( front[i] ) ;</p>
<p>				if ( s  <  min )</p>
<p>				{<br />
					min = s ;</p>
<p>					lane = i ;<br />
				}<br />
			}</p>
<p>			if ( lane == -1 )</p>
<p>				printf ( "\nNo room available" ) ;</p>
<p>			else<br />
			{<br />
				add_dq ( &#038;front[ lane ], &#038;rear[ lane ], BOT, plate ) ;</p>
<p>				printf ( "\npark car at lane %d slot %d\n", lane, s ) ;<br />
			}<br />
		}<br />
		else<br />
		{<br />
			if ( ad == 'D' )  /* departure of car */<br />
			{<br />
				for ( i = 0 ; i  <  5 ; ++i )<br />
				{<br />
					s = search ( front[i], plate ) ;</p>
<p>					if ( s != -1 )<br />
					{<br />
						lane = i ;</p>
<p>						break ;<br />
					}<br />
				}<br />
            	if ( i == 5 )<br />
					printf ( "\nno such car!!\n" ) ;<br />
				else<br />
				{<br />
					printf ( "\ncar found at lane %d slot %d\n", lane, s ) ;</p>
<p>					del_dq ( &#038;front[ lane ], &#038;rear[ lane ], s ) ;<br />
				}<br />
			}<br />
			else<br />
				if ( ad == 'Q' )</p>
<p>					exit ( 1 ) ;<br />
        }<br />
    }<br />
}</p>
<p><strong>/* adds a new element at the end of queue */</strong></p>
<p>void add_dq ( struct node **f, struct node **r, int tb, char *p )</p>
<p>{<br />
	struct node *q ;</p>
<p>	/* create new node */</p>
<p>	q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;</p>
<p>	strcpy ( q &#8211; >  plate, p ) ;</p>
<p>	q &#8211; >  link = NULL ;</p>
<p>	/* if the queue is empty */</p>
<p>	if ( *f == NULL )</p>
<p>		*f = q ;</p>
<p>	else</p>
<p>	{</p>
<p>		if ( tb == BOT )</p>
<p>			( *r ) &#8211; >  link = q ;</p>
<p>		else</p>
<p>		{</p>
<p>			q &#8211; >  link = *f ;</p>
<p>			*f = q ;</p>
<p>			return ;</p>
<p>		}<br />
	}</p>
<p>	*r = q ;<br />
}</p>
<p>char* del_dq ( struct node **f, struct node **r, int n )<br />
{<br />
	struct node *q, *top = NULL ;</p>
<p>	/* if queue is empty */</p>
<p>	if ( *f == NULL )</p>
<p>		printf ( &#8220;queue is empty&#8221; ) ;</p>
<p>	else<br />
	{<br />
		if ( n == 0 )<br />
		{<br />
			strcpy ( temp, ( *f ) &#8211; >  plate ) ;</p>
<p>			q = *f ;</p>
<p>			*f = ( *f ) &#8211; >  link ;</p>
<p>			free ( q ) ;</p>
<p>			return temp ;<br />
		}</p>
<p>		/* locate node */</p>
<p>		for ( i = 0 ; i  <  n ; i++ )<br />
		{<br />
			/* drive out cars */</p>
<p>			push ( &#038;top, ( *f ) - >  plate ) ;</p>
<p>			/* delete the node */</p>
<p>			q = *f ;</p>
<p>			*f = q &#8211; >  link ;</p>
<p>			free ( q ) ;<br />
		}</p>
<p>		/* delete the nth node */</p>
<p>		q = *f ;</p>
<p>		*f = q &#8211; >  link ;</p>
<p>		free ( q ) ;</p>
<p>		for ( i = 0 ; i  <  n ; i++ )<br />
		{<br />
			strcpy ( temp, pop ( &#038;top ) ) ;</p>
<p>			/* <strong>add the node</strong> */</p>
<p>			add_dq ( f, r, TOP, temp ) ;<br />
		}<br />
	}<br />
}</p>
<p>int count ( struct node *q )</p>
<p>{<br />
	int c = 0 ;</p>
<p>	/* <strong>traverse the entire <a href="http://www.c-cplusplus.com/category/linked-lists">linked list</a></strong> */</p>
<p>	while ( q!= NULL )</p>
<p>	{<br />
		q = q &#8211; >  link ;</p>
<p>		c++ ;<br />
	}</p>
<p>	return c ;<br />
}</p>
<p>int search ( struct node *q, char *p )</p>
<p>{<br />
	int s = -1, c = 0 ;</p>
<p>	while ( q != NULL )<br />
	{<br />
		if ( strcmp ( p, q &#8211; >  plate ) == 0 )<br />
		{<br />
			s = c ;</p>
<p>			break ;<br />
		}<br />
		else<br />
		{<br />
			q = q &#8211; >  link ;</p>
<p>			c++ ;<br />
		}<br />
	}<br />
	return ( s ) ;<br />
}</p>
<p><strong>/* <strong>adds a new element to the top of stack</strong> */</strong></p>
<p>void push ( struct node **s, char* item )</p>
<p>{<br />
	struct node *q ;</p>
<p>	q = ( struct node* ) malloc ( sizeof ( struct node ) ) ;</p>
<p>	strcpy ( q &#8211; >  plate, item ) ;</p>
<p>	q &#8211; >  link = *s ;</p>
<p>	*s = q ;<br />
}</p>
<p><strong>/* <strong>removes an element from top of stack</strong> */</strong></p>
<p>char* pop ( struct node **s )</p>
<p>{<br />
	struct node *q ;</p>
<p>	/* if stack is empty */</p>
<p>	if ( *s == NULL )</p>
<p>	{</p>
<p>		return NULL ;</p>
<p>	}</p>
<p>	else</p>
<p>	{<br />
		q = *s ;</p>
<p>		strcpy ( temp, q &#8211; >  plate ) ;</p>
<p>		*s = q &#8211; >  link ;</p>
<p>		free ( q ) ;</p>
<p>		return ( temp ) ;<br />
	}<br />
}</p>
<p>void q_display ( struct node *q )<br />
{<br />
	while( q != NULL )</p>
<p>	{</p>
<p>		printf ( &#8220;%s&#8221;, q &#8211; >  plate ) ;</p>
<p>		q = q &#8211; >  link ;<br />
	}</p>
<p>	printf ( &#8220;\n&#8221; ) ;</p>
<p>}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/car-garage-simulation-using-de-queue/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simulation of an Airport</title>
		<link>http://www.c-cplusplus.com/simulation-of-an-airport</link>
		<comments>http://www.c-cplusplus.com/simulation-of-an-airport#comments</comments>
		<pubDate>Sat, 14 Aug 2010 11:10:17 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Tricky Programs]]></category>
		<category><![CDATA[Simulation of an Airport]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=192</guid>
		<description><![CDATA[There is a small busy airport with only one runway. In each unit of time one plane can land or one plane can take off, but not both. Planes arrive ready to land or to take off at random times, so at any given unit of time, the runway may be idle or a plane [...]]]></description>
			<content:encoded><![CDATA[<p>There is a small busy airport with only one runway. In each unit of time one plane can land or one plane can take off, but not both. Planes arrive ready to land or to take off at random times, so at any given unit of time, the runway may be idle or a plane may be landing or taking off. There may be several planes waiting either to land or to take off. Follow the steps given below to design the C program for <a href="http://www.c-cplusplus.com">simulation of an Airport</a>.<br />
<span id="more-192"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p> •	Create two queues one for the planes landing and the other for planes taking off.<br />
•	Get the maximum number of units  < endtime >  for which the simulation program would run.<br />
•	Get the expected number of planes arriving in one unit  < expectarrive >  and number of planes ready to take off in one unit  < expectdepart > .<br />
•	To display the statistical data concerning the simulation, declare following data members.<br />
•	idletime &#8211; to store the number of units the runway was idle<br />
•	landwait &#8211; to store total waiting time required for planes landed<br />
•	nland &#8211; to store number of planes landed<br />
•	nplanes &#8211; to store number of planes processed<br />
•	nrefuse &#8211; to store number of planes refused to land on airport<br />
•	ntakeoff &#8211; to store number of planes taken off<br />
•	takeoffwait &#8211; to store total waiting time taken for take off<br />
•	Initialize the queue used for the plane landing and for the take off<br />
•	Get the data for  < endtime > ,  < expectarrive >  and  < expectdepart >  from the user.</p>
<p>•	The process of simulation would run for many units of time, hence run a loop in main( ) that would run from  < curtime >  to  < endtime >  where  < curtime >  would be 1 and  < endtime >  would be the maximum number of units the program has to be run.<br />
•	Generate a random number. Depending on the value of random number generated, perform following tasks.</p>
<p>•	If the random number is less than or equal to 1 then get data for the plane ready to land. Check whether or not the queue for landing of planes is full. If the queue is full then refuse the plane to land. If the queue is not empty then add the data to the queue maintained for planes landing.<br />
•	If the random number generated is zero, then generate a random number again. Check if this number is less than or equal to 1. If it is , then get data for the plane ready to take off. Check whether or not the queue for taking a plane off is full. If the queue is full then refuse the plane to take off otherwise add the data to the queue maintained for planes taking off.<br />
•	It is better to keep a plane waiting on the ground than in the air, hence allow a plane to take off only, if there are no planes waiting to land.<br />
•	After receiving a request from new plane to land or take off, check the queue of planes waiting to land, and only if the landing queue is empty, allow a plane to take off.<br />
•	If the queue for planes landing is not empty then remove the data of plane in the queue else run the procedure to land the plane.<br />
•	Similarly, if the queue for planes taking off is not empty then remove the data of plane in the queue else run the procedure to take off the plane.<br />
•	If both the queues are empty then the runway would be idle.<br />
•	Finally, display the statistical data As given below.</p>
<p>Total number of planes processed<br />
   Number of planes landed :<br />
   Number of planes taken off :<br />
   Number of planes refused use :<br />
   Number of planes left ready to land :<br />
   Number of planes left ready to take off :<br />
Percentage of time the runway was idle :<br />
Average wait time to land :<br />
Average wait time to take off :</p>
<p><strong>/* Airport simulation */</strong></p>
<p>#include  < stdio.h ><br />
#include  < conio.h ><br />
#include  < stdlib.h ><br />
#include  < ctype.h ><br />
#include  < math.h ><br />
#include  < time.h ><br />
#include  < limits.h > </p>
<p>#define MAX 3<br />
#define ARRIVE 0<br />
#define DEPART 1</p>
<p>struct plane<br />
{<br />
	int id ;<br />
	int tm ;<br />
} ;</p>
<p>struct queue<br />
{<br />
	int count ;<br />
	int front ;<br />
	int rear ;<br />
   	struct plane p[MAX] ;<br />
} ;</p>
<p>void initqueue ( struct queue * ) ;<br />
void addqueue ( struct queue *, struct plane ) ;<br />
struct plane delqueue ( struct queue * ) ;<br />
int size ( struct queue ) ;<br />
int empty ( struct queue ) ;<br />
int full ( struct queue ) ;</p>
<p>void initqueue ( struct queue *pq )<br />
{<br />
	pq &#8211; >  count = 0 ;<br />
	pq &#8211; >  front = 0 ;<br />
	pq &#8211; >  rear = -1 ;<br />
}</p>
<p>void addqueue ( struct queue *pq, struct plane item )<br />
{<br />
	if ( pq &#8211; >  count  > = MAX )<br />
	{<br />
		printf ( &#8220;\nQueue is full.\n&#8221; ) ;<br />
		return ;<br />
	}<br />
	( pq &#8211; >  count )++ ;</p>
<p>	pq &#8211; >  rear = ( pq &#8211; >  rear + 1 ) % MAX ;<br />
	pq &#8211; >  p[pq - >  rear] = item ;<br />
}</p>
<p>struct plane delqueue ( struct queue *pq )<br />
{<br />
	struct plane p1 ;</p>
<p>	if ( pq &#8211; >  count  < = 0 )<br />
	{<br />
		printf ( "\nQueue is empty.\n" ) ;<br />
		p1.id = 0 ;<br />
		p1.tm = 0 ;<br />
	}<br />
	else<br />
	{<br />
		( pq - >  count )&#8211; ;<br />
		p1 = pq &#8211; >  p[pq - >  front] ;<br />
		pq &#8211; >  front = ( pq &#8211; >  front + 1 ) % MAX ;<br />
	}<br />
	return p1 ;<br />
}</p>
<p>int size ( struct queue q )<br />
{<br />
	return q.count ;<br />
}</p>
<p>int empty ( struct queue q )<br />
{<br />
	return ( q.count  < = 0 ) ;<br />
}</p>
<p>int full ( struct queue q )<br />
{<br />
	return ( q.count  > = MAX ) ;<br />
}</p>
<p>struct airport<br />
{<br />
	struct queue landing ;<br />
	struct queue takeoff ;<br />
	struct queue *pl ;<br />
	struct queue *pt ;<br />
	int idletime ;<br />
	int landwait, takeoffwait ;<br />
	int nland, nplanes, nrefuse, ntakeoff ;<br />
	struct plane pln ;<br />
} ;</p>
<p>void initairport ( struct airport * ) ;<br />
void start ( int *, double *, double * ) ;<br />
void newplane ( struct airport *, int, int ) ;<br />
void refuse ( struct airport *, int ) ;<br />
void land ( struct airport *, struct plane, int ) ;<br />
void fly ( struct airport *, struct plane, int ) ;<br />
void idle ( struct airport *, int ) ;<br />
void conclude ( struct airport *, int ) ;<br />
int randomnumber ( double ) ;<br />
void apaddqueue ( struct airport *, char ) ;<br />
struct plane apdelqueue ( struct airport *, char ) ;<br />
int apsize ( struct airport, char ) ;<br />
int apfull ( struct airport, char ) ;<br />
int apempty ( struct airport, char ) ;<br />
void myrandomize ( ) ;</p>
<p>void initairport ( struct airport *ap )<br />
{<br />
    initqueue ( &#038;( ap- >  landing ) ) ;<br />
    initqueue ( &#038;( ap &#8211; >  takeoff ) ) ;</p>
<p>	ap &#8211; >  pl = &#038;( ap &#8211; >  landing ) ;<br />
	ap &#8211; >  pt = &#038;( ap &#8211; >  takeoff ) ;<br />
	ap &#8211; >  nplanes = ap &#8211; >  nland = ap &#8211; >  ntakeoff = ap &#8211; >  nrefuse = 0 ;<br />
	ap &#8211; >  landwait = ap &#8211; >  takeoffwait = ap &#8211; >  idletime = 0 ;<br />
}</p>
<p>void start ( int *endtime, double *expectarrive, double *expectdepart )<br />
{<br />
	int flag = 0 ;<br />
	char wish ;</p>
<p>	printf ( &#8220;\nProgram that simulates an airport with only one runway.\n&#8221; ) ;<br />
	printf ( &#8220;One plane can land or depart in each unit of time.\n&#8221; ) ;<br />
	printf ( &#8220;Up to %d planes can be waiting to land or take off at any time.\n&#8221;, MAX ) ;<br />
	printf ( &#8220;How many units of time will the simulation run?&#8221; ) ;<br />
	scanf ( &#8220;%d&#8221;, endtime ) ;<br />
	myrandomize( ) ;<br />
	do<br />
	{<br />
		printf ( &#8220;\nExpected number of arrivals per unit time? &#8221; ) ;<br />
		scanf ( &#8220;%lf&#8221;, expectarrive ) ;<br />
		printf ( &#8220;\nExpected number of departures per unit time? &#8221; ) ;<br />
		scanf ( &#8220;%lf&#8221;, expectdepart ) ;</p>
<p>		if ( *expectarrive  <  0.0 || *expectdepart  <  0.0 )<br />
		{<br />
			printf ( "These numbers must be nonnegative.\n" ) ;<br />
			flag = 0 ;<br />
		}<br />
		else<br />
		{<br />
			if ( *expectarrive + *expectdepart  >  1.0 )<br />
			{<br />
				printf ( &#8220;The airport will become saturated. Read new numbers? &#8221; ) ;<br />
                fflush ( stdin ) ;<br />
				scanf ( &#8220;%c&#8221;, &#038;wish ) ;<br />
				if ( tolower ( wish ) == &#8216;y&#8217; )<br />
					flag = 0 ;<br />
				else<br />
					flag = 1 ;<br />
			}<br />
			else<br />
				flag = 1 ;<br />
		}<br />
	} while ( flag == 0 ) ;<br />
}</p>
<p>void newplane ( struct airport *ap, int curtime, int action )<br />
{<br />
	( ap &#8211; >  nplanes )++ ;<br />
	ap &#8211; >  pln.id = ap &#8211; >  nplanes ;<br />
	ap &#8211; >  pln.tm = curtime ;</p>
<p>	switch ( action )<br />
	{<br />
		case ARRIVE :<br />
			printf ( &#8220;\n&#8221; ) ;<br />
			printf ( &#8220;Plane %d ready to land.\n&#8221;, ap &#8211; >  nplanes ) ;<br />
			break ;</p>
<p>		case DEPART :<br />
			printf ( &#8220;\nPlane %d ready to take off.\n&#8221;, ap &#8211; >  nplanes ) ;<br />
			break ;<br />
	}<br />
}</p>
<p>void refuse ( struct airport *ap, int action )<br />
{<br />
	switch ( action )<br />
	{<br />
		case ARRIVE :</p>
<p>			 printf ( &#8220;\tplane  %d directed to another airport.\n&#8221;, ap &#8211; >  pln.id ) ;<br />
			 break ;</p>
<p>		case DEPART :</p>
<p>			 printf ( &#8220;\tplane %d told to try later.\n&#8221;, ap &#8211; >  pln.id ) ;<br />
			 break ;<br />
	}<br />
	( ap &#8211; >  nrefuse )++ ;<br />
}</p>
<p>void land ( struct airport *ap, struct plane pl, int curtime )<br />
{<br />
	int wait ;</p>
<p>	wait = curtime &#8211; pl.tm ;<br />
	printf ( &#8220;%d: Plane %d landed &#8220;, curtime, pl.id ) ;<br />
	printf ( &#8220;in queue %d units \n&#8221;, wait ) ;<br />
	( ap &#8211; >  nland ) ++ ;<br />
	( ap &#8211; >  landwait ) += wait ;<br />
}</p>
<p>void fly ( struct airport *ap, struct plane pl, int curtime )<br />
{<br />
	int wait ;</p>
<p>	wait = curtime &#8211; pl.tm ;<br />
	printf ( &#8220;%d: Plane %d took off &#8220;, curtime, pl.id ) ;<br />
	printf ( &#8220;in queue %d units \n&#8221;, wait ) ;<br />
	( ap &#8211; >  ntakeoff )++ ;<br />
	( ap &#8211; >  takeoffwait ) += wait ;<br />
}</p>
<p>void idle ( struct airport *ap, int curtime )<br />
{<br />
	printf ( &#8220;%d: Runway is idle.\n&#8221;, curtime ) ;<br />
	ap &#8211; >  idletime++ ;<br />
}</p>
<p>void conclude ( struct airport *ap, int endtime )<br />
{<br />
	printf ( &#8220;\tSimulation has concluded after %d units.\n&#8221;, endtime ) ;<br />
	printf ( &#8220;\tTotal number of planes processed: %d\n&#8221;, ap &#8211; >  nplanes ) ;<br />
	printf ( &#8220;\tNumber of planes landed: %d\n&#8221;, ap &#8211; >  nland ) ;<br />
	printf ( &#8220;\tNumber of planes taken off: %d\n&#8221;, ap &#8211; >  ntakeoff ) ;<br />
	printf ( &#8220;\tNumber of planes refused use: %d\n&#8221;, ap &#8211; >  nrefuse ) ;<br />
	printf ( &#8220;\tNumber left ready to land: %d\n&#8221;, apsize ( *ap, &#8216;l&#8217; ) ) ;<br />
	printf ( &#8220;\tNumber left ready to take off: %d\n&#8221;, apsize ( *ap, &#8216;t&#8217; ) ) ;</p>
<p>	if ( endtime  >  0 )<br />
		printf ( &#8220;\tPercentage of time runway idle: %lf \n&#8221;, ( ( double ) ap &#8211; >  idletime / endtime ) * 100.0 ) ;</p>
<p>	if ( ap &#8211; >  nland  >  0 )<br />
		printf ( &#8220;\tAverage wait time to land: %lf \n&#8221;, ( ( double ) ap &#8211; >  landwait / ap &#8211; >  nland ) ) ;</p>
<p>	if ( ap &#8211; >  ntakeoff  >  0 )<br />
		printf ( &#8220;\tAverage wait time to take off: %lf \n&#8221;, ( ( double ) ap &#8211; >  takeoffwait / ap &#8211; >  ntakeoff ) ) ;<br />
}</p>
<p>int randomnumber ( double expectedvalue )<br />
{<br />
	int n = 0 ;<br />
	double em ;<br />
	double x ;</p>
<p>	em = exp ( -expectedvalue ) ;<br />
	x = rand( ) / ( double ) INT_MAX ;</p>
<p>	while ( x  >  em )<br />
	{<br />
		n++ ;<br />
		x *= rand( ) / ( double ) INT_MAX ;<br />
	}</p>
<p>	return n ;<br />
}</p>
<p>void apaddqueue ( struct airport *ap, char type )<br />
{<br />
	switch ( tolower( type ) )<br />
	{<br />
		case &#8216;l&#8217; :<br />
			  addqueue ( ap &#8211; >  pl, ap &#8211; >  pln ) ;<br />
			  break ;</p>
<p>		case &#8216;t&#8217; :<br />
			  addqueue ( ap &#8211; >  pt, ap &#8211; >  pln ) ;<br />
			  break ;<br />
	}<br />
}</p>
<p>struct plane apdelqueue ( struct airport *ap, char type )<br />
{<br />
	struct plane p1 ;</p>
<p>	switch ( tolower ( type ) )<br />
	{<br />
		case &#8216;l&#8217; :<br />
			  p1 = delqueue ( ap &#8211; >  pl ) ;<br />
			  break ;</p>
<p>		case &#8216;t&#8217; :<br />
			  p1 = delqueue ( ap &#8211; >  pl ) ;<br />
			  break ;<br />
	}</p>
<p>	return p1 ;<br />
}</p>
<p>int apsize ( struct airport ap, char type )<br />
{<br />
	switch ( tolower ( type ) )<br />
	{<br />
		case &#8216;l&#8217; :<br />
			  return ( size ( *( ap.pl ) ) ) ;</p>
<p>		case &#8216;t&#8217; :<br />
			  return ( size ( *( ap.pt ) ) ) ;<br />
	}</p>
<p>	return 0 ;<br />
}</p>
<p>int apfull ( struct airport ap, char type )<br />
{<br />
	switch ( tolower ( type ) )<br />
	{<br />
		case &#8216;l&#8217; :<br />
			  return ( full ( *( ap.pl ) ) ) ;</p>
<p>		case &#8216;t&#8217; :<br />
			  return ( full ( *( ap.pt ) ) ) ;<br />
	}</p>
<p>	return 0 ;<br />
}</p>
<p>int apempty ( struct airport ap, char type )<br />
{<br />
	switch ( tolower ( type ) )<br />
	{<br />
		case &#8216;l&#8217; :<br />
			  return ( empty ( *( ap.pl ) ) ) ;</p>
<p>		case &#8216;t&#8217; :<br />
			  return ( empty ( *( ap.pt ) ) ) ;<br />
	}</p>
<p>	return 0 ;<br />
}</p>
<p>void myrandomize( )<br />
{<br />
	srand ( ( unsigned int ) ( time ( NULL ) % 10000 ) ) ;<br />
}</p>
<p>void main( )<br />
{<br />
	struct airport a ;<br />
	int i, pri, curtime, endtime ;<br />
	double expectarrive, expectdepart ;<br />
	struct plane temp ;</p>
<p>    clrscr( ) ;</p>
<p>    initairport ( &#038;a );</p>
<p>	start ( &#038;endtime, &#038;expectarrive, &#038;expectdepart ) ;</p>
<p>	for ( curtime = 1 ; curtime  < = endtime ; curtime++ )<br />
	{<br />
		pri = randomnumber ( expectarrive ) ;</p>
<p>		for ( i = 1 ; i  < = pri ; i++ )<br />
		{<br />
			newplane ( &#038;a, curtime, ARRIVE ) ;<br />
			if ( apfull ( a, &#8216;l&#8217; ) )<br />
				 refuse ( &#038;a, ARRIVE ) ;<br />
			else<br />
				apaddqueue( &#038;a, &#8216;l&#8217; ) ;<br />
		}</p>
<p>		pri = randomnumber ( expectdepart ) ;<br />
		for ( i = 1 ; i  < = pri ; i++ )<br />
		{<br />
			newplane ( &#038;a, curtime, DEPART ) ;<br />
			if ( apfull ( a, &#8216;t&#8217; ) )<br />
			   refuse ( &#038;a, DEPART ) ;<br />
			else<br />
			   apaddqueue ( &#038;a, &#8216;t&#8217; ) ;<br />
		}</p>
<p>		if (  ! ( apempty ( a, &#8216;l&#8217; ) ) )<br />
		{<br />
			temp = apdelqueue ( &#038;a, &#8216;l&#8217; ) ;<br />
			land ( &#038;a, temp, curtime ) ;<br />
		}<br />
		else<br />
		{<br />
			if ( ! ( apempty ( a, &#8216;t&#8217; ) ) )<br />
			{<br />
				temp = apdelqueue ( &#038;a, &#8216;t&#8217; ) ;<br />
				fly ( &#038;a, temp, curtime ) ;<br />
			}<br />
			else<br />
				idle ( &#038;a, curtime ) ;<br />
		}<br />
	}</p>
<p>	conclude ( &#038;a, endtime ) ;</p>
<p>    getch( ) ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/simulation-of-an-airport/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tower Of Hanoi</title>
		<link>http://www.c-cplusplus.com/tower-of-hanoi</link>
		<comments>http://www.c-cplusplus.com/tower-of-hanoi#comments</comments>
		<pubDate>Fri, 13 Aug 2010 10:06:19 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Tricky Programs]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=188</guid>
		<description><![CDATA[Tower of hanoi is a historical problem, which can be easily expressed using recursion. There are N disks of decreasing size stacked on one needle, and two other empty needles. Tower of hanoi is required to stack all the disks onto a second needle in the decreasing order of size. The third needle can be [...]]]></description>
			<content:encoded><![CDATA[<p>Tower of hanoi is a historical problem, which can be easily expressed using recursion. There are N disks of decreasing size stacked on one needle, and two other empty needles.<a href="http://www.c-cplusplus.com"> Tower of hanoi</a> is required to stack all the disks onto a second needle in the decreasing order of size. The third needle can be used as a temporary storage. The movement of the disks must confirm to the following rules,<br />
<span id="more-188"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>1. Only one disk may be moved at a time<br />
2. A disk can be moved from any needle to any other.<br />
3. The larger disk should not rest upon a smaller one.</p>
<p><strong>/* Program of towers of hanoi. */</strong></p>
<p>#include  < stdio.h ><br />
#include  < conio.h > </p>
<p>void move ( int, char, char, char ) ;</p>
<p>void main( )<br />
{<br />
	int n = 3 ;</p>
<p>	clrscr( ) ;</p>
<p>	move ( n, &#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217; ) ;</p>
<p>	getch( ) ;<br />
}</p>
<p>void move ( int n, char sp, char ap, char ep )<br />
{<br />
	if ( n == 1 )<br />
		printf (&#8220;\nMove from %c to %c &#8220;, sp, ep ) ;<br />
	else<br />
	{<br />
		move ( n &#8211; 1, sp, ep, ap ) ;<br />
		move ( 1, sp, &#8216; &#8216;, ep ) ;<br />
		move ( n &#8211; 1, ap, sp, ep ) ;<br />
	}<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/tower-of-hanoi/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Function Calls and Stack</title>
		<link>http://www.c-cplusplus.com/function-calls-and-stack</link>
		<comments>http://www.c-cplusplus.com/function-calls-and-stack#comments</comments>
		<pubDate>Thu, 12 Aug 2010 11:01:45 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Tricky Programs]]></category>
		<category><![CDATA[Function Calls and Stack]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=185</guid>
		<description><![CDATA[A stack is used by programming languages for implementing function calls. C program to check how function calls are made using stack.






/* To show the use of stack in function calls */
#include  < stdio.h >
#include  < conio.h >
#include  < stdlib.h >
#include  < dos.h > 
unsigned int far *ptr ;
void ( *p [...]]]></description>
			<content:encoded><![CDATA[<p>A stack is used by programming languages for implementing function calls. C program to check how function calls are made using<a href="http://www.c-cplusplus.com"> stack</a>.<br />
<span id="more-185"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>/* To show the use of stack in function calls */</p>
<p>#include  < stdio.h ><br />
#include  < conio.h ><br />
#include  < stdlib.h ><br />
#include  < dos.h > </p>
<p>unsigned int far *ptr ;<br />
void ( *p )( void ) ;</p>
<p>void f1( ) ;<br />
void f2( ) ;</p>
<p>void main( )<br />
{<br />
	f1( ) ;<br />
	f2( ) ;</p>
<p>	printf ( &#8220;\nback to main&#8230;&#8221; ) ;<br />
<br />
	exit ( 1 ) ;<br />
}</p>
<p>void f1( )<br />
{<br />
	ptr = ( unsigned int far * ) MK_FP ( _SS, _SP + 2 ) ;<br />
<br />
	printf ( &#8220;\n%d&#8221;, *ptr ) ;</p>
<p>	p = ( void ( * )( ) ) MK_FP ( _CS, *ptr ) ;<br />
	( *p )( ) ;<br />
	printf ( &#8220;\nI am f1( ) function &#8221; ) ;<br />
}</p>
<p>void f2( )<br />
{<br />
	printf ( &#8220;\nI am f2( ) function&#8221; ) ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/function-calls-and-stack/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
