<?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/" >

<channel><title><![CDATA[Hamed Kiani (Ph.D.) - Coding Interview]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview]]></link><description><![CDATA[Coding Interview]]></description><pubDate>Sat, 17 Jan 2026 04:53:38 -0800</pubDate><generator>Weebly</generator><item><title><![CDATA[A class implementation of Binary Search Tree in C++]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/a-class-implementation-of-binary-search-tree-in-c]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/a-class-implementation-of-binary-search-tree-in-c#comments]]></comments><pubDate>Fri, 18 Sep 2015 15:12:59 GMT</pubDate><category><![CDATA[Trees and Graphs]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/a-class-implementation-of-binary-search-tree-in-c</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616 [...] ]]></description><content:encoded><![CDATA[<div><div id="940122746569782051" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 4 Trees and Graphs</span><span style="color: #888888">This is a class implementation of Binary Search Tree (BST), containing:</span><span style="color: #888888">   inser(): insert a value in a BST</span><span style="color: #888888">   isBalanced(): check if a BST is balanced, a BST is balanced if the difference of left and right subtrees height is atmost one!</span><span style="color: #888888">   getHeight(): returns the height of a BST</span><span style="color: #888888">   deleteBST(): deletes a BST      </span><span style="color: #888888">   inOrder():      prints a BST i in-order fashion</span><span style="color: #888888">   preOrder(): prints a BST i pre-order fashion</span><span style="color: #888888">   postOrder(): prints a BST i post-order fashion</span><span style="color: #888888">By: Hamed Kiani (Sep. 18, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #888888">// tree node, including left and right pointers, data </span><span style="color: #008800; font-weight: bold">struct</span> Node{        Node(<span style="color: #333399; font-weight: bold">int</span> value)<span style="color: #333333">:</span> data(value), left(<span style="color: #007020">NULL</span>), right(<span style="color: #007020">NULL</span>) {}        <span style="color: #333399; font-weight: bold">int</span> data;        Node <span style="color: #333333">*</span>left;        Node <span style="color: #333333">*</span>right;};<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #888888">// BST class</span><span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">BST</span>{<span style="color: #997700; font-weight: bold">private:</span>        Node <span style="color: #333333">*</span>_root;        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">insert</span>(Node <span style="color: #333333">*</span>treeNode, <span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isBalanced</span>(Node <span style="color: #333333">*</span>treeNode);        <span style="color: #333399; font-weight: bold">int</span>  <span style="color: #0066BB; font-weight: bold">getHeight</span>(Node <span style="color: #333333">*</span>treeNode);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">deleteBST</span>(Node <span style="color: #333333">*</span>treeNode);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">inOrder</span>(Node <span style="color: #333333">*</span> treeNode);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">preOrder</span>(Node <span style="color: #333333">*</span> treeNode);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">postOrder</span>(Node <span style="color: #333333">*</span> treeNode);<span style="color: #997700; font-weight: bold">public:</span>        BST();  <span style="color: #888888">// constructor     </span>        <span style="color: #333333">~</span>BST();     <span style="color: #888888">// destractor</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">insert</span>(<span style="color: #333399; font-weight: bold">int</span> data){ insert(_root, data);}               <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">getHeight</span>(){<span style="color: #008800; font-weight: bold">return</span> getHeight(_root);}        Node <span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">getMaxNode</span>();        Node <span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">getMinNode</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">deleteBST</span>() {deleteBST(_root);}        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isBalanced</span>(){<span style="color: #008800; font-weight: bold">return</span> isBalanced(_root);        }        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">inOrder</span>() {inOrder(_root);}        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">preOrder</span>(){preOrder(_root);}        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">postOrder</span>(){postOrder(_root);}};<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span>BST<span style="color: #333333">::</span>BST(){        _root <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> BST<span style="color: #333333">::</span>insert(Node <span style="color: #333333">*</span>treeNode, <span style="color: #333399; font-weight: bold">int</span> data){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)        {                treeNode <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node(data);                           _root <span style="color: #333333">=</span> treeNode;                   }        <span style="color: #008800; font-weight: bold">else</span>        {                <span style="color: #008800; font-weight: bold">if</span> (data <span style="color: #333333">&lt;</span> treeNode<span style="color: #333333">-&gt;</span>data)                {                        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode<span style="color: #333333">-&gt;</span>left)                        {                                Node <span style="color: #333333">*</span>treeTemp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node(data);                                treeNode<span style="color: #333333">-&gt;</span>left <span style="color: #333333">=</span> treeTemp;                        }                        <span style="color: #008800; font-weight: bold">else</span>                                insert(treeNode<span style="color: #333333">-&gt;</span>left, data);                }                <span style="color: #008800; font-weight: bold">else</span>                {                        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode<span style="color: #333333">-&gt;</span>right)                        {                                Node <span style="color: #333333">*</span>treeTemp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node(data);                                                         treeNode<span style="color: #333333">-&gt;</span>right <span style="color: #333333">=</span> treeTemp;                        }                        <span style="color: #008800; font-weight: bold">else</span>                                insert(treeNode<span style="color: #333333">-&gt;</span>right, data);                }        }}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> BST<span style="color: #333333">::</span>getHeight(Node <span style="color: #333333">*</span>treeNode){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">1</span> <span style="color: #333333">+</span> max(getHeight(treeNode<span style="color: #333333">-&gt;</span>left) , getHeight(treeNode<span style="color: #333333">-&gt;</span>right));}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> BST<span style="color: #333333">::</span>isBalanced(Node <span style="color: #333333">*</span>treeNode){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;        <span style="color: #333399; font-weight: bold">int</span> leftHeight <span style="color: #333333">=</span> getHeight(treeNode<span style="color: #333333">-&gt;</span>left);        <span style="color: #333399; font-weight: bold">int</span> rightHeight <span style="color: #333333">=</span> getHeight(treeNode<span style="color: #333333">-&gt;</span>right);        <span style="color: #008800; font-weight: bold">if</span> (abs(leftHeight <span style="color: #333333">-</span> rightHeight) <span style="color: #333333">&gt;</span> <span style="color: #0000DD; font-weight: bold">1</span>)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">true</span>;}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span>Node <span style="color: #333333">*</span> BST<span style="color: #333333">::</span>getMaxNode(){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>_root)        {                cout <span style="color: #333333">&lt;&lt;</span>  <span style="background-color: #fff0f0">" the BST is empty!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        }        Node <span style="color: #333333">*</span> treeNode <span style="color: #333333">=</span> _root;        <span style="color: #008800; font-weight: bold">while</span>(treeNode<span style="color: #333333">-&gt;</span>right)                treeNode <span style="color: #333333">=</span> treeNode <span style="color: #333333">-&gt;</span>right;        <span style="color: #008800; font-weight: bold">return</span> treeNode;}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span>Node <span style="color: #333333">*</span> BST<span style="color: #333333">::</span>getMinNode(){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>_root)        {                cout <span style="color: #333333">&lt;&lt;</span>  <span style="background-color: #fff0f0">" the BST is empty!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        }        Node <span style="color: #333333">*</span> treeNode <span style="color: #333333">=</span> _root;        <span style="color: #008800; font-weight: bold">while</span>(treeNode<span style="color: #333333">-&gt;</span>left)                treeNode <span style="color: #333333">=</span> treeNode <span style="color: #333333">-&gt;</span>left;        <span style="color: #008800; font-weight: bold">return</span> treeNode;}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> BST<span style="color: #333333">::</span>deleteBST(Node <span style="color: #333333">*</span>treeNode) {        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)                <span style="color: #008800; font-weight: bold">return</span>;        Node <span style="color: #333333">*</span> curTreeNode <span style="color: #333333">=</span> treeNode;        Node <span style="color: #333333">*</span> leftTreeNode <span style="color: #333333">=</span> treeNode<span style="color: #333333">-&gt;</span>left;        Node <span style="color: #333333">*</span> rightTreeNode <span style="color: #333333">=</span> treeNode<span style="color: #333333">-&gt;</span>right;        <span style="color: #008800; font-weight: bold">delete</span>(curTreeNode);        deleteBST(leftTreeNode);        deleteBST(rightTreeNode);}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span>BST<span style="color: #333333">::~</span>BST(){        deleteBST();}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> BST<span style="color: #333333">::</span>inOrder(Node <span style="color: #333333">*</span> treeNode){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)                <span style="color: #008800; font-weight: bold">return</span>;        inOrder(treeNode<span style="color: #333333">-&gt;</span>left);        cout <span style="color: #333333">&lt;&lt;</span> treeNode<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;        inOrder(treeNode<span style="color: #333333">-&gt;</span>right);}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> BST<span style="color: #333333">::</span>preOrder(Node <span style="color: #333333">*</span> treeNode){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)                <span style="color: #008800; font-weight: bold">return</span>;        cout <span style="color: #333333">&lt;&lt;</span> treeNode<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span>;        preOrder(treeNode<span style="color: #333333">-&gt;</span>left);        preOrder(treeNode<span style="color: #333333">-&gt;</span>right);}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> BST<span style="color: #333333">::</span>postOrder(Node <span style="color: #333333">*</span> treeNode){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>treeNode)                <span style="color: #008800; font-weight: bold">return</span>;        postOrder(treeNode<span style="color: #333333">-&gt;</span>left);        postOrder(treeNode<span style="color: #333333">-&gt;</span>right);        cout <span style="color: #333333">&lt;&lt;</span> treeNode<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span>;}<span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #888888">/////////////////////////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        BST myBST;        myBST.insert(<span style="color: #0000DD; font-weight: bold">5</span>);        myBST.insert(<span style="color: #0000DD; font-weight: bold">10</span>);        myBST.insert(<span style="color: #0000DD; font-weight: bold">3</span>);        myBST.insert(<span style="color: #0000DD; font-weight: bold">51</span>);        myBST.insert(<span style="color: #0000DD; font-weight: bold">110</span>);        myBST.insert(<span style="color: #0000DD; font-weight: bold">13</span>);                <span style="color: #333399; font-weight: bold">int</span> h <span style="color: #333333">=</span> myBST.getHeight();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the height of this BSt is : "</span> <span style="color: #333333">&lt;&lt;</span> h <span style="color: #333333">&lt;&lt;</span> endl;        Node <span style="color: #333333">*</span> mx <span style="color: #333333">=</span> myBST.getMaxNode();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"max value: "</span> <span style="color: #333333">&lt;&lt;</span> mx<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> endl;        Node <span style="color: #333333">*</span> mi <span style="color: #333333">=</span> myBST.getMinNode();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"min value: "</span> <span style="color: #333333">&lt;&lt;</span> mi<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #333399; font-weight: bold">bool</span> isbal <span style="color: #333333">=</span> myBST.isBalanced();        <span style="color: #008800; font-weight: bold">if</span> (isbal)                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"BST is balanced! "</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">else</span>                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"BST is not balanced! "</span> <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" in-order traverse is : "</span> <span style="color: #333333">&lt;&lt;</span> endl;        myBST.inOrder();cout <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" pre-order traverse is : "</span> <span style="color: #333333">&lt;&lt;</span> endl;        myBST.preOrder();cout <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" post-order traverse is : "</span> <span style="color: #333333">&lt;&lt;</span> endl;        myBST.postOrder();cout <span style="color: #333333">&lt;&lt;</span> endl;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Write a program to sort a stack in ascending order. c++]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/write-a-program-to-sort-a-stack-in-ascending-order-c]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/write-a-program-to-sort-a-stack-in-ascending-order-c#comments]]></comments><pubDate>Tue, 15 Sep 2015 14:36:44 GMT</pubDate><category><![CDATA[Stack and Queue]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/write-a-program-to-sort-a-stack-in-ascending-order-c</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616 [...] ]]></description><content:encoded><![CDATA[<div><div id="908139253997053683" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">Write a program to sort a stack in ascending order.</span><span style="color: #888888">You should not make any assumptions about how the stack is implemented.</span><span style="color: #888888">The following are the only functions that should be used to write this program: push | pop | peek | isEmpty.</span><span style="color: #888888">peek: return the top value of stack without removing it.</span><span style="color: #888888">We use two stacks for ordering the elements. The main stack contains the elements in a sorted fashion. </span><span style="color: #888888">The auxulary stack is used to sort the elements of the main stack if required. </span><span style="color: #888888">memory  O(n)</span><span style="color: #888888">time       O(n^2)</span><span style="color: #888888">Questions:</span><span style="color: #888888">how can we write this function with the memory of O(1)?</span><span style="color: #888888">how about a time of O(n)? is that possible?</span><span style="color: #888888">By: Hamed Kiani (Sep. 14, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">sortedStack</span>{<span style="color: #997700; font-weight: bold">private:</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>_mainStack;     <span style="color: #888888">// main ordered stack</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>_auxStack;              <span style="color: #888888">// auxulary stack for ordering</span>        <span style="color: #333399; font-weight: bold">int</span> _size;                       <span style="color: #888888">// the max size of stack for overflow checking</span>        <span style="color: #333399; font-weight: bold">int</span> _top;                        <span style="color: #888888">// index of the top element of main stack</span>        <span style="color: #333399; font-weight: bold">int</span> _aux_top;            <span style="color: #888888">// index of the top element of aux stack</span><span style="color: #997700; font-weight: bold">public:</span>        sortedStack(<span style="color: #333399; font-weight: bold">int</span> n);      <span style="color: #888888">// constructor</span>        <span style="color: #333333">~</span>sortedStack();             <span style="color: #888888">// destructor</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isEmpty</span>();           <span style="color: #888888">// is the stack empty?</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isFull</span>();            <span style="color: #888888">// is the stack full?</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">push</span>(<span style="color: #333399; font-weight: bold">int</span> data);       <span style="color: #888888">// ordered push</span><span style="color: #888888">//         void ordered_push(int data);</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">pop</span>();                       <span style="color: #888888">// normal pop operation</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">peek</span>();                       <span style="color: #888888">// peek function   </span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print</span>();             <span style="color: #888888">// print the main stack</span>};<span style="color: #888888">////////////////////////////////////////////////////////////</span>sortedStack<span style="color: #333333">::</span>sortedStack(<span style="color: #333399; font-weight: bold">int</span> n){        _size <span style="color: #333333">=</span> n;        _top <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;        _aux_top <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;        <span style="color: #888888">// initializaing the main and aux stacks</span>        _mainStack <span style="color: #333333">=</span> (<span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span>) malloc(<span style="color: #008800; font-weight: bold">sizeof</span>(<span style="color: #333399; font-weight: bold">int</span>) <span style="color: #333333">*</span> _size);        _auxStack  <span style="color: #333333">=</span> (<span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span>) malloc(<span style="color: #008800; font-weight: bold">sizeof</span>(<span style="color: #333399; font-weight: bold">int</span>) <span style="color: #333333">*</span> _size);}<span style="color: #888888">////////////////////////////////////////////////////////////</span>sortedStack<span style="color: #333333">::~</span>sortedStack(){        free(_mainStack);        free(_auxStack);        }<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> sortedStack<span style="color: #333333">::</span>isEmpty(){        <span style="color: #008800; font-weight: bold">return</span> (_top <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> sortedStack<span style="color: #333333">::</span>isFull(){        <span style="color: #008800; font-weight: bold">return</span> (_top <span style="color: #333333">==</span> _size<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> sortedStack<span style="color: #333333">::</span>push(<span style="color: #333399; font-weight: bold">int</span> data){        <span style="color: #888888">// is full, not possible to push a new element</span>        <span style="color: #008800; font-weight: bold">if</span> (isFull())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack overflow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #888888">// if the stack is empty or the new element is smaller </span>        <span style="color: #888888">//  than the top value just do simple push</span>        <span style="color: #333399; font-weight: bold">int</span> top_data <span style="color: #333333">=</span> peek();        <span style="color: #008800; font-weight: bold">if</span> ((data <span style="color: #333333">&gt;=</span> top_data) <span style="color: #333333">|</span> (isEmpty()))        {                _mainStack[<span style="color: #333333">++</span>_top] <span style="color: #333333">=</span> data;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #888888">// otherwise, use aux-stack to push the new element in the right place</span>        <span style="color: #008800; font-weight: bold">while</span>(<span style="color: #333333">~</span>isEmpty() <span style="color: #333333">&amp;</span> (top_data <span style="color: #333333">&gt;</span> data ))        {                _auxStack[<span style="color: #333333">++</span>_aux_top] <span style="color: #333333">=</span> top_data;                pop();                top_data <span style="color: #333333">=</span> peek();        }        _mainStack[<span style="color: #333333">++</span>_top] <span style="color: #333333">=</span> data;        <span style="color: #888888">// re-push the lements in aux-stack back to the main stack</span>        <span style="color: #008800; font-weight: bold">while</span>(_aux_top <span style="color: #333333">&gt;=</span> <span style="color: #0000DD; font-weight: bold">0</span>)        {                _mainStack[<span style="color: #333333">++</span>_top] <span style="color: #333333">=</span> _auxStack[_aux_top<span style="color: #333333">--</span>];                 }        <span style="color: #008800; font-weight: bold">return</span>;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> sortedStack<span style="color: #333333">::</span>pop(){        <span style="color: #008800; font-weight: bold">if</span> (isEmpty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack unerflow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        _top<span style="color: #333333">--</span>;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> sortedStack<span style="color: #333333">::</span>peek(){        <span style="color: #008800; font-weight: bold">if</span> (isEmpty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack is empty!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }        <span style="color: #008800; font-weight: bold">return</span> _mainStack[_top];}<span style="color: #333399; font-weight: bold">void</span> sortedStack<span style="color: #333333">::</span>print(){        <span style="color: #008800; font-weight: bold">if</span> (isEmpty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack is empty!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the elemets of stack "</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">0</span>; i<span style="color: #333333">&lt;=</span>_top; i<span style="color: #333333">++</span>)                cout <span style="color: #333333">&lt;&lt;</span> _mainStack[i] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;        cout <span style="color: #333333">&lt;&lt;</span> endl; }<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        sortedStack myStack(<span style="color: #0000DD; font-weight: bold">10</span>);        myStack.push(<span style="color: #0000DD; font-weight: bold">1</span>);        myStack.push(<span style="color: #0000DD; font-weight: bold">15</span>);        myStack.push(<span style="color: #0000DD; font-weight: bold">32</span>);        myStack.push(<span style="color: #0000DD; font-weight: bold">4</span>);        myStack.push(<span style="color: #0000DD; font-weight: bold">1</span>);        myStack.print();                myStack.pop();        myStack.pop();        myStack.push(<span style="color: #0000DD; font-weight: bold">77</span>);        myStack.push(<span style="color: #0000DD; font-weight: bold">10</span>);        myStack.pop();        myStack.push(<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">15</span>);           myStack.print();                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Implement a MyQueue class which implements a queue using two stacks : c++]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/implement-a-myqueue-class-which-implements-a-queue-using-two-stacks-c]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/implement-a-myqueue-class-which-implements-a-queue-using-two-stacks-c#comments]]></comments><pubDate>Mon, 07 Sep 2015 16:24:26 GMT</pubDate><category><![CDATA[Stack and Queue]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/implement-a-myqueue-class-which-implements-a-queue-using-two-stacks-c</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616 [...] ]]></description><content:encoded><![CDATA[<div><div id="575362352707082850" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">Implement a MyQueue class which implements a queue using two stacks.</span><span style="color: #888888">We use two stacks, s1 and s2 for enqueue and dequeue. </span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (Sep. 7, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">struct</span> Node{        <span style="color: #333399; font-weight: bold">int</span> data;        Node <span style="color: #333333">*</span>next;};<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">MyQueue</span>{<span style="color: #997700; font-weight: bold">private:</span>        Node <span style="color: #333333">*</span>_s1_head;     <span style="color: #888888">// enQueue is done using s1</span>        Node <span style="color: #333333">*</span>_s2_head;     <span style="color: #888888">// s2 is used for dequeue</span>        <span style="color: #333399; font-weight: bold">int</span> _s1_size;    <span style="color: #888888">// the size of s1 stack    </span>        <span style="color: #333399; font-weight: bold">int</span> _s2_size;    <span style="color: #888888">// the size of s2 stack</span>        <span style="color: #997700; font-weight: bold">public:</span>        MyQueue();        <span style="color: #333333">~</span>MyQueue();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">enQueue</span>(<span style="color: #333399; font-weight: bold">int</span> data);    <span style="color: #888888">// enqueue data value using s1</span>        Node<span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">deQueue</span>();             <span style="color: #888888">// dequeue using s1 and s2</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_s1</span>();          <span style="color: #888888">// print elements of s1 stack</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_s2</span>();          <span style="color: #888888">// print elements of s2 stack</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isEmpty</span>(Node <span style="color: #333333">*</span>); <span style="color: #888888">// check if s1 or s2 are empty</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">get_s1_size</span>(){<span style="color: #008800; font-weight: bold">return</span> _s1_size;}        <span style="color: #888888">// return size of s1</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">get_s2_size</span>(){<span style="color: #008800; font-weight: bold">return</span> _s2_size;}        <span style="color: #888888">// return size of s2</span>};<span style="color: #888888">////////////////////////////////////////////////////////////////////</span>MyQueue<span style="color: #333333">::</span>MyQueue(){        _s1_head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        _s2_head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;         _s1_size <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        _s2_size <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the stack and queue are initialized! "</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////////////</span>MyQueue<span style="color: #333333">::~</span>MyQueue(){        Node <span style="color: #333333">*</span>temp;                <span style="color: #008800; font-weight: bold">while</span>(_s1_head <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                temp <span style="color: #333333">=</span> _s1_head;                _s1_head <span style="color: #333333">=</span> _s1_head<span style="color: #333333">-&gt;</span>next;                <span style="color: #008800; font-weight: bold">delete</span> temp;        }        <span style="color: #008800; font-weight: bold">while</span>(_s2_head <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                temp <span style="color: #333333">=</span> _s2_head;                            _s2_head <span style="color: #333333">=</span> _s2_head<span style="color: #333333">-&gt;</span>next;                <span style="color: #008800; font-weight: bold">delete</span> temp;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the stack and queue are deleted! "</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////////////</span><span style="color: #888888">// enQueue data at begin of s1 (push s1 stack)</span><span style="color: #333399; font-weight: bold">void</span> MyQueue<span style="color: #333333">::</span>enQueue(<span style="color: #333399; font-weight: bold">int</span> data){        Node <span style="color: #333333">*</span>temp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node;        temp<span style="color: #333333">-&gt;</span>data <span style="color: #333333">=</span> data;        temp<span style="color: #333333">-&gt;</span>next <span style="color: #333333">=</span> _s1_head;        _s1_head <span style="color: #333333">=</span> temp;        _s1_size<span style="color: #333333">++</span>;          cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the enQueue value is: "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////////////</span><span style="color: #888888">// For deQueue, </span><span style="color: #888888">// we copy all from s1 to ss except the last node of s1 O(n)</span><span style="color: #888888">// re-copy s2 to s1 in reverse order O(n)</span><span style="color: #888888">// update the s1_size and s2_size</span>Node<span style="color: #333333">*</span> MyQueue<span style="color: #333333">::</span>deQueue(){        <span style="color: #888888">// the queue is empty and dequeue is not possible</span>        <span style="color: #008800; font-weight: bold">if</span> ((_s1_size <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">0</span>) )        {                cout<span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"dequeue is not possible !"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;                         }        <span style="color: #888888">// there is just one element in queue</span>        <span style="color: #008800; font-weight: bold">if</span> ((_s1_size <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">1</span>) )        {                Node <span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _s1_head;                _s1_head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;                _s1_size<span style="color: #333333">--</span>;                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the dequeue value is: "</span> <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> temp;        }        <span style="color: #888888">// there are more than one element in the queue</span>        _s2_head <span style="color: #333333">=</span> _s1_head;        _s1_head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        Node <span style="color: #333333">*</span> temp;        temp <span style="color: #333333">=</span> _s2_head;        _s1_size<span style="color: #333333">--</span>;        <span style="color: #008800; font-weight: bold">while</span>(temp<span style="color: #333333">-&gt;</span>next<span style="color: #333333">-&gt;</span>next <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>next;        }        Node <span style="color: #333333">*</span> temp2;        temp2 <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>next;        temp<span style="color: #333333">-&gt;</span>next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;               _s1_head <span style="color: #333333">=</span> _s2_head;        _s2_head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the dequeue value is: "</span> <span style="color: #333333">&lt;&lt;</span> temp2<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">return</span> temp2;}<span style="color: #888888">////////////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> MyQueue<span style="color: #333333">::</span>print_s1(){        <span style="color: #008800; font-weight: bold">if</span> (_s1_head <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack s1 is empty"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        Node<span style="color: #333333">*</span> temp;        temp <span style="color: #333333">=</span> _s1_head;        <span style="color: #008800; font-weight: bold">while</span>(temp)        {                cout <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>next;        }        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">//////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> MyQueue<span style="color: #333333">::</span>print_s2(){        <span style="color: #008800; font-weight: bold">if</span> (_s2_head <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack s2 is empty"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        Node<span style="color: #333333">*</span> temp;        temp <span style="color: #333333">=</span> _s2_head;        <span style="color: #008800; font-weight: bold">while</span>(temp)        {                cout <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>next;        }        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        MyQueue queue;        queue.enQueue(<span style="color: #0000DD; font-weight: bold">1</span>);                cout <span style="color: #333333">&lt;&lt;</span> queue.get_s1_size() <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> queue.get_s2_size() <span style="color: #333333">&lt;&lt;</span> endl;        queue.print_s1();        queue.print_s2();        Node <span style="color: #333333">*</span> temp <span style="color: #333333">=</span> queue.deQueue();        temp <span style="color: #333333">=</span> queue.deQueue();        temp <span style="color: #333333">=</span> queue.deQueue();        temp <span style="color: #333333">=</span> queue.deQueue();        temp <span style="color: #333333">=</span> queue.deQueue();        queue.print_s1();        queue.print_s2();        queue.enQueue(<span style="color: #0000DD; font-weight: bold">3</span>);        queue.enQueue(<span style="color: #0000DD; font-weight: bold">4</span>);        queue.print_s1();        queue.print_s2();        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Imagine a (literal) stack of plates.If the stack gets too high, it might topple.Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this.Implement ]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/imagine-a-literal-stack-of-platesif-the-stack-gets-too-high-it-might-toppletherefore-in-real-life-we-would-likely-start-a-new-stack-when-the-previous-stack-exceeds-some-threshold-implement-a-data-structure-setofstacks-that-mimics-thisimplement]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/imagine-a-literal-stack-of-platesif-the-stack-gets-too-high-it-might-toppletherefore-in-real-life-we-would-likely-start-a-new-stack-when-the-previous-stack-exceeds-some-threshold-implement-a-data-structure-setofstacks-that-mimics-thisimplement#comments]]></comments><pubDate>Mon, 07 Sep 2015 12:21:11 GMT</pubDate><category><![CDATA[Stack and Queue]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/imagine-a-literal-stack-of-platesif-the-stack-gets-too-high-it-might-toppletherefore-in-real-life-we-would-likely-start-a-new-stack-when-the-previous-stack-exceeds-some-threshold-implement-a-data-structure-setofstacks-that-mimics-thisimplement</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616 [...] ]]></description><content:encoded><![CDATA[<div><div id="156253461948947446" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">Imagine a (literal) stack of plates.</span><span style="color: #888888">If the stack gets too high, it might topple.</span><span style="color: #888888">Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. </span><span style="color: #888888">Implement a data structure SetOfStacks that mimics this.</span><span style="color: #888888">Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.</span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (Sep. 7, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">SetOfStacks</span>{<span style="color: #997700; font-weight: bold">private:</span>        <span style="color: #333399; font-weight: bold">int</span> _numOfStk;   <span style="color: #888888">// number of stacks</span>        <span style="color: #333399; font-weight: bold">int</span> _thOfStk;    <span style="color: #888888">// threshold of stacks</span>        <span style="color: #333399; font-weight: bold">int</span> _curStk;     <span style="color: #888888">// index of current stack</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">**</span>_stacks;       <span style="color: #888888">// two-dim array to keep stacks elements</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>_elmts; <span style="color: #888888">// number of elements in each stack</span><span style="color: #997700; font-weight: bold">public:</span>        SetOfStacks(<span style="color: #333399; font-weight: bold">int</span> n, <span style="color: #333399; font-weight: bold">int</span> t);        <span style="color: #333333">~</span>SetOfStacks();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">push</span>(<span style="color: #333399; font-weight: bold">int</span> n);  <span style="color: #888888">// push the element n on the stack</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">pop</span>();                        <span style="color: #888888">// pop operation</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">pop</span>(<span style="color: #333399; font-weight: bold">int</span> k);            <span style="color: #888888">// pop operation from stack k</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isFull</span>(<span style="color: #333399; font-weight: bold">int</span> k);        <span style="color: #888888">// is stack k full?</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isFull</span>();            <span style="color: #888888">// is the stack full?</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isEmpty</span>(<span style="color: #333399; font-weight: bold">int</span> k);<span style="color: #888888">//        is the stack k empty?</span>        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">isEmpty</span>();           <span style="color: #888888">// is the stack empty?</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">printAll</span>();  <span style="color: #888888">// print all elements</span>        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">printK</span>(<span style="color: #333399; font-weight: bold">int</span> k);        <span style="color: #888888">// print elemets of stack k</span>};<span style="color: #888888">////////////////////////////////////////////////////////////       </span>SetOfStacks<span style="color: #333333">::</span>SetOfStacks(<span style="color: #333399; font-weight: bold">int</span> n, <span style="color: #333399; font-weight: bold">int</span> t){        _numOfStk <span style="color: #333333">=</span> n;        _thOfStk  <span style="color: #333333">=</span> t;        _curStk   <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #888888">// _elmts = new int[_numOfStk];</span>        _elmts <span style="color: #333333">=</span> (<span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span>) malloc(_numOfStk <span style="color: #333333">*</span> <span style="color: #008800; font-weight: bold">sizeof</span>(<span style="color: #333399; font-weight: bold">int</span>));        <span style="color: #008800; font-weight: bold">for</span>(<span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; i <span style="color: #333333">&lt;</span> _numOfStk; i<span style="color: #333333">++</span>)                _elmts[i] <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;  <span style="color: #888888">// all are empty</span>        _stacks <span style="color: #333333">=</span> (<span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">**</span>) malloc(_numOfStk <span style="color: #333333">*</span> <span style="color: #008800; font-weight: bold">sizeof</span>(<span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>));        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; i<span style="color: #333333">&lt;</span>_numOfStk; i<span style="color: #333333">++</span>)                _stacks[i] <span style="color: #333333">=</span> (<span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>) malloc(_thOfStk <span style="color: #333333">*</span> <span style="color: #008800; font-weight: bold">sizeof</span>(<span style="color: #333399; font-weight: bold">int</span>));        cout <span style="color: #333333">&lt;&lt;</span> n <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stacks are created, windex 0,..., "</span> <span style="color: #333333">&lt;&lt;</span> n<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span> <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the threshold for each stack is "</span> <span style="color: #333333">&lt;&lt;</span> t <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" elements "</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////</span>SetOfStacks<span style="color: #333333">::~</span>SetOfStacks(){        free(_elmts);        free(_stacks);        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stacks are deleted!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> SetOfStacks<span style="color: #333333">::</span>isFull(<span style="color: #333399; font-weight: bold">int</span> k){        <span style="color: #008800; font-weight: bold">return</span> (_elmts[k] <span style="color: #333333">==</span> _thOfStk<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> SetOfStacks<span style="color: #333333">::</span>isFull(){        <span style="color: #008800; font-weight: bold">return</span> isFull(_numOfStk<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> SetOfStacks<span style="color: #333333">::</span>isEmpty(<span style="color: #333399; font-weight: bold">int</span> k){        <span style="color: #008800; font-weight: bold">return</span> (_elmts[k] <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">bool</span> SetOfStacks<span style="color: #333333">::</span>isEmpty(){        <span style="color: #008800; font-weight: bold">return</span> isEmpty(<span style="color: #0000DD; font-weight: bold">0</span>);}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> SetOfStacks<span style="color: #333333">::</span>push(<span style="color: #333399; font-weight: bold">int</span> n){        <span style="color: #008800; font-weight: bold">if</span> (isFull())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack overflow! cann't push "</span> <span style="color: #333333">&lt;&lt;</span> n <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" value! "</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }               <span style="color: #008800; font-weight: bold">if</span> (_elmts[_curStk] <span style="color: #333333">==</span> _thOfStk <span style="color: #333333">-</span> <span style="color: #0000DD; font-weight: bold">1</span>)                _curStk<span style="color: #333333">++</span>;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"value "</span> <span style="color: #333333">&lt;&lt;</span> n <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" pushed on stack "</span> <span style="color: #333333">&lt;&lt;</span> _curStk <span style="color: #333333">&lt;&lt;</span>  endl;        _elmts[_curStk]<span style="color: #333333">++</span>;        _stacks[_curStk][_elmts[_curStk]] <span style="color: #333333">=</span> n;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> SetOfStacks<span style="color: #333333">::</span>pop(){        <span style="color: #008800; font-weight: bold">if</span> (isEmpty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack under flow! "</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> _stacks[_curStk][_elmts[_curStk]];        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"value "</span> <span style="color: #333333">&lt;&lt;</span> i <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is popped from stack "</span> <span style="color: #333333">&lt;&lt;</span> _curStk <span style="color: #333333">&lt;&lt;</span> endl;        _elmts[_curStk]<span style="color: #333333">--</span>;          <span style="color: #008800; font-weight: bold">if</span> ((_elmts[_curStk] <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>) <span style="color: #333333">&amp;</span> (_curStk <span style="color: #333333">&gt;</span> <span style="color: #0000DD; font-weight: bold">0</span>))                _curStk<span style="color: #333333">--</span>;                <span style="color: #008800; font-weight: bold">return</span> i;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> SetOfStacks<span style="color: #333333">::</span>pop(<span style="color: #333399; font-weight: bold">int</span> k){        <span style="color: #008800; font-weight: bold">if</span> (isEmpty(k))        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack under flow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }        <span style="color: #008800; font-weight: bold">if</span> ((k <span style="color: #333333">&gt;</span> _numOfStk<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>) <span style="color: #333333">|</span> (k <span style="color: #333333">&lt;</span> <span style="color: #0000DD; font-weight: bold">0</span>))        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack index is out of range! "</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }                <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> _stacks[k][_elmts[k]];        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"value "</span> <span style="color: #333333">&lt;&lt;</span> i <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is popped from stack "</span> <span style="color: #333333">&lt;&lt;</span> k <span style="color: #333333">&lt;&lt;</span> endl;        _elmts[k]<span style="color: #333333">--</span>;        <span style="color: #008800; font-weight: bold">if</span> ((_elmts[k] <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>) <span style="color: #333333">&amp;</span> (k <span style="color: #333333">&gt;</span> <span style="color: #0000DD; font-weight: bold">0</span>) <span style="color: #333333">&amp;</span> (k <span style="color: #333333">==</span> _curStk))                _curStk<span style="color: #333333">--</span>;        <span style="color: #008800; font-weight: bold">return</span> i;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> SetOfStacks<span style="color: #333333">::</span>printAll(){        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"print all the stacks:"</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; i <span style="color: #333333">&lt;</span> _numOfStk; i<span style="color: #333333">++</span>)        {                <span style="color: #008800; font-weight: bold">if</span> (isEmpty(i))                        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack "</span> <span style="color: #333333">&lt;&lt;</span> i <span style="color: #333333">&lt;&lt;</span>  <span style="background-color: #fff0f0">" is empty "</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">else</span>                {                        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; j <span style="color: #333333">&lt;=</span> _elmts[i]  ; j<span style="color: #333333">++</span>)                                cout <span style="color: #333333">&lt;&lt;</span> _stacks[i][j] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;                        cout <span style="color: #333333">&lt;&lt;</span> endl;                }        }}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">void</span> SetOfStacks<span style="color: #333333">::</span>printK(<span style="color: #333399; font-weight: bold">int</span> k){        <span style="color: #008800; font-weight: bold">if</span> (k <span style="color: #333333">&gt;</span> _numOfStk<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"index is more than the stack size!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">if</span> (k <span style="color: #333333">&lt;</span> <span style="color: #0000DD; font-weight: bold">0</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"index is less than the stack size!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">if</span> (isEmpty(k))                        {                                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack "</span> <span style="color: #333333">&lt;&lt;</span> k <span style="color: #333333">&lt;&lt;</span>  <span style="background-color: #fff0f0">" is empty "</span> <span style="color: #333333">&lt;&lt;</span> endl;                                <span style="color: #008800; font-weight: bold">return</span>;        }                <span style="color: #008800; font-weight: bold">else</span>                {                        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; j <span style="color: #333333">&lt;=</span> _elmts[k]  ; j<span style="color: #333333">++</span>)                                cout <span style="color: #333333">&lt;&lt;</span> _stacks[k][j] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;                        cout <span style="color: #333333">&lt;&lt;</span> endl;                }}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        <span style="color: #333399; font-weight: bold">int</span> n <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">2</span>;        <span style="color: #333399; font-weight: bold">int</span> t <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">3</span>;        SetOfStacks <span style="color: #0066BB; font-weight: bold">stacks</span>(n,t);                stacks.push(<span style="color: #0000DD; font-weight: bold">1</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">2</span>);         stacks.push(<span style="color: #0000DD; font-weight: bold">3</span>);                stacks.push(<span style="color: #0000DD; font-weight: bold">10</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">20</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">30</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">100</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">200</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">300</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">1000</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">2000</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">3000</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">3001</span>);        stacks.push(<span style="color: #0000DD; font-weight: bold">3002</span>);               stacks.printK(<span style="color: #0000DD; font-weight: bold">0</span>);        stacks.printK(<span style="color: #0000DD; font-weight: bold">1</span>);        stacks.printK(<span style="color: #0000DD; font-weight: bold">2</span>);        stacks.printK(<span style="color: #0000DD; font-weight: bold">3</span>);        stacks.printK(<span style="color: #0000DD; font-weight: bold">4</span>);        stacks.printAll();        stacks.pop();        stacks.pop();        stacks.pop(<span style="color: #0000DD; font-weight: bold">0</span>);        stacks.pop(<span style="color: #0000DD; font-weight: bold">0</span>);        stacks.pop(<span style="color: #0000DD; font-weight: bold">0</span>);        stacks.pop(<span style="color: #0000DD; font-weight: bold">2</span>);        stacks.printAll();        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.We design this class using array instead of linkedlist]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/design-a-stack-which-in-addition-to-push-and-pop-also-has-a-function-min-which-returns-the-minimum-element-push-pop-and-min-should-all-operate-in-o1-timewe-design-this-class-using-array-instead-of-linkedlist]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/design-a-stack-which-in-addition-to-push-and-pop-also-has-a-function-min-which-returns-the-minimum-element-push-pop-and-min-should-all-operate-in-o1-timewe-design-this-class-using-array-instead-of-linkedlist#comments]]></comments><pubDate>Wed, 02 Sep 2015 15:21:23 GMT</pubDate><category><![CDATA[Stack and Queue]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/design-a-stack-which-in-addition-to-push-and-pop-also-has-a-function-min-which-returns-the-minimum-element-push-pop-and-min-should-all-operate-in-o1-timewe-design-this-class-using-array-instead-of-linkedlist</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/********** [...] ]]></description><content:encoded><![CDATA[<div><div id="467619888674559087" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">Design a stack which, in addition to push and pop, also has a function min which returns the minimum element? </span><span style="color: #888888">Push, pop and min should all operate in O(1) time.</span><span style="color: #888888">We design this class using array instead of linkedlist</span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (Sep. 2, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">minStack</span>{<span style="color: #997700; font-weight: bold">private:</span>        <span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span> _main_stk;      <span style="color: #888888">// main stack</span>        <span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span> _aux_stk;       <span style="color: #888888">// aux. stack to keep elements in sorted</span>        <span style="color: #333399; font-weight: bold">int</span> _cap;        <span style="color: #888888">// the maximum capacity of stack</span>        <span style="color: #333399; font-weight: bold">int</span> _top;        <span style="color: #888888">// size of stack</span><span style="color: #997700; font-weight: bold">public:</span>        minStack(<span style="color: #333399; font-weight: bold">int</span> n);        <span style="color: #333333">~</span>minStack();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">push</span>(<span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">pop</span>();        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_empty</span>();        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_full</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_stk</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_aux_stk</span>();        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">getMin</span>();};minStack<span style="color: #333333">::</span>minStack(<span style="color: #333399; font-weight: bold">int</span> n){        _cap <span style="color: #333333">=</span> n;        _main_stk <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[_cap];        _aux_stk  <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[_cap];        _top <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;               cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" an object of minStack is created!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}minStack<span style="color: #333333">::~</span>minStack(){               <span style="color: #008800; font-weight: bold">delete</span>[] _main_stk;        <span style="color: #008800; font-weight: bold">delete</span>[] _aux_stk;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" an object of minStack is deleted!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">bool</span> minStack<span style="color: #333333">::</span>is_empty(){        <span style="color: #008800; font-weight: bold">return</span> (_top <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #333399; font-weight: bold">bool</span> minStack<span style="color: #333333">::</span>is_full(){        <span style="color: #008800; font-weight: bold">return</span> (_top <span style="color: #333333">==</span> _cap<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #333399; font-weight: bold">void</span> minStack<span style="color: #333333">::</span>push(<span style="color: #333399; font-weight: bold">int</span> data){        <span style="color: #008800; font-weight: bold">if</span> (is_full())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack overflow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" The element "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is pushed on the stack!"</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #888888">// if the stack is empty or data is less than all elements in the aux. stack, just push it</span>        <span style="color: #008800; font-weight: bold">if</span> ((_top <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>) <span style="color: #333333">||</span> (data <span style="color: #333333">&lt;=</span> _aux_stk[_top]))        {                _top<span style="color: #333333">++</span>;                             _main_stk[_top] <span style="color: #333333">=</span> data;                _aux_stk[_top]  <span style="color: #333333">=</span> data;                             <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #888888">// otherwise, push data on the main stack and duplicate the current min in the new position</span>        <span style="color: #333399; font-weight: bold">int</span> temp <span style="color: #333333">=</span> _aux_stk[_top];        _top<span style="color: #333333">++</span>;                     _main_stk[_top] <span style="color: #333333">=</span> data;             _aux_stk[_top] <span style="color: #333333">=</span> temp;      }<span style="color: #888888">// pop operation: simply just _top--!</span><span style="color: #333399; font-weight: bold">int</span> minStack<span style="color: #333333">::</span>pop(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"underflow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }               _top<span style="color: #333333">--</span>;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" The element "</span> <span style="color: #333333">&lt;&lt;</span> _main_stk[_top<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span>] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is popped from the stack!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">// return min value</span><span style="color: #333399; font-weight: bold">int</span> minStack<span style="color: #333333">::</span>getMin(){        <span style="color: #008800; font-weight: bold">return</span> _aux_stk[_top];}<span style="color: #888888">// print the main stack</span><span style="color: #333399; font-weight: bold">void</span> minStack<span style="color: #333333">::</span>print_stk(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" nothing to print!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; j <span style="color: #333333">&lt;=</span> _top; j<span style="color: #333333">++</span>)                cout <span style="color: #333333">&lt;&lt;</span> _main_stk[j] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">// print the main stack</span><span style="color: #333399; font-weight: bold">void</span> minStack<span style="color: #333333">::</span>print_aux_stk(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" nothing to print!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; j <span style="color: #333333">&lt;=</span> _top; j<span style="color: #333333">++</span>)                cout <span style="color: #333333">&lt;&lt;</span> _aux_stk[j] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        minStack stack(<span style="color: #0000DD; font-weight: bold">10</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">12</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">3</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">4</span>);           stack.push(<span style="color: #0000DD; font-weight: bold">11</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">12</span>);        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.push(<span style="color: #0000DD; font-weight: bold">11</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">41</span>);        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.push(<span style="color: #0000DD; font-weight: bold">12</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">1</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">3</span>);                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.print_stk();        stack.print_aux_stk();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.pop();        stack.pop();        stack.pop();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.print_stk();        stack.print_aux_stk();        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/design-a-stack-which-in-addition-to-push-and-pop-also-has-a-function-min-which-returns-the-minimum-element-push-pop-and-min-should-all-operate-in-o1-time]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/design-a-stack-which-in-addition-to-push-and-pop-also-has-a-function-min-which-returns-the-minimum-element-push-pop-and-min-should-all-operate-in-o1-time#comments]]></comments><pubDate>Wed, 02 Sep 2015 15:19:35 GMT</pubDate><category><![CDATA[Uncategorized]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/design-a-stack-which-in-addition-to-push-and-pop-also-has-a-function-min-which-returns-the-minimum-element-push-pop-and-min-should-all-operate-in-o1-time</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/********** [...] ]]></description><content:encoded><![CDATA[<div><div id="938671520871360409" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">Design a stack which, in addition to push and pop, also has a function min which returns the minimum element? </span><span style="color: #888888">Push, pop and min should all operate in O(1) time.</span><span style="color: #888888">We design this class using array instead of linkedlist</span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (Sep. 2, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">minStack</span>{<span style="color: #997700; font-weight: bold">private:</span>        <span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span> _main_stk;      <span style="color: #888888">// main stack</span>        <span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">*</span> _aux_stk;       <span style="color: #888888">// aux. stack to keep elements in sorted</span>        <span style="color: #333399; font-weight: bold">int</span> _cap;        <span style="color: #888888">// the maximum capacity of stack</span>        <span style="color: #333399; font-weight: bold">int</span> _top;        <span style="color: #888888">// size of stack</span><span style="color: #997700; font-weight: bold">public:</span>        minStack(<span style="color: #333399; font-weight: bold">int</span> n);        <span style="color: #333333">~</span>minStack();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">push</span>(<span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">pop</span>();        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_empty</span>();        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_full</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_stk</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_aux_stk</span>();        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">getMin</span>();};minStack<span style="color: #333333">::</span>minStack(<span style="color: #333399; font-weight: bold">int</span> n){        _cap <span style="color: #333333">=</span> n;        _main_stk <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[_cap];        _aux_stk  <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[_cap];        _top <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;               cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" an object of minStack is created!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}minStack<span style="color: #333333">::~</span>minStack(){               <span style="color: #008800; font-weight: bold">delete</span>[] _main_stk;        <span style="color: #008800; font-weight: bold">delete</span>[] _aux_stk;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" an object of minStack is deleted!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">bool</span> minStack<span style="color: #333333">::</span>is_empty(){        <span style="color: #008800; font-weight: bold">return</span> (_top <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #333399; font-weight: bold">bool</span> minStack<span style="color: #333333">::</span>is_full(){        <span style="color: #008800; font-weight: bold">return</span> (_top <span style="color: #333333">==</span> _cap<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #333399; font-weight: bold">void</span> minStack<span style="color: #333333">::</span>push(<span style="color: #333399; font-weight: bold">int</span> data){        <span style="color: #008800; font-weight: bold">if</span> (is_full())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack overflow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" The element "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is pushed on the stack!"</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #888888">// if the stack is empty or data is less than all elements in the aux. stack, just push it</span>        <span style="color: #008800; font-weight: bold">if</span> ((_top <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>) <span style="color: #333333">||</span> (data <span style="color: #333333">&lt;=</span> _aux_stk[_top]))        {                _top<span style="color: #333333">++</span>;                             _main_stk[_top] <span style="color: #333333">=</span> data;                _aux_stk[_top]  <span style="color: #333333">=</span> data;                             <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #888888">// otherwise, push data on the main stack and duplicate the current min in the new position</span>        <span style="color: #333399; font-weight: bold">int</span> temp <span style="color: #333333">=</span> _aux_stk[_top];        _top<span style="color: #333333">++</span>;                     _main_stk[_top] <span style="color: #333333">=</span> data;             _aux_stk[_top] <span style="color: #333333">=</span> temp;      }<span style="color: #888888">// pop operation: simply just _top--!</span><span style="color: #333399; font-weight: bold">int</span> minStack<span style="color: #333333">::</span>pop(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"underflow!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }               _top<span style="color: #333333">--</span>;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" The element "</span> <span style="color: #333333">&lt;&lt;</span> _main_stk[_top<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span>] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is popped from the stack!"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">// return min value</span><span style="color: #333399; font-weight: bold">int</span> minStack<span style="color: #333333">::</span>getMin(){        <span style="color: #008800; font-weight: bold">return</span> _aux_stk[_top];}<span style="color: #888888">// print the main stack</span><span style="color: #333399; font-weight: bold">void</span> minStack<span style="color: #333333">::</span>print_stk(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" nothing to print!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; j <span style="color: #333333">&lt;=</span> _top; j<span style="color: #333333">++</span>)                cout <span style="color: #333333">&lt;&lt;</span> _main_stk[j] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">// print the main stack</span><span style="color: #333399; font-weight: bold">void</span> minStack<span style="color: #333333">::</span>print_aux_stk(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" nothing to print!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; j <span style="color: #333333">&lt;=</span> _top; j<span style="color: #333333">++</span>)                cout <span style="color: #333333">&lt;&lt;</span> _aux_stk[j] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">////////////////////////////////////////////////////////////</span><span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        minStack stack(<span style="color: #0000DD; font-weight: bold">10</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">12</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">3</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">4</span>);           stack.push(<span style="color: #0000DD; font-weight: bold">11</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">12</span>);        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.push(<span style="color: #0000DD; font-weight: bold">11</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">41</span>);        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.push(<span style="color: #0000DD; font-weight: bold">12</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">1</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">3</span>);                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.print_stk();        stack.print_aux_stk();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.pop();        stack.pop();        stack.pop();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the min value is : "</span> <span style="color: #333333">&lt;&lt;</span> stack.getMin() <span style="color: #333333">&lt;&lt;</span> endl;        stack.print_stk();        stack.print_aux_stk();        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[A class of K Stacks in a single array! Efficient memory and run time.: c++]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/a-class-of-k-stacks-in-a-single-array-efficient-memory-and-run-time-c]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/a-class-of-k-stacks-in-a-single-array-efficient-memory-and-run-time-c#comments]]></comments><pubDate>Wed, 02 Sep 2015 15:17:49 GMT</pubDate><category><![CDATA[Stack and Queue]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/a-class-of-k-stacks-in-a-single-array-efficient-memory-and-run-time-c</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166/* [...] ]]></description><content:encoded><![CDATA[<div><div id="958826404197753416" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">A class of K Stacks in a single array! Efficient memory and run time. </span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (Sep. 2, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">kStacks</span>{<span style="color: #997700; font-weight: bold">private:</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>arr;    <span style="color: #888888">// an array of size n to save the values in stacks</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>top;    <span style="color: #888888">// an array of size k to keep the index of top values of k stacks</span>        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #333333">*</span>next;   <span style="color: #888888">// an array of size n to keep the next entry in stacks</span>        <span style="color: #333399; font-weight: bold">int</span> n,k; <span style="color: #888888">// n: size of array, k: number of stacks</span>        <span style="color: #333399; font-weight: bold">int</span> next_slot;   <span style="color: #888888">// the next free slot in the array </span><span style="color: #997700; font-weight: bold">public:</span>        kStacks(<span style="color: #333399; font-weight: bold">int</span> k1, <span style="color: #333399; font-weight: bold">int</span> n1);        <span style="color: #333333">~</span>kStacks();                 <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_full</span>() { <span style="color: #008800; font-weight: bold">return</span> (next_slot <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_empty</span>(<span style="color: #333399; font-weight: bold">int</span> sn) { <span style="color: #008800; font-weight: bold">return</span>(top[sn] <span style="color: #333333">==</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>);}        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">push</span>(<span style="color: #333399; font-weight: bold">int</span> sn, <span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">pop</span>(<span style="color: #333399; font-weight: bold">int</span> sn);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_sn</span>(<span style="color: #333399; font-weight: bold">int</span> sn);};<span style="color: #888888">// constructor</span>kStacks<span style="color: #333333">::</span>kStacks(<span style="color: #333399; font-weight: bold">int</span> k1, <span style="color: #333399; font-weight: bold">int</span> n1){        k <span style="color: #333333">=</span> k1; n <span style="color: #333333">=</span> n1;         arr  <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[n];    <span style="color: #888888">// to keep the values in the stacks</span>        top  <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[k];    <span style="color: #888888">// to keep the last index of stacks</span>        next <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span>[n];    <span style="color: #888888">// to handle the next and previous index of stacks</span>        <span style="color: #888888">// initial to -1, all k stacks are empty</span>        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; i <span style="color: #333333">&lt;</span> k; i<span style="color: #333333">++</span>)                top[i] <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;        <span style="color: #888888">// the next slot of each entry is the next index</span>        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; i <span style="color: #333333">&lt;</span> n<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>; i<span style="color: #333333">++</span>)                next[i] <span style="color: #333333">=</span> i<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span>;        <span style="color: #888888">// for the last entry there is no free slot</span>        next[n<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>] <span style="color: #333333">=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;        <span style="color: #888888">// the initial free slot is arr[0]</span>        next_slot <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;}<span style="color: #888888">// destructor</span>kStacks<span style="color: #333333">::~</span>kStacks(){        k <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; n <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">delete</span>[] arr;        <span style="color: #008800; font-weight: bold">delete</span>[] next;         <span style="color: #008800; font-weight: bold">delete</span>[] top;} <span style="color: #888888">// push operator</span><span style="color: #333399; font-weight: bold">void</span> kStacks<span style="color: #333333">::</span>push(<span style="color: #333399; font-weight: bold">int</span> sn, <span style="color: #333399; font-weight: bold">int</span> data){        <span style="color: #888888">// wrong stack number</span>        <span style="color: #008800; font-weight: bold">if</span> (sn <span style="color: #333333">&gt;</span> k<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"sn must be in [0...k-1]"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #888888">// first check if the stack sn is overflow</span>        <span style="color: #008800; font-weight: bold">if</span> (is_full())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack overflow! cann't push!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #888888">// keep the next free slot in i, we will use this to push data in arr</span>        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> next_slot;        <span style="color: #888888">// update the next free slot using next[i]. next free slot will be used for next push operation</span>        next_slot <span style="color: #333333">=</span> next[i];        <span style="color: #888888">// now we use next in a different role, to keep the second top value in stack, </span>        next[i] <span style="color: #333333">=</span> top[sn];        <span style="color: #888888">// update the top by i</span>        top[sn] <span style="color: #333333">=</span> i;        <span style="color: #888888">// push the data in arr[i]</span>        arr[i] <span style="color: #333333">=</span> data;}<span style="color: #888888">// pop operator</span><span style="color: #333399; font-weight: bold">int</span> kStacks<span style="color: #333333">::</span>pop(<span style="color: #333399; font-weight: bold">int</span> sn){        <span style="color: #008800; font-weight: bold">if</span> (sn <span style="color: #333333">&gt;</span> k<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"sn must be in [0...k-1]"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }        <span style="color: #008800; font-weight: bold">if</span> (is_empty(sn))        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"stack underflow! cann't pup!"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span> INT_MAX;        }        <span style="color: #888888">// keep the current index of stack sn</span>        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> top[sn];        <span style="color: #888888">// keep the previous index of stack sn in top</span>        top[sn] <span style="color: #333333">=</span> next[i];        <span style="color: #888888">// initialize the next[i] by next free slot</span>        next[i] <span style="color: #333333">=</span> next_slot;        <span style="color: #888888">// next free slot is current i</span>        next_slot <span style="color: #333333">=</span> i;        <span style="color: #888888">// return current value</span>        <span style="color: #008800; font-weight: bold">return</span> arr[i];}<span style="color: #888888">// print the stack sn</span><span style="color: #333399; font-weight: bold">void</span> kStacks<span style="color: #333333">::</span>print_sn(<span style="color: #333399; font-weight: bold">int</span> sn){        <span style="color: #008800; font-weight: bold">if</span> (sn <span style="color: #333333">&gt;</span> k<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"sn must be in [0...k-1]"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">if</span> (is_empty(sn))        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"no value to print"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> top[sn];                <span style="color: #008800; font-weight: bold">while</span>(next[i] <span style="color: #333333">!=</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> arr[i] <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span>;                i <span style="color: #333333">=</span>  next[i];        }        <span style="color: #888888">// for the last stack value with next[i] = -1;</span>        cout <span style="color: #333333">&lt;&lt;</span> arr[i] <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        kStacks stack(<span style="color: #0000DD; font-weight: bold">3</span>, <span style="color: #0000DD; font-weight: bold">10</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">0</span>, <span style="color: #0000DD; font-weight: bold">1</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">0</span>, <span style="color: #0000DD; font-weight: bold">2</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">0</span>, <span style="color: #0000DD; font-weight: bold">3</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">1</span>, <span style="color: #0000DD; font-weight: bold">10</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">1</span>, <span style="color: #0000DD; font-weight: bold">20</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">2</span>, <span style="color: #0000DD; font-weight: bold">100</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">2</span>, <span style="color: #0000DD; font-weight: bold">200</span>);        stack.print_sn(<span style="color: #0000DD; font-weight: bold">0</span>);        stack.pop(<span style="color: #0000DD; font-weight: bold">0</span>);        stack.pop(<span style="color: #0000DD; font-weight: bold">0</span>);        stack.print_sn(<span style="color: #0000DD; font-weight: bold">0</span>);                stack.print_sn(<span style="color: #0000DD; font-weight: bold">1</span>);        stack.print_sn(<span style="color: #0000DD; font-weight: bold">2</span>);        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Stack class: c++ code: we assume that there is not stack overflow error!]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/stack-class-c-code]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/stack-class-c-code#comments]]></comments><pubDate>Tue, 01 Sep 2015 08:10:37 GMT</pubDate><category><![CDATA[Stack and Queue]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/stack-class-c-code</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130/************************************************************************************************************* [...] ]]></description><content:encoded><![CDATA[<div><div id="360768755628818992" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 3 Stack and Queue</span><span style="color: #888888">A simple class of Stack!</span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (Sep. 1, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #008800; font-weight: bold">struct</span> Node{        <span style="color: #333399; font-weight: bold">int</span> _data;        Node<span style="color: #333333">*</span> _next;};<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">myStack</span>{<span style="color: #997700; font-weight: bold">private:</span>        Node<span style="color: #333333">*</span> _head;        <span style="color: #333399; font-weight: bold">int</span> _size;<span style="color: #997700; font-weight: bold">public:</span>        myStack();        <span style="color: #333333">~</span>myStack();         <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">push</span>(<span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">pop</span>();        Node<span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">return_top</span>();        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">get_size</span>();        <span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_empty</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">clear</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">print_stack</span>();};<span style="color: #888888">// constructor</span>myStack<span style="color: #333333">::</span>myStack(){        _head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        _size <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;}myStack<span style="color: #333333">::~</span>myStack(){        clear();}<span style="color: #888888">// is empty function</span><span style="color: #333399; font-weight: bold">bool</span> myStack<span style="color: #333333">::</span>is_empty(){        <span style="color: #008800; font-weight: bold">return</span> (_size <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">0</span>);}<span style="color: #888888">// retrun the stack size</span><span style="color: #333399; font-weight: bold">int</span> myStack<span style="color: #333333">::</span>get_size(){        <span style="color: #008800; font-weight: bold">return</span> _size;}<span style="color: #888888">// puch on the top</span><span style="color: #333399; font-weight: bold">void</span> myStack<span style="color: #333333">::</span>push(<span style="color: #333399; font-weight: bold">int</span> data){        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node;        temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> data;        temp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> _head;        _head <span style="color: #333333">=</span> temp;        _size<span style="color: #333333">++</span>;}<span style="color: #888888">// pop the top data</span><span style="color: #333399; font-weight: bold">void</span> myStack<span style="color: #333333">::</span>pop(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())        {                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the stack is empty, poping faild"</span> <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #008800; font-weight: bold">return</span>;        }        Node<span style="color: #333333">*</span> temp  <span style="color: #333333">=</span> _head;        _head <span style="color: #333333">=</span> _head<span style="color: #333333">-&gt;</span>_next;        <span style="color: #008800; font-weight: bold">delete</span> temp;        _size<span style="color: #333333">--</span>;}<span style="color: #888888">// return the pointer of the top value</span>Node<span style="color: #333333">*</span> myStack<span style="color: #333333">::</span>return_top(){        <span style="color: #008800; font-weight: bold">if</span> (is_empty())                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">return</span> temp;}<span style="color: #888888">// empty the stack</span><span style="color: #333399; font-weight: bold">void</span> myStack<span style="color: #333333">::</span>clear(){        <span style="color: #008800; font-weight: bold">while</span>(_size)                pop();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" stack is deleted"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">// print the stack values</span><span style="color: #333399; font-weight: bold">void</span> myStack<span style="color: #333333">::</span>print_stack(){        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span>(temp <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>){                cout <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;        }        cout <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">int</span> _tmain(<span style="color: #333399; font-weight: bold">int</span> argc, _TCHAR<span style="color: #333333">*</span> argv[]){        myStack stack;          stack.push(<span style="color: #0000DD; font-weight: bold">1</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">2</span>);        stack.push(<span style="color: #0000DD; font-weight: bold">3</span>);        stack.print_stack();        cout <span style="color: #333333">&lt;&lt;</span> stack.get_size() <span style="color: #333333">&lt;&lt;</span> endl;        stack.pop();        stack.print_stack();        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> stack.return_top();        cout <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;}</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[Linked list class in c++]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/linked-list-class-in-c]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/linked-list-class-in-c#comments]]></comments><pubDate>Sun, 26 Jul 2015 11:08:44 GMT</pubDate><category><![CDATA[Linked list]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/linked-list-class-in-c</guid><description><![CDATA[A brief tutorial for linked list from the Stanford University.Linked list at Youtube:Tutorial:&nbsp;https://www.youtube.com/watch?v=U-MfAoL6qjMReverse a linked listDetect a loop in a linked listFind the junction node of two listsFind the mid point of a list by just one scanning  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;"><strong><a href="http://cslibrary.stanford.edu/103/LinkedListBasics.pdf" target="_blank" title=""><font size="4">A brief tutorial for linked list from the Stanford University.</font></a><br></strong><br><br><font size="4">Linked list at Youtube:</font><br><br><br><font size="4">Tutorial:&nbsp;</font><a href="https://www.youtube.com/watch?v=U-MfAoL6qjM" target="_blank"><font size="4">https://www.youtube.com/watch?v=U-MfAoL6qjM</font></a><br><br><a href="https://www.youtube.com/watch?v=sYcOK51hl-A" target="_blank"><font size="4">Reverse a linked list<br></font></a><br><a href="https://www.youtube.com/watch?v=hPod1R6Zlaw" target="_blank"><font size="4">Detect a loop in a linked list</font></a><br><br><a href="https://www.youtube.com/watch?v=gE0GopCq378" target="_blank"><font size="4">Find the junction node of two lists</font></a><br><br><a href="https://www.youtube.com/watch?v=ZpfmW0W8ivY" target="_blank"><font size="4">Find the mid point of a list by just one scanning</font></a><br><br></div><div><div id="531082478160074263" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 2 Linked List: a class and three interview questions</span><span style="color: #888888">By: Hamed Kiani (July 25, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include "stdafx.h"</span><span style="color: #557799">#include &lt;iostream&gt;</span><span style="color: #557799">#include &lt;cstring&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #888888">// a struct of linked list node, </span><span style="color: #888888">// contains two elements, _data: the value of node, _next: a pointer to the next node/NULL</span><span style="color: #008800; font-weight: bold">struct</span> Node{        <span style="color: #333399; font-weight: bold">int</span> _data;        Node<span style="color: #333333">*</span> _next;};<span style="color: #888888">// LinkedList class, </span><span style="color: #888888">// private: </span><span style="color: #888888">//         _head: keeps the head address (first node)</span><span style="color: #888888">//         _tail: keeps the tail address (last node)</span><span style="color: #888888">//         _size: the number of nodes in a linked list</span><span style="color: #888888">// public:</span><span style="color: #888888">//         LinkedList(): constructor</span><span style="color: #888888">//         ~LinkedList(): destructor</span><span style="color: #888888">//         size(): return the _size of the list</span><span style="color: #888888">//         addFront(data): add a node at begining/head of the list with _data = data </span><span style="color: #888888">//         addTail(data):  add a node at end/tail of the list with _data = data</span><span style="color: #888888">//         deleteFront():  delete the first node of the list</span><span style="color: #888888">//         deleteTail():   delete the tail node</span><span style="color: #888888">//         getHead():              return the address of list's head</span><span style="color: #888888">//         getTail():              return the address of list's tail</span><span style="color: #888888">//         insertAfter(position, data):    instert a node with data value after the position address</span><span style="color: #888888">//         deleteThisNode(position):               delete the node at the position address</span><span style="color: #888888">//         searchInList(data):                             find data in the list</span><span style="color: #888888">//         reverseList():                                  reverse the linked list</span><span style="color: #888888">//         addWithList(listTemp):                  sum the list with the listTemp</span><span style="color: #888888">//         appendTheList(listTemp):                append the listTemp to the end of the list</span><span style="color: #888888">//         printList():                                    print the list</span><span style="color: #888888">//         printListRecursion(temp):               recursion version of print list</span><span style="color: #888888">//         remove_deuplicate_fun_1():              remove duplicate values in the list</span><span style="color: #888888">//         nth_to_end_node(n):                             return the address of the n-th to end of the list</span><span style="color: #888888">// class declaration</span><span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">LinkedList</span>{<span style="color: #997700; font-weight: bold">private:</span>        Node<span style="color: #333333">*</span> _head;        Node<span style="color: #333333">*</span> _tail;        <span style="color: #333399; font-weight: bold">int</span> _size;<span style="color: #997700; font-weight: bold">public:</span>        LinkedList();        <span style="color: #333333">~</span>LinkedList();        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">size</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">addFront</span>(<span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">addTail</span>(<span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">deleteFront</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">deleteTail</span>();        Node<span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">getHead</span>();        Node<span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">getTail</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">insertAfter</span>(Node<span style="color: #333333">*</span> position, <span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">deleteThisNode</span>(Node<span style="color: #333333">*</span> position);        <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">searchInList</span>(<span style="color: #333399; font-weight: bold">int</span> data);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">reverseList</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">addWithList</span>(<span style="color: #008800; font-weight: bold">const</span> LinkedList <span style="color: #333333">*</span>listTemp);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">appendTheList</span>(<span style="color: #008800; font-weight: bold">const</span> LinkedList <span style="color: #333333">*</span>listTemp);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">printList</span>();        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">printListRecursion</span>(Node<span style="color: #333333">*</span> temp);        <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">remove_deuplicate_fun_1</span>();        Node<span style="color: #333333">*</span> <span style="color: #0066BB; font-weight: bold">nth_to_end_node</span>(<span style="color: #333399; font-weight: bold">int</span> n);};LinkedList<span style="color: #333333">::</span>LinkedList(){        _head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        _tail <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        _size <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;}LinkedList<span style="color: #333333">::~</span>LinkedList(){        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"**********DESTRUCTOR***********"</span> <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"**********DESTRUCTOR***********"</span> <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"**********DESTRUCTOR***********"</span> <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the destructor is called! deleting the linked list by calling deleteFront() function!"</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">while</span> (_size<span style="color: #333333">&gt;</span><span style="color: #0000DD; font-weight: bold">0</span>)                deleteFront();}<span style="color: #333399; font-weight: bold">int</span> LinkedList<span style="color: #333333">::</span>size(){        <span style="color: #008800; font-weight: bold">return</span> _size;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>addFront(<span style="color: #333399; font-weight: bold">int</span> data){                Node<span style="color: #333333">*</span> nodeTemp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node;        <span style="color: #008800; font-weight: bold">if</span> (_head <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                nodeTemp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> data;                _head <span style="color: #333333">=</span> nodeTemp;                _tail <span style="color: #333333">=</span> nodeTemp;                nodeTemp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;                _size <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">1</span>;        }        <span style="color: #008800; font-weight: bold">else</span>        {                nodeTemp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> data;                nodeTemp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> _head;                _head <span style="color: #333333">=</span> nodeTemp;                _size<span style="color: #333333">++</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value: "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is added to the front of list! "</span> <span style="color: #333333">&lt;&lt;</span>                <span style="background-color: #fff0f0">"current size is: "</span> <span style="color: #333333">&lt;&lt;</span> _size <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>addTail(<span style="color: #333399; font-weight: bold">int</span> data){        Node<span style="color: #333333">*</span> nodeTemp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node;        nodeTemp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> data;        nodeTemp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        <span style="color: #008800; font-weight: bold">if</span> (_tail <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                _tail<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> nodeTemp;                _tail <span style="color: #333333">=</span> nodeTemp;                _size<span style="color: #333333">++</span>;                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value: "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is added to the tail of list! "</span> <span style="color: #333333">&lt;&lt;</span>                        <span style="background-color: #fff0f0">"current size is: "</span> <span style="color: #333333">&lt;&lt;</span> _size <span style="color: #333333">&lt;&lt;</span> endl;        }        <span style="color: #008800; font-weight: bold">else</span>                addFront(data);}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>printList(){        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"***********************************"</span> <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"The list containts: "</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> _size;        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span> (i<span style="color: #333333">&gt;</span><span style="color: #0000DD; font-weight: bold">0</span>)        {                cout <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" "</span> ;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;                i<span style="color: #333333">--</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"***********************************"</span> <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>deleteFront(){        <span style="color: #008800; font-weight: bold">if</span> (_head <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)                <span style="color: #008800; font-weight: bold">return</span>;        <span style="color: #333399; font-weight: bold">int</span> data <span style="color: #333333">=</span> _head<span style="color: #333333">-&gt;</span>_data;        Node<span style="color: #333333">*</span> temp;        temp <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">if</span> (_head<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                _head <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;                _tail <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        }        <span style="color: #008800; font-weight: bold">else</span>                _head <span style="color: #333333">=</span> _head<span style="color: #333333">-&gt;</span>_next;        _size<span style="color: #333333">--</span>;        <span style="color: #008800; font-weight: bold">delete</span> temp;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value: "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is deleted from the front of list! "</span> <span style="color: #333333">&lt;&lt;</span>                <span style="background-color: #fff0f0">"current size is: "</span> <span style="color: #333333">&lt;&lt;</span> _size <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>deleteTail(){        <span style="color: #008800; font-weight: bold">if</span> (_tail <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)                <span style="color: #008800; font-weight: bold">return</span>;        <span style="color: #333399; font-weight: bold">int</span> data <span style="color: #333333">=</span> _tail<span style="color: #333333">-&gt;</span>_data;        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span> (temp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">!=</span> _tail)                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;        _tail <span style="color: #333333">=</span> temp;        temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;        _tail<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        <span style="color: #008800; font-weight: bold">delete</span> temp;        _size<span style="color: #333333">--</span>;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value: "</span> <span style="color: #333333">&lt;&lt;</span> data <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" is deleted from the tail of list! "</span> <span style="color: #333333">&lt;&lt;</span>                <span style="background-color: #fff0f0">"current size is: "</span> <span style="color: #333333">&lt;&lt;</span> _size <span style="color: #333333">&lt;&lt;</span> endl;}Node<span style="color: #333333">*</span> LinkedList<span style="color: #333333">::</span>getHead(){        <span style="color: #008800; font-weight: bold">return</span> _head;}Node<span style="color: #333333">*</span> LinkedList<span style="color: #333333">::</span>getTail(){        <span style="color: #008800; font-weight: bold">return</span> _tail;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>insertAfter(Node<span style="color: #333333">*</span> position, <span style="color: #333399; font-weight: bold">int</span> data){        <span style="color: #008800; font-weight: bold">if</span> (position <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                addFront(data);                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">if</span> (position<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                addTail(data);                <span style="color: #008800; font-weight: bold">return</span>;        }        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node;        temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> data;        temp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> position<span style="color: #333333">-&gt;</span>_next;        position<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> temp;        _size<span style="color: #333333">++</span>;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>deleteThisNode(Node<span style="color: #333333">*</span> position){        <span style="color: #008800; font-weight: bold">if</span> (position <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)                <span style="color: #008800; font-weight: bold">return</span>;        <span style="color: #008800; font-weight: bold">if</span> (position <span style="color: #333333">==</span> _head)        {                deleteFront();                <span style="color: #008800; font-weight: bold">return</span>;        }        <span style="color: #008800; font-weight: bold">if</span> (position<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)        {                deleteTail();                <span style="color: #008800; font-weight: bold">return</span>;        }        Node <span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span> (temp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">!=</span> position)                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;        temp<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> position<span style="color: #333333">-&gt;</span>_next;        <span style="color: #008800; font-weight: bold">delete</span> position;        _size<span style="color: #333333">--</span>;}<span style="color: #333399; font-weight: bold">int</span> LinkedList<span style="color: #333333">::</span>searchInList(<span style="color: #333399; font-weight: bold">int</span> data){        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> _head;        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">1</span>;        <span style="color: #008800; font-weight: bold">while</span> (temp <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                <span style="color: #008800; font-weight: bold">if</span> (temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">==</span> data) <span style="color: #008800; font-weight: bold">return</span> i;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;                i<span style="color: #333333">++</span>;        }        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>reverseList(){        <span style="color: #888888">// keep the next node to add to reverse list. </span>        Node<span style="color: #333333">*</span> next;        <span style="color: #888888">// keep the head of the reversed list so far.</span>        <span style="color: #888888">// at the first, the reversed list is empty - null</span>        Node<span style="color: #333333">*</span> prev <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        <span style="color: #888888">// a loop to traverse the list</span>        <span style="color: #008800; font-weight: bold">while</span> (_head)        {                next <span style="color: #333333">=</span> _head<span style="color: #333333">-&gt;</span>_next;                _head<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> prev;                prev <span style="color: #333333">=</span> _head;                _head <span style="color: #333333">=</span> next;        }        <span style="color: #888888">// at the end, the head of reversed list is pointed by prev</span>        _head <span style="color: #333333">=</span> prev;        <span style="color: #888888">// we need to update the tail too</span>        next <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span> (next<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)                next <span style="color: #333333">=</span> next<span style="color: #333333">-&gt;</span>_next;        _tail <span style="color: #333333">=</span> next;}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>addWithList(<span style="color: #008800; font-weight: bold">const</span> LinkedList <span style="color: #333333">*</span>listTemp){        <span style="color: #333399; font-weight: bold">int</span> s <span style="color: #333333">=</span> listTemp<span style="color: #333333">-&gt;</span>_size;        <span style="color: #008800; font-weight: bold">if</span> (s <span style="color: #333333">!=</span> _size)                <span style="color: #008800; font-weight: bold">return</span>;        Node<span style="color: #333333">*</span> t1 <span style="color: #333333">=</span> listTemp<span style="color: #333333">-&gt;</span>_head;        Node<span style="color: #333333">*</span> t2 <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span> (t1 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                t2<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">+=</span> t1<span style="color: #333333">-&gt;</span>_data;                t1 <span style="color: #333333">=</span> t1<span style="color: #333333">-&gt;</span>_next;                t2 <span style="color: #333333">=</span> t2<span style="color: #333333">-&gt;</span>_next;        }}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>appendTheList(<span style="color: #008800; font-weight: bold">const</span> LinkedList <span style="color: #333333">*</span>listTemp){        Node<span style="color: #333333">*</span> temp <span style="color: #333333">=</span> listTemp<span style="color: #333333">-&gt;</span>_head;        <span style="color: #008800; font-weight: bold">while</span> (temp <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                Node<span style="color: #333333">*</span> newNode <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Node;                newNode<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_data;                newNode<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;                _tail<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> newNode;                _tail <span style="color: #333333">=</span> _tail<span style="color: #333333">-&gt;</span>_next;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;                _size<span style="color: #333333">++</span>;        }}<span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>printListRecursion(Node<span style="color: #333333">*</span> temp){        <span style="color: #008800; font-weight: bold">if</span> (temp <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)                <span style="color: #008800; font-weight: bold">return</span>;        printListRecursion(temp<span style="color: #333333">-&gt;</span>_next);        cout <span style="color: #333333">&lt;&lt;</span> temp<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;}<span style="color: #888888">// inplace removing, O(n^2) time and O(1) additional space</span><span style="color: #333399; font-weight: bold">void</span> LinkedList<span style="color: #333333">::</span>remove_deuplicate_fun_1(){        Node <span style="color: #333333">*</span>head;        Node <span style="color: #333333">*</span>end;        head <span style="color: #333333">=</span> _head;        <span style="color: #333399; font-weight: bold">int</span> l <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>head)                <span style="color: #008800; font-weight: bold">return</span>;        end <span style="color: #333333">=</span> head<span style="color: #333333">-&gt;</span>_next;        Node <span style="color: #333333">*</span>c;        head <span style="color: #333333">=</span> head<span style="color: #333333">-&gt;</span>_next;        <span style="color: #008800; font-weight: bold">while</span> (head <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                c <span style="color: #333333">=</span> _head;                <span style="color: #008800; font-weight: bold">while</span> (c <span style="color: #333333">!=</span> end){                        <span style="color: #008800; font-weight: bold">if</span> (c<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">==</span> head<span style="color: #333333">-&gt;</span>_data)                                <span style="color: #008800; font-weight: bold">break</span>;                        <span style="color: #008800; font-weight: bold">else</span>                                c <span style="color: #333333">=</span> c<span style="color: #333333">-&gt;</span>_next;                }                <span style="color: #008800; font-weight: bold">if</span> (c <span style="color: #333333">==</span> end)                {                        end<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">=</span> head<span style="color: #333333">-&gt;</span>_data;                        end <span style="color: #333333">=</span> end<span style="color: #333333">-&gt;</span>_next;                }                head <span style="color: #333333">=</span> head<span style="color: #333333">-&gt;</span>_next;        }        head <span style="color: #333333">=</span> _head;        <span style="color: #008800; font-weight: bold">while</span> (head<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">!=</span> end)        {                l<span style="color: #333333">++</span>;                head <span style="color: #333333">=</span> head<span style="color: #333333">-&gt;</span>_next;                _tail <span style="color: #333333">=</span> head;        }        _size <span style="color: #333333">=</span> <span style="color: #333333">++</span>l;        head<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        _tail<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;}Node<span style="color: #333333">*</span> LinkedList<span style="color: #333333">::</span>nth_to_end_node(<span style="color: #333399; font-weight: bold">int</span> n){        Node <span style="color: #333333">*</span>p, <span style="color: #333333">*</span>q;        p <span style="color: #333333">=</span> _head;        q <span style="color: #333333">=</span> _head;        <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">while</span> (i <span style="color: #333333">&lt;</span> n <span style="color: #333333">&amp;</span> q <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                q <span style="color: #333333">=</span> q<span style="color: #333333">-&gt;</span>_next;                i<span style="color: #333333">++</span>;        }        <span style="color: #008800; font-weight: bold">if</span> (q <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        <span style="color: #008800; font-weight: bold">while</span> (q<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                q <span style="color: #333333">=</span> q<span style="color: #333333">-&gt;</span>_next;                p <span style="color: #333333">=</span> p<span style="color: #333333">-&gt;</span>_next;        }        <span style="color: #008800; font-weight: bold">return</span> p;}<span style="color: #888888">//         some useful functions and linked list questions</span><span style="color: #333399; font-weight: bold">int</span> pow(<span style="color: #333399; font-weight: bold">int</span> a, <span style="color: #333399; font-weight: bold">int</span> b){        <span style="color: #008800; font-weight: bold">if</span> (b <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">0</span>)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">1</span>;        <span style="color: #008800; font-weight: bold">return</span> a<span style="color: #333333">*</span><span style="color: #0066BB; font-weight: bold">pow</span>(a, b <span style="color: #333333">-</span> <span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">// time: O(N), additional space:O(N)</span><span style="color: #333399; font-weight: bold">void</span> add_two_lists(LinkedList<span style="color: #333333">*</span> l1, LinkedList<span style="color: #333333">*</span> l2, LinkedList<span style="color: #333333">*</span> l3){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>l1 <span style="color: #333333">&amp;</span> <span style="color: #333333">!</span>l2)                <span style="color: #008800; font-weight: bold">return</span>;        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>l1)                l3 <span style="color: #333333">=</span> l2;        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>l2)                l3 <span style="color: #333333">=</span> l1;        <span style="color: #888888">// convert the linked list to a decimal digit</span>        <span style="color: #333399; font-weight: bold">int</span> d1 <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #333399; font-weight: bold">int</span> d2 <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #333399; font-weight: bold">int</span> i  <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        Node<span style="color: #333333">*</span> temp1 <span style="color: #333333">=</span> l1<span style="color: #333333">-&gt;</span>getHead();        Node<span style="color: #333333">*</span> temp2 <span style="color: #333333">=</span> l2<span style="color: #333333">-&gt;</span>getHead();        <span style="color: #008800; font-weight: bold">while</span> (temp1 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                d1 <span style="color: #333333">+=</span> temp1<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">*</span> pow(<span style="color: #0000DD; font-weight: bold">10</span>, i);                temp1 <span style="color: #333333">=</span> temp1<span style="color: #333333">-&gt;</span>_next;                i<span style="color: #333333">++</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the digit of first link: "</span> <span style="color: #333333">&lt;&lt;</span> d1 <span style="color: #333333">&lt;&lt;</span> endl;        i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">while</span> (temp2 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                d2 <span style="color: #333333">+=</span> temp2<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">*</span> pow(<span style="color: #0000DD; font-weight: bold">10</span>, i);                temp2 <span style="color: #333333">=</span> temp2<span style="color: #333333">-&gt;</span>_next;                i<span style="color: #333333">++</span>;        }        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the digit of 2nd link: "</span> <span style="color: #333333">&lt;&lt;</span> d2 <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #333399; font-weight: bold">int</span> add <span style="color: #333333">=</span> d1 <span style="color: #333333">+</span> d2;        <span style="color: #008800; font-weight: bold">while</span> ((add) <span style="color: #333333">&gt;</span> <span style="color: #0000DD; font-weight: bold">0</span>)        {                l3<span style="color: #333333">-&gt;</span>addFront(add <span style="color: #333333">%</span> <span style="color: #0000DD; font-weight: bold">10</span>);                add <span style="color: #333333">/=</span> <span style="color: #0000DD; font-weight: bold">10</span>;        }}<span style="color: #888888">// without converting to decimal and re-digiting into the third linked list</span><span style="color: #333399; font-weight: bold">void</span> add_two_lists_2(LinkedList<span style="color: #333333">*</span> l1, LinkedList<span style="color: #333333">*</span> l2, LinkedList<span style="color: #333333">*</span> l3){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>l1 <span style="color: #333333">&amp;</span> <span style="color: #333333">!</span>l2)                <span style="color: #008800; font-weight: bold">return</span>;        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>l1)                l3 <span style="color: #333333">=</span> l2;        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>l2)                l3 <span style="color: #333333">=</span> l1;        <span style="color: #333399; font-weight: bold">int</span> carry <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        Node<span style="color: #333333">*</span> temp1 <span style="color: #333333">=</span> l1<span style="color: #333333">-&gt;</span>getHead();        Node<span style="color: #333333">*</span> temp2 <span style="color: #333333">=</span> l2<span style="color: #333333">-&gt;</span>getHead();        <span style="color: #008800; font-weight: bold">while</span> (temp1 <span style="color: #333333">||</span> temp2)        {                <span style="color: #333399; font-weight: bold">int</span> t1 <span style="color: #333333">=</span> (<span style="color: #333333">!</span>temp1) <span style="color: #333333">?</span> <span style="color: #0000DD; font-weight: bold">0</span> <span style="color: #333333">:</span> temp1<span style="color: #333333">-&gt;</span>_data;                <span style="color: #333399; font-weight: bold">int</span> t2 <span style="color: #333333">=</span> (<span style="color: #333333">!</span>temp2) <span style="color: #333333">?</span> <span style="color: #0000DD; font-weight: bold">0</span> <span style="color: #333333">:</span> temp2<span style="color: #333333">-&gt;</span>_data;                l3<span style="color: #333333">-&gt;</span>addFront((t1 <span style="color: #333333">+</span> t2 <span style="color: #333333">+</span> carry) <span style="color: #333333">%</span> <span style="color: #0000DD; font-weight: bold">10</span>);                carry <span style="color: #333333">=</span> (t1 <span style="color: #333333">+</span> t2 <span style="color: #333333">+</span> carry) <span style="color: #333333">/</span> <span style="color: #0000DD; font-weight: bold">10</span>;                <span style="color: #008800; font-weight: bold">if</span> (temp1 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)                        temp1 <span style="color: #333333">=</span> temp1<span style="color: #333333">-&gt;</span>_next;                <span style="color: #008800; font-weight: bold">if</span> (temp2 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)                        temp2 <span style="color: #333333">=</span> temp2<span style="color: #333333">-&gt;</span>_next;        }        <span style="color: #008800; font-weight: bold">if</span> (carry <span style="color: #333333">&gt;</span> <span style="color: #0000DD; font-weight: bold">0</span>)                l3<span style="color: #333333">-&gt;</span>addFront(<span style="color: #0000DD; font-weight: bold">1</span>);}<span style="color: #888888">//         find the joint node of two linked list: time O(N), memory: O(N)</span>Node<span style="color: #333333">*</span> find_joint_node_of_two_links(Node<span style="color: #333333">*</span> h1, Node<span style="color: #333333">*</span> h2){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>h1 <span style="color: #333333">|</span> <span style="color: #333333">!</span>h2)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        <span style="color: #333399; font-weight: bold">int</span> l1, l2;        l1 <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        l2 <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        Node <span style="color: #333333">*</span>temp <span style="color: #333333">=</span> h1;        <span style="color: #008800; font-weight: bold">while</span> (temp <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                l1<span style="color: #333333">++</span>;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;        }        temp <span style="color: #333333">=</span> h2;        <span style="color: #008800; font-weight: bold">while</span> (temp <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                l2<span style="color: #333333">++</span>;                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;        }        <span style="color: #333399; font-weight: bold">int</span> diff <span style="color: #333333">=</span> (l1 <span style="color: #333333">-</span> l2<span style="color: #333333">&gt;</span><span style="color: #0000DD; font-weight: bold">0</span>) <span style="color: #333333">?</span> (l1 <span style="color: #333333">-</span> l2) <span style="color: #333333">:</span> (l2 <span style="color: #333333">-</span> l1);        <span style="color: #008800; font-weight: bold">for</span> (<span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; i <span style="color: #333333">&lt;</span> diff; i<span style="color: #333333">++</span>)        {                <span style="color: #008800; font-weight: bold">if</span> (l1 <span style="color: #333333">&gt;</span> l2)                        h1 <span style="color: #333333">=</span> h1<span style="color: #333333">-&gt;</span>_next;                <span style="color: #008800; font-weight: bold">else</span>                        h2 <span style="color: #333333">=</span> h2<span style="color: #333333">-&gt;</span>_next;        }        <span style="color: #008800; font-weight: bold">while</span> (h1 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span> <span style="color: #333333">&amp;</span> h2 <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>){                <span style="color: #008800; font-weight: bold">if</span> (h1 <span style="color: #333333">==</span> h2)                        <span style="color: #008800; font-weight: bold">return</span> h1;                h1 <span style="color: #333333">=</span> h1<span style="color: #333333">-&gt;</span>_next;                h2 <span style="color: #333333">=</span> h2<span style="color: #333333">-&gt;</span>_next;        }        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;}<span style="color: #888888">// check if there is a loop in the linked list</span>Node<span style="color: #333333">*</span> is_loop_in_linkedlist(Node<span style="color: #333333">*</span> head){        Node<span style="color: #333333">*</span> slow <span style="color: #333333">=</span> head;      <span style="color: #888888">// moves one node</span>        Node<span style="color: #333333">*</span> fast <span style="color: #333333">=</span> head;      <span style="color: #888888">// moves two nodes</span>        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>head)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        <span style="color: #008800; font-weight: bold">while</span> (fast <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span> <span style="color: #333333">&amp;</span> fast<span style="color: #333333">-&gt;</span>_next <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span> <span style="color: #333333">&amp;</span> slow <span style="color: #333333">!=</span> <span style="color: #007020">NULL</span>)        {                slow <span style="color: #333333">=</span> slow<span style="color: #333333">-&gt;</span>_next;                fast <span style="color: #333333">=</span> fast<span style="color: #333333">-&gt;</span>_next<span style="color: #333333">-&gt;</span>_next;                <span style="color: #008800; font-weight: bold">if</span> (fast <span style="color: #333333">==</span> slow)                        <span style="color: #008800; font-weight: bold">return</span> slow;        }        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;}<span style="color: #888888">// check the video and description in the post for more details</span>Node<span style="color: #333333">*</span> find_the_first_node_of_loop(Node<span style="color: #333333">*</span> head){        Node<span style="color: #333333">*</span> meetPoint <span style="color: #333333">=</span> is_loop_in_linkedlist(head);        <span style="color: #008800; font-weight: bold">if</span> (meetPoint <span style="color: #333333">==</span> <span style="color: #007020">NULL</span>)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">NULL</span>;        Node<span style="color: #333333">*</span> temp;        temp <span style="color: #333333">=</span> head;        <span style="color: #008800; font-weight: bold">while</span> (temp <span style="color: #333333">!=</span> meetPoint)        {                temp <span style="color: #333333">=</span> temp<span style="color: #333333">-&gt;</span>_next;                meetPoint <span style="color: #333333">=</span> meetPoint<span style="color: #333333">-&gt;</span>_next;        }        <span style="color: #008800; font-weight: bold">return</span> temp;}<span style="color: #333399; font-weight: bold">void</span> main() {        <span style="color: #888888">// test the basics functions</span>        <span style="color: #888888">// a linked list object with five nodes</span>        LinkedList myList;        myList.addFront(<span style="color: #0000DD; font-weight: bold">5</span>);        myList.addFront(<span style="color: #0000DD; font-weight: bold">4</span>);        myList.addFront(<span style="color: #0000DD; font-weight: bold">3</span>);        myList.addFront(<span style="color: #0000DD; font-weight: bold">2</span>);        myList.addFront(<span style="color: #0000DD; font-weight: bold">1</span>);        <span style="color: #888888">// print the list</span>        myList.printList();        <span style="color: #888888">// myList.printListRecursion(myList.getHead());</span>        <span style="color: #888888">// add to the tail</span>        myList.addTail(<span style="color: #0000DD; font-weight: bold">1</span>);        myList.addTail(<span style="color: #0000DD; font-weight: bold">3</span>);        myList.addTail(<span style="color: #0000DD; font-weight: bold">3</span>);        <span style="color: #888888">// print the list</span>        myList.printList();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"delete from front"</span> <span style="color: #333333">&lt;&lt;</span> endl;        myList.deleteFront();        myList.deleteTail();        myList.printList();        <span style="color: #888888">// print the head and tail values</span>        Node<span style="color: #333333">*</span> tail <span style="color: #333333">=</span> myList.getTail();        Node<span style="color: #333333">*</span> head <span style="color: #333333">=</span> myList.getHead();        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value in the first node is: "</span> <span style="color: #333333">&lt;&lt;</span> head<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value in the last node is: "</span>  <span style="color: #333333">&lt;&lt;</span> tail<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #888888">// insert a node in the list</span>        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"insert a new value of "</span> <span style="color: #333333">&lt;&lt;</span> <span style="color: #0000DD; font-weight: bold">200</span> <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" in the third place: "</span> <span style="color: #333333">&lt;&lt;</span> endl;        Node<span style="color: #333333">*</span> position <span style="color: #333333">=</span> myList.getHead();        myList.insertAfter(position<span style="color: #333333">-&gt;</span>_next, <span style="color: #0000DD; font-weight: bold">200</span>);        myList.printList();                <span style="color: #888888">// delete a node from the list</span>        cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"delete the third node: "</span> <span style="color: #333333">&lt;&lt;</span> endl;        position <span style="color: #333333">=</span> myList.getHead();        myList.deleteThisNode(position<span style="color: #333333">-&gt;</span>_next<span style="color: #333333">-&gt;</span>_next);        myList.printList();        <span style="color: #888888">// search a value in the linked list</span>        <span style="color: #333399; font-weight: bold">int</span> pos <span style="color: #333333">=</span> myList.searchInList(<span style="color: #0000DD; font-weight: bold">10</span>);        (pos <span style="color: #333333">&lt;</span> <span style="color: #0000DD; font-weight: bold">0</span>) <span style="color: #333333">?</span> (cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value 10 is no found"</span> <span style="color: #333333">&lt;&lt;</span> endl) <span style="color: #333333">:</span> (cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value 10 is found at node : "</span> <span style="color: #333333">&lt;&lt;</span> pos <span style="color: #333333">&lt;&lt;</span> endl);        <span style="color: #888888">// reverse the list</span>        myList.reverseList();        myList.printList();        <span style="color: #888888">// a new linked list object</span>        LinkedList myList_2;        myList_2.addFront(<span style="color: #0000DD; font-weight: bold">7</span>);        myList_2.addFront(<span style="color: #0000DD; font-weight: bold">6</span>);        myList_2.addFront(<span style="color: #0000DD; font-weight: bold">5</span>);        myList_2.addFront(<span style="color: #0000DD; font-weight: bold">4</span>);        myList_2.addFront(<span style="color: #0000DD; font-weight: bold">4</span>);        myList_2.addFront(<span style="color: #0000DD; font-weight: bold">4</span>);        <span style="color: #888888">// adding the myList_2 to myList</span>        myList.addWithList(<span style="color: #333333">&amp;</span>myList_2);        myList.printList();        <span style="color: #888888">// appending two lists</span>        myList.appendTheList(<span style="color: #333333">&amp;</span>myList_2);        myList.printList();        <span style="color: #888888">// remove duplicates form myList</span>        myList.remove_deuplicate_fun_1();        myList.printList();        <span style="color: #888888">// return the n-th node to the end</span>        <span style="color: #333399; font-weight: bold">int</span> n <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">4</span>;        position <span style="color: #333333">=</span> myList.nth_to_end_node(n);        <span style="color: #008800; font-weight: bold">if</span> (position)                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" the "</span> <span style="color: #333333">&lt;&lt;</span> n <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">" to end element is :"</span> <span style="color: #333333">&lt;&lt;</span> position<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">else</span>                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"is longer the length of the list ! "</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #888888">// Problem1: adding the values of two linked lists</span>        LinkedList myList_3;        add_two_lists(<span style="color: #333333">&amp;</span>myList, <span style="color: #333333">&amp;</span>myList_2, <span style="color: #333333">&amp;</span>myList_3);        <span style="color: #888888">// add_two_lists_2(&amp;myList, &amp;myList_2, &amp;myList_3);</span>        myList_3.printList();        <span style="color: #888888">// Problem 2: find the junction of two lists</span>        Node n1, n2, n3, n4, n5, n6;        n1._data <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">1</span>;        n2._data <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">2</span>;        n3._data <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">3</span>;        n4._data <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">4</span>;        n5._data <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">5</span>;        n6._data <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">6</span>;        n1._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n2;        n2._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n3;        n3._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n4;        n4._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n5;        n5._next <span style="color: #333333">=</span> <span style="color: #007020">NULL</span>;        n6._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n4;        Node<span style="color: #333333">*</span> conj <span style="color: #333333">=</span> find_joint_node_of_two_links(<span style="color: #333333">&amp;</span>n1, <span style="color: #333333">&amp;</span>n6);        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>conj)                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"there is no conjuction  "</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">else</span>                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the conjuction node contains the value of : "</span> <span style="color: #333333">&lt;&lt;</span> conj<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;                <span style="color: #888888">// Problem 3: is there a cycle in the linked list</span>        n1._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n2;        n2._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n3;        n3._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n4;        n4._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n5;        n5._next <span style="color: #333333">=</span> <span style="color: #333333">&amp;</span>n2;        conj <span style="color: #333333">=</span> is_loop_in_linkedlist(<span style="color: #333333">&amp;</span>n1);        <span style="color: #008800; font-weight: bold">if</span> (conj)                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"there is a loop"</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">else</span>                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"there is not loop"</span> <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #888888">// Problem 4: if there is a cycle in the linked list, return the start node of the loop</span>                conj <span style="color: #333333">=</span> find_the_first_node_of_loop(<span style="color: #333333">&amp;</span>n1);        <span style="color: #008800; font-weight: bold">if</span> (conj)                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"the value of the start-loop node is : "</span> <span style="color: #333333">&lt;&lt;</span> conj<span style="color: #333333">-&gt;</span>_data <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #008800; font-weight: bold">else</span>                cout <span style="color: #333333">&lt;&lt;</span> <span style="background-color: #fff0f0">"there is not loop"</span> <span style="color: #333333">&lt;&lt;</span> endl;               }</pre></td></tr></table></div></div></div>]]></content:encoded></item><item><title><![CDATA[A method isSubstring which checks if one word is a substring of another, use this method to check if two strings are rotated.]]></title><link><![CDATA[http://www.hamedkiani.com/coding-interview/a-method-issubstring-which-checks-if-one-word-is-a-substring-of-another-use-this-method-to-check-if-two-strings-are-rotated]]></link><comments><![CDATA[http://www.hamedkiani.com/coding-interview/a-method-issubstring-which-checks-if-one-word-is-a-substring-of-another-use-this-method-to-check-if-two-strings-are-rotated#comments]]></comments><pubDate>Mon, 20 Jul 2015 15:44:10 GMT</pubDate><category><![CDATA[Arrays and Strings]]></category><guid isPermaLink="false">http://www.hamedkiani.com/coding-interview/a-method-issubstring-which-checks-if-one-word-is-a-substring-of-another-use-this-method-to-check-if-two-strings-are-rotated</guid><description><![CDATA[  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107/********************************************************************************************************************************************************************************** [...] ]]></description><content:encoded><![CDATA[<div><div id="493218159969403012" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #888888">/******************************************************************************************</span><span style="color: #888888">*******************************************************************************************</span><span style="color: #888888">Chapter 1 Arrays and Strings</span><span style="color: #888888"> * Assume you have a method isSubstring which checks if one word is a substring of another.</span><span style="color: #888888"> * Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using</span><span style="color: #888888"> * only one call to isSubstring (i.e., "waterbottle" is a rotation of "erbottlewat")</span><span style="color: #888888"> </span><span style="color: #888888">By: Hamed Kiani (July 20, 2015)</span><span style="color: #888888">******************************************************************************************</span><span style="color: #888888">******************************************************************************************/</span><span style="color: #557799">#include&lt;iostream&gt;</span><span style="color: #557799">#include&lt;cstring&gt;</span><span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;<span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_sub_string</span>(<span style="color: #008800; font-weight: bold">const</span> <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> s1, <span style="color: #008800; font-weight: bold">const</span> <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> s2){        <span style="color: #888888">// check if two strings are not null</span>        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>s1 <span style="color: #333333">||</span> <span style="color: #333333">!</span>s2)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;        <span style="color: #888888">// find the first char of s2, s2[0] in the s1, if found then check the rest of s2 over s1</span>        <span style="color: #333399; font-weight: bold">int</span> i,j ;        j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">for</span> (i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>; s1[i] <span style="color: #333333">!=</span> <span style="color: #0044DD">'\0'</span>; i<span style="color: #333333">++</span>)        {                <span style="color: #008800; font-weight: bold">if</span> (s1[i] <span style="color: #333333">==</span> s2[<span style="color: #0000DD; font-weight: bold">0</span>]) {                        <span style="color: #008800; font-weight: bold">for</span>(j <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">1</span>; s2[j] <span style="color: #333333">!=</span> <span style="color: #0044DD">'\0'</span>; j<span style="color: #333333">++</span>)                                                    <span style="color: #888888">// we are already sure that s1[i] == s2[0]</span>                                <span style="color: #008800; font-weight: bold">if</span> (s2[j] <span style="color: #333333">!=</span> s1[i<span style="color: #333333">+</span>j])                                        <span style="color: #008800; font-weight: bold">break</span>;                }                <span style="color: #888888">// if (s2[j] == '\0') means that we have similar chars till the end of s2</span>                <span style="color: #008800; font-weight: bold">if</span> (s2[j] <span style="color: #333333">==</span> <span style="color: #0044DD">'\0'</span>)                        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">true</span>;        }        <span style="color: #888888">// is not s sub string</span>        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;}<span style="color: #888888">// O(n) time, O(1) additional space.</span><span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_rotation_fun1</span>(<span style="color: #008800; font-weight: bold">const</span> <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> s1, <span style="color: #008800; font-weight: bold">const</span> <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> s2){        <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">!</span>s1 <span style="color: #333333">||</span> <span style="color: #333333">!</span>s2)                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;        <span style="color: #008800; font-weight: bold">if</span> (strlen(s1) <span style="color: #333333">!=</span> strlen(s2))                <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;        <span style="color: #008800; font-weight: bold">while</span>(<span style="color: #333333">*</span>s2 <span style="color: #333333">!=</span> <span style="color: #0044DD">'\0'</span>)                s2<span style="color: #333333">++</span>;        s2<span style="color: #333333">--</span>;        <span style="color: #008800; font-weight: bold">while</span>(<span style="color: #333333">*</span>s1 <span style="color: #333333">!=</span> <span style="color: #0044DD">'\0'</span>)        {                <span style="color: #008800; font-weight: bold">if</span> (<span style="color: #333333">*</span>s1 <span style="color: #333333">!=</span> <span style="color: #333333">*</span>s2)                        <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">false</span>;                s1<span style="color: #333333">++</span>; s2<span style="color: #333333">--</span>;        }               <span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">true</span>;}<span style="color: #888888">// O(n) time, O(n) additional space.</span><span style="color: #888888">// using is_rotation function</span><span style="color: #888888">// the trick is that we append the inverse of s1 to s1 and then check:</span><span style="color: #888888">// if is_sub_string(s1+inv(s1[1:length(s1)-1]), s2).</span><span style="color: #888888">// name + man = nameman</span><span style="color: #888888">// is is_sub_string(nameman, eman)</span><span style="color: #333399; font-weight: bold">bool</span> <span style="color: #0066BB; font-weight: bold">is_rotation_fun2</span>(  <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> s1,   <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> s2){        <span style="color: #333399; font-weight: bold">int</span> l1;        l1 <span style="color: #333333">=</span> strlen(s1);                    <span style="color: #333399; font-weight: bold">char</span><span style="color: #333333">*</span> temp <span style="color: #333333">=</span> (<span style="color: #333399; font-weight: bold">char</span> <span style="color: #333333">*</span>) malloc( <span style="color: #008800; font-weight: bold">sizeof</span>(<span style="color: #333399; font-weight: bold">char</span>) <span style="color: #333333">*</span> l1 <span style="color: #333333">*</span> <span style="color: #0000DD; font-weight: bold">2</span>);        <span style="color: #333399; font-weight: bold">char</span> <span style="color: #333333">*</span>t1 <span style="color: #333333">=</span> s1;           <span style="color: #333399; font-weight: bold">int</span> i <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;        <span style="color: #008800; font-weight: bold">while</span>(<span style="color: #333333">*</span>t1 <span style="color: #333333">!=</span> <span style="color: #0044DD">'\0'</span>)        {                temp[i] <span style="color: #333333">=</span> <span style="color: #333333">*</span>t1;                t1<span style="color: #333333">++</span>; i<span style="color: #333333">++</span>;        }        <span style="color: #888888">// move t1 pointin to end of s1 to next to last char. </span>        <span style="color: #888888">// accordingly, we change l1 = l1 -2, since we ignore the last char of s1 </span>        <span style="color: #888888">//and consider length counter as [0...strlen(s1)-1]</span>        t1 <span style="color: #333333">-=</span> <span style="color: #0000DD; font-weight: bold">2</span>; l1 <span style="color: #333333">-=</span> <span style="color: #0000DD; font-weight: bold">2</span>;        <span style="color: #008800; font-weight: bold">while</span>(l1<span style="color: #333333">&gt;=</span><span style="color: #0000DD; font-weight: bold">0</span>)        {                temp[i] <span style="color: #333333">=</span> s1[l1];                l1<span style="color: #333333">--</span>; i<span style="color: #333333">++</span>;        }               <span style="color: #333399; font-weight: bold">bool</span> check <span style="color: #333333">=</span> is_sub_string(temp, s2);        free(temp);        <span style="color: #008800; font-weight: bold">return</span> check;}<span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">main</span>() {        <span style="color: #333399; font-weight: bold">char</span> str1[] <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"check this string"</span>;        <span style="color: #333399; font-weight: bold">char</span> str2[] <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"this"</span>;        <span style="color: #333399; font-weight: bold">bool</span> check ;        check <span style="color: #333333">=</span> is_sub_string(str1,str2);        cout <span style="color: #333333">&lt;&lt;</span> check <span style="color: #333333">&lt;&lt;</span> endl;        <span style="color: #333399; font-weight: bold">char</span> str3[] <span style="color: #333333">=</span> <span style="background-color: #fff0f0">"siht"</span>;        check <span style="color: #333333">=</span> is_rotation_fun1(str3, str2);        cout <span style="color: #333333">&lt;&lt;</span> check <span style="color: #333333">&lt;&lt;</span> endl;        check <span style="color: #333333">=</span> is_rotation_fun2(str3, str2);        cout <span style="color: #333333">&lt;&lt;</span> check <span style="color: #333333">&lt;&lt;</span> endl;}</pre></td></tr></table></div></div></div>]]></content:encoded></item></channel></rss>