<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amarnath Sharma on Medium]]></title>
        <description><![CDATA[Stories by Amarnath Sharma on Medium]]></description>
        <link>https://medium.com/@amarsharma1997?source=rss-2c0b4f43231a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_6wKZI2qYrjCTGL6</url>
            <title>Stories by Amarnath Sharma on Medium</title>
            <link>https://medium.com/@amarsharma1997?source=rss-2c0b4f43231a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 11:00:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amarsharma1997/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An Introduction to Graph]]></title>
            <link>https://medium.com/programming-club-nit-raipur/an-introduction-to-graph-1d09bc15839?source=rss-2c0b4f43231a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d09bc15839</guid>
            <category><![CDATA[nit-raipur]]></category>
            <category><![CDATA[graph-algorithms]]></category>
            <category><![CDATA[programming-club]]></category>
            <dc:creator><![CDATA[Amarnath Sharma]]></dc:creator>
            <pubDate>Wed, 02 Jan 2019 08:20:01 GMT</pubDate>
            <atom:updated>2019-01-02T08:20:01.515Z</atom:updated>
            <content:encoded><![CDATA[<p>Here we are not gonna discuss the definitions of graph and its algorithm we are just going to visualise what graph is and different ways to store the graph.</p><h4><strong>What is a graph?</strong></h4><p>It is totally different from the one we see in mathematics. <strong><em>Basically it is a way to represent the relations between a set of entities. This set of entities are called nodes and the relation between any two entities are called edge</em></strong>.</p><p>Let us see an example to get the actual <strong><em>visualization</em></strong> of graph. Everyone uses <strong><em>Facebook</em></strong>. Suppose there is a set of people <strong><em>Ankit</em></strong><em>, </em><strong><em>Abhishek</em></strong><em>, </em><strong><em>Aditya</em></strong><em>, </em><strong><em>Akshay</em></strong> who are on <strong><em>Facebook</em></strong>.</p><p>i) <strong>Ankit</strong> is a friend of <strong>Aditya</strong> and <strong>Akshay</strong>.</p><p>ii) <strong>Abhishek</strong> is a friend of <strong>Akshay</strong> only.</p><p>iii) <strong>Aditya</strong> is friend with <strong>Ankit</strong> only.</p><p>iv) <strong>Akshay</strong> is friend of <strong>Ankit</strong> and <strong>Abhishek</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*BZbzoCh5VR7IP2-SU3VOIg.png" /><figcaption>A Graphical Representation of the above friendship data</figcaption></figure><p>In the above figure the <strong><em>ovals labeled with the names are called nodes</em></strong> and the <strong><em>lines which represent the relation between two nodes are known as edges</em> </strong>and the above figure is called <strong><em>a</em></strong><em> </em><strong><em>graph</em>.</strong>This edges represent that two connecting nodes are friends with each other.</p><h3><strong>Storing a graph</strong></h3><p>Right now we are gonna discuss the <strong><em>two ways</em></strong> to store a graph.</p><ol><li>First we can maintain a table with size N*N where N is number of nodes in the graph. Where each cell of the table will be having either 1 if corresponding row and column are friends or 0 otherwise.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*uaRHHcGXk-p3lvIZqT5prw.png" /><figcaption>A Tabular representation of the above graph</figcaption></figure><p>As we can see that the above table is N*N square matrix and this representation is called <strong><em>Adjacency Matrix</em></strong> representation. Which has <strong><em>O(N²) space complexity</em></strong>. For this representation we use a <strong><em>2-D array of size N²</em></strong>.</p><p>2. We can maintain a single <strong><em>list</em></strong> for each <strong><em>person</em></strong> which will contain the name of their friends. So the representation would look like.</p><p><strong>Ankit -&gt; Aditya, Akshay</strong></p><p><strong>Aditya -&gt; Ankit</strong></p><p><strong>Akshay -&gt; Ankit, Abhishek</strong></p><p><strong>Abhishek -&gt; Akshay</strong></p><p>This way of representing a graph is called <strong><em>Adjacency List</em></strong> representation. It has <strong><em>O(N+E)</em></strong><em> </em><strong><em>space complexity</em></strong>. Where <strong>N</strong> represents number of nodes and E represents number of edges in the graph.</p><h3><strong>A comparison between these two ways of representation</strong></h3><p>As we can see that <strong><em>adjacency list representation</em></strong> requires <strong><em>less space</em></strong> as compared to <strong><em>adjacency matrix representation,</em></strong> but <strong>we</strong> <strong><em>can not say</em></strong> that <strong><em>adjacency list representation</em></strong> is the better than <strong><em>adjacency matrix representation</em></strong> because in adjacency matrix representation we use array which is easier to handle for insertion and deletion (if we have created a 2-D array of fixed size N*N we just need to put 0 or 1 in corresponding cell) but insertions and deletions are a bit complex in <strong><em>list</em></strong> as compared to <strong><em>array</em></strong> (for each insertion we will have to create a new list node and insert into the list). So it totally depends upon the type of graph whether it is <strong><em>sparse</em></strong> or <strong><em>dense</em></strong>.</p><p>A graph is called <strong><em>dense</em></strong> if the number of edges ( considering each pair of nodes can have max of only one edge between them ) in the graph are close to maximum number of edges in the graph otherwise it is <strong><em>sparse</em></strong>.</p><p>So if the graph is <strong><em>sparse</em></strong> then we can use <strong><em>adjacency list representation</em></strong> because in sparse graph we do not have too many edges so this requires less space to be stored. But if the graph is <strong><em>dense</em></strong> then we can use <strong><em>Adjacency matrix representation</em></strong> for convenience.</p><h3><strong>Directed and Undirected edge</strong></h3><p>There are multiple types of edges. Here we are gonna discuss these two types of edges.</p><p>If the edge has a <strong><em>direction</em></strong> then it is called <strong><em>directed edge</em></strong> otherwise it is called <strong><em>undirected</em> <em>edge</em></strong>.</p><p>If two people are friends means they became friends with the mutual consent. So we can not represent this relation in only one direction so we just do not give any direction to the representation and represent it like A — B. This edge is called undirected edge.</p><p><strong><em>Let us take an example, there are a set of people and we want to show who likes whom</em></strong>. Suppose there are <strong><em>two people A and B</em></strong> from the set. We only know that <strong><em>A likes B</em></strong>. So for representing this relation we can use a <strong><em>directed edge which will be having direction from A to B (A -&gt; B )</em></strong> denoting A likes B.</p><p><strong><em>Suppose if B does not like</em></strong> <strong><em>A</em></strong> then do not have to care about it. <strong><em>If B likes A then we will have to use one separate edge</em></strong> which will have direction from B to A ( B -&gt; A) denoting B likes A.</p><blockquote>We can think of an undirected edge as two directed edges.</blockquote><p>A — B can be thought as A -&gt; B and B -&gt; A. We will use this concept to store the graph.</p><p><strong>Note:- </strong>We use <strong>List</strong> or <strong>vector</strong> STL for adjacency list representation. We do not have to write code for insertion or deletion we just need to import the library and just some function calls. <a href="https://medium.com/programming-club-nit-raipur/stl-standard-template-library-5eace21acfc7"><strong>Read about vector</strong></a>. List is nothing but a linked list. We are going to use <strong><em>Vector</em> </strong>for adjacency list representation.</p><h4><strong>A Sample code for Adjacency List Representation:-</strong></h4><blockquote>Before proceeding to the code you should have some prior knowledge of vectors.</blockquote><pre>#<strong>include</strong> &lt;iostream&gt;<br>#<strong>include </strong>&lt;vector&gt;<br><strong>using</strong> <strong>namespace</strong> <strong>std</strong>;</pre><pre><strong>int</strong> main() {<br>    <strong>int</strong> N; // Number of nodes<br>    <strong>int</strong> E; // Number of edges<br>    <strong>int</strong> i,j,sz;<br>    <br>    <strong>cout</strong>&lt;&lt;&quot;Enter the number of nodes in the graph \n&quot;;<br>    <strong>cout</strong>&lt;&lt;&quot;Enter number of edges in the graph:- &quot;;    <br>    <strong>cin</strong>&gt;&gt;N&gt;&gt;E;</pre><pre>    <strong>vector</strong>&lt;<strong>int</strong> &gt; adj_list[N+1];<br>    // each index is a list for the ith node<br>    <br>    <strong>int</strong> a,b;<br>    <strong>cout</strong>&lt;&lt;&quot;Enter edges as two space separated integers\n&quot;;<br>    <strong>cout</strong>&lt;&lt;&quot;which represents they have an edge between them\n&quot;;</pre><pre>    <strong>for</strong>(i=0;i&lt;<strong>E</strong>;i++) {<br>        <strong>cin</strong>&gt;&gt;a&gt;&gt;b;<br>        // if edge is undirected then we will need to put b in <br>        // a&#39;s list and a in b&#39;s list so we will follow below code<br>        <strong>adj_list</strong>[a].<strong>push_back</strong>(b);<br>        <strong>adj_list</strong>[b].<strong>push_back</strong>(a);<br>        // If the edge is directed as a-&gt;b then we will only need<br>        // to put b in a&#39;s list and we will follow below code<br>        // adj_list[a].push_back(b);<br>        // We consider the edges to be undirected<br>    }</pre><pre>    <strong>cout</strong>&lt;&lt;<strong>endl</strong>&lt;&lt;&quot;In this graph&quot;&lt;&lt;<strong>endl</strong>;<br>    <strong>for</strong>(i=1;i&lt;=<strong>N</strong>;i++) {<br>        sz=<strong>adj_list</strong>[i].<strong>size</strong>();<br>        <strong>cout</strong>&lt;&lt;i&lt;&lt;&quot; is connected to &quot;;<br>        <strong>for</strong>(j=0;j&lt;sz;j++){<br>            <strong>cout</strong>&lt;&lt;<strong>adj_list</strong>[i][j]&lt;&lt;&quot; &quot;;<br>        }<br>        <strong>cout</strong>&lt;&lt;<strong>endl</strong>;<br>    }<br>    <strong>return</strong> 0;<br>}</pre><pre><strong>Output:-<br></strong>Enter the number of nodes in the graph 4 3<br>Enter number of edges in the graph:- Enter edges as two space separated integers<br>which represents they have an edge between them<br>1 2<br>1 3<br>4 3</pre><pre>In this graph<br>1 is connected to 2 3 <br>2 is connected to 1 <br>3 is connected to 1 4 <br>4 is connected to 3</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d09bc15839" width="1" height="1" alt=""><hr><p><a href="https://medium.com/programming-club-nit-raipur/an-introduction-to-graph-1d09bc15839">An Introduction to Graph</a> was originally published in <a href="https://medium.com/programming-club-nit-raipur">Programming Club, NIT Raipur</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[STL — Standard Template Library]]></title>
            <link>https://medium.com/programming-club-nit-raipur/stl-standard-template-library-5eace21acfc7?source=rss-2c0b4f43231a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5eace21acfc7</guid>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-club]]></category>
            <category><![CDATA[stl]]></category>
            <category><![CDATA[nit-raipur]]></category>
            <dc:creator><![CDATA[Amarnath Sharma]]></dc:creator>
            <pubDate>Thu, 27 Sep 2018 05:59:53 GMT</pubDate>
            <atom:updated>2018-09-27T05:59:54.040Z</atom:updated>
            <content:encoded><![CDATA[<p>Here we are going to discuss about some STLs that are frequently used in competitive programming. Like string, vector, pair, queue, stack, set and map.</p><h3><strong>What is STL ?</strong></h3><p><strong>STL (Standard Template Library): </strong>STL provides a set of common classes for C++, such as containers ( Queue , Stack ) and associative arrays ( Hash tables ) , that can be used with any built-in data-type and with any user-defined data-type that supports some elementary operations (such as copying and assignment).</p><p>All STLs provide dynamic memory allocation. Vector and string have contiguous memory allocation and the rest have non-contiguous memory allocation.</p><p>So now we should see each STL one by one:-</p><h3><strong>Vector:-</strong></h3><p>This is just like an array but it has dynamic and contiguous memory allocation. This comes under the STL library vector.</p><p><strong>Library to be included:<em>-</em> </strong>vector (#include&lt;vector&gt;)</p><p><strong>Declaration of a vector:-</strong></p><p><strong><em>vector&lt; data-type &gt; vector-name;</em></strong></p><p>here data-type can be any <strong><em>in-built data type</em></strong> ( char, int ,float, double ) or any <strong><em>user-defined data type</em></strong> ( classes , structures ).</p><p><strong>The difference and similarity between vector and array:-</strong></p><p>Declaration of an array which can store 8 integers will go as <br><strong>int arr[8];</strong></p><p>and if we want to use a vector instead of array then we just need to write<br><strong>vector&lt; int &gt; v; ( specifying the size is optional )</strong></p><p><strong>Specifying the size while declaration of vector:-</strong></p><p>vector&lt;int &gt; v(8);<br><strong>( not specifying the size will also be fine )</strong></p><p>vector&lt;int &gt; v;<br><strong>( This declaration does not mean that this vector v is going to store only one integer. This is a list of integer which has a contiguous memory allocation </strong>).</p><p>We use <strong>push_back</strong> function for inserting any element at the end of vector.</p><p>suppose we wrote the following code</p><pre><strong>vector</strong>&lt;<strong>int</strong>&gt; v;</pre><pre><strong>for</strong>(<strong>int</strong> i=0;i&lt;8;i++){<br>    arr[i]=i;<br>    v.<strong>push_back</strong>(i);<br>}</pre><p><strong>Now this vector v has 8 integers 0,1,2,3,4,5,6,7.</strong></p><p><strong>Difference:</strong></p><p>The difference between vector and array is that the size in vector is not fixed and it can be changed <strong>dynamically</strong> because vector has dynamic memory allocation while array has static memory allocation. We do not have to specify anything about the size while declaration.</p><p><strong>Similarity:</strong></p><p>The similarity between vector and array is that they both have contiguous memory allocation. The advantage of having contiguous memory allocation is that we can access any element in<strong> O(1) time complexity</strong> by mentioning the index of element between the square brackets with the vector-name.</p><p>Suppose we want to access 5th element in array then we will write arr[4] and this will be same in vector v[4](because of <strong>contiguous</strong> memory allocation). both vector and array will output 4 because of the above code. <strong>But we can not access in the same way in </strong><a href="https://www.geeksforgeeks.org/list-cpp-stl/"><strong><em>list</em></strong></a><strong> because it has <em>non-contiguous memory allocation</em>. It is basically a linked list and the time complexities for all the operations are same as linked list data structure.</strong></p><p>We can do all the operations on vectors in the same way as we do in arrays.</p><p>Some<strong> member functions</strong> which are frequently used in vector are:-</p><ol><li><strong>push_back</strong>(arg):- This function pushes the element arg at the end of the vector.</li><li><strong>pop_back</strong>():- This function pops an element from the end of the vector.</li><li><strong>back</strong>():- This function returns the last element of the vector.</li><li><strong>front</strong>():- This function returns the first element of the vector.</li><li><strong>clear</strong>():- This function clears the vector.</li><li><strong>size</strong>():- This returns the total number of elements which are stored in the vector.</li><li><strong>begin</strong>():- This returns an iterator pointing to the first element of the vector.</li><li><strong>end</strong>():- This returns an iterator pointing to <strong>theoretical element that follows the last element</strong>.</li></ol><p><strong>All of the above functions have time complexities O(1)</strong>.</p><p><strong>Example:-</strong></p><pre>#<strong>include</strong>&lt;iostream&gt;<br>#<strong>include</strong>&lt;vector&gt;<br><strong>using</strong> <strong>namespace</strong> <strong>std</strong>;</pre><pre><strong>int</strong> main() {<br>    <strong>vector</strong>&lt;int &gt; v;<br>    int i;<br> <br>    <strong>for</strong>(i=0;i&lt;8;i++) {<br>        v.<strong>push_back</strong>(i);<br>    }</pre><pre>    <strong>cout</strong>&lt;&lt;”First element is:- “&lt;&lt;v.front()&lt;&lt;endl;<br>    <strong>cout</strong>&lt;&lt;”Last element is:- “&lt;&lt;v.back()&lt;&lt;endl;</pre><pre>    <strong>cout</strong>&lt;&lt;”Elements in the vector are:- “&lt;&lt;endl;<br> <br>    <strong>for</strong>(i=0;i&lt;v.size();i++) {<br>        cout&lt;&lt;v[i]&lt;&lt;endl;<br>    }</pre><pre>    v.<strong>pop_back</strong>();<br>    cout&lt;&lt;”Last element after pop_back:- “&lt;&lt;v.back()&lt;&lt;endl;</pre><pre>    v.<strong>clear</strong>();<br>    cout&lt;&lt;”Size of vector after clearing the vector:- “&lt;&lt;v.size();</pre><pre>    <strong>return</strong> 0;<br>}</pre><pre><strong>Output of the above program:-</strong></pre><pre>First element is:- 0<br>Last element is:- 7<br>Elements in the vector are:- <br>0<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>Last element after pop_back:- 6<br>Size of vector after clearing the vector:- 0</pre><p><strong>How does a vector do dynamic and contiguous allocation?:-</strong></p><p>When a vector is declared a block of memory is assigned to it with a fixed capacity (the maximum number of elements to be stored in that block ). As a new element is added in the vector and the size of the vector goes more than the capacity then a new block of memory is assigned with more capacity and the previous block is copied to that new block of memory and the new element is added just after that copied block and the previous block of memory is freed. This is how it maintains above two properties.</p><h3><strong>String:-</strong></h3><p>A sequence of character is called String. This also has contiguous and dynamic memory allocation and works in the same way as vector do for dynamic and contiguous memory allocation.</p><p><strong>Library to be included:- </strong>string (#include&lt;string&gt;)</p><p><strong>Declaration of a String:-</strong></p><p><strong>string string-name;</strong></p><p>How do we declare a character array in C:-</p><pre><strong>char</strong> str[N+1]; ( Here N is the number of characters in the string and extra 1 for null character )</pre><p>but if want to use a STL string then we will declare it as.</p><pre><strong>string</strong> str; ( Here we do not need to specify the size )</pre><p>Now we can use this same as the character array but we can not use any function from string.h in c because str is an object of class string while str in character array declaration is a pointer.</p><p>For string objects comparison, assignment and concatenation is very easy and we do not have to write any function for that we can compare them in the same way we compare integers.</p><pre><strong>string</strong> s1=”abc”,s2=”cde”;</pre><pre>s1&lt;s2 , s1==s2 , s2≥s1 any comparison will work fine but with same <strong>string</strong> object.</pre><pre>s1+=s2; ( This concatenation will make s1=”abccde” ).</pre><p>Here are some of the <strong>member functions</strong> of string:-</p><ol><li><strong>length</strong>():- This returns the size of the string.</li><li><strong>clear</strong>():- clears the string</li><li><strong>substr</strong>(pos, len):- Returns a substring starting from pos( 0-indexed ) with size len. If the requested substring extends past the end of the string, the returned substring is [pos, <a href="https://en.cppreference.com/w/cpp/string/basic_string/size">size()</a>).</li></ol><p><strong>Example code:-</strong></p><pre>#<strong>include</strong>&lt;iostream&gt;<br>#<strong>include</strong>&lt;string&gt;</pre><pre><strong>using</strong> <strong>namespace</strong> <strong>std</strong>;</pre><pre><strong>int</strong> main() {<br>    <strong>string</strong> s=”abcdefghijklmnopqrstuvwxyz”;<br> <br>    <strong>cout</strong>&lt;&lt;”Length of the String:- “&lt;&lt;s.length()&lt;&lt;endl;<br>    <strong>cout</strong>&lt;&lt;”Characters in the String:- “;<br> <br>    <strong>for</strong>(<strong>int</strong> i=0;i&lt;s.length();i++) {<br>       cout&lt;&lt;s[i]&lt;&lt;’ ‘;<br>    }<br> <br>    <strong>cout</strong>&lt;&lt;endl;<br> <br>    <strong>cout</strong>&lt;&lt;”Substring is:- “&lt;&lt;s.substr(23,3)&lt;&lt;endl;<br> <br>    s.clear();<br>    <strong>cout</strong>&lt;&lt;s.length();</pre><pre>    <strong>return</strong> 0;<br>}</pre><pre><strong>Output of the above program:-</strong></pre><pre>Length of the String:- 26<br>Characters in the String:- a b c d e f g h i j k l m n o p q r s t u v w x y z <br>Substring is:- xyz</pre><h3><strong>Queue</strong></h3><p>Queue is a linear data structure which follows <strong>FIFO ( First in first out )</strong>. That the elements will be inserted at the end and will be removed from the front.</p><p><strong>Push and pop in queue:-</strong> <strong><em>Push</em></strong><em> means inserting an element and </em><strong><em>Pop</em></strong><em> means removing an element.</em></p><p>It has <strong>dynamic </strong>but <strong>non-contiguous memory</strong> allocation.</p><p><strong>Library to be included:- </strong>queue(#include&lt;queue&gt;)</p><p><strong>Declaration of a Queue:-<br>queue&lt;data-type&gt; queue-name;</strong></p><p>(<strong> </strong>Here data type can be any <strong><em>inbuilt</em></strong> or <strong><em>user defined</em></strong> datatype )</p><p>Here are some <strong>member functions</strong> of queue stl:-</p><ol><li><strong>size</strong>():- returns <strong><em>number of elements</em></strong> in the queue.</li><li><strong>front</strong>():- returns the <strong><em>first</em></strong> element in the queue.</li><li><strong>push</strong>(arg):- <strong><em>pushes arg</em></strong> in the queue.</li><li><strong>pop</strong>():- <strong><em>pops the first element</em></strong> from the queue and <strong>does not return anything</strong>.</li><li><strong>empty</strong>():- returns <strong><em>true</em></strong> if the queue is empty else <strong><em>false</em></strong>.</li><li><strong>back</strong>():- returns the <strong><em>last</em></strong> element of the queue.</li></ol><p><strong>Example code:-</strong></p><pre>#<strong>include</strong>&lt;iostream&gt;<br>#<strong>include</strong>&lt;queue&gt;<br><strong>using</strong> <strong>namespace</strong> <strong>std</strong>;</pre><pre><strong>int</strong> main() {<br>    <strong>queue</strong>&lt;int&gt; q;</pre><pre>    <strong>for</strong>(<strong>int</strong> i=0;i&lt;4;i++) {<br>        q.push(i);<br>    }</pre><pre>    <strong>cout</strong>&lt;&lt;&quot;Size of the queue is:- &quot;&lt;&lt;q.size()&lt;&lt;endl;</pre><pre>    <strong>while</strong>(!q.empty()) {<br>        <strong>cout</strong>&lt;&lt;&quot;First element in the queue:- &quot;&lt;&lt;q.front()&lt;&lt;endl;<br>        <strong>cout</strong>&lt;&lt;&quot;Last element in the queue:- &quot;&lt;&lt;q.back()&lt;&lt;endl;</pre><pre>        q.<strong>pop</strong>();</pre><pre>        <strong>cout</strong>&lt;&lt;&quot;Element popped&quot;&lt;&lt;endl;<br>    }</pre><pre>    <strong>return</strong> 0;<br>}</pre><pre><strong>Output of the above program:-</strong></pre><pre>Size of the queue is:- 4<br>First element in the queue:- 0<br>Last element in the queue:- 3<br>Element popped<br>First element in the queue:- 1<br>Last element in the queue:- 3<br>Element popped<br>First element in the queue:- 2<br>Last element in the queue:- 3<br>Element popped<br>First element in the queue:- 3<br>Last element in the queue:- 3<br>Element popped</pre><h3><strong>Stack:-</strong></h3><p>Stack is a data structure which follows<strong> LIFO ( Last in First out )</strong>. That the elements will be inserted at the end and will be removed from the end.</p><p>It has <strong>dynamic </strong>but <strong>non-contiguous memory</strong> allocation.</p><p><strong>Library to be included:- </strong>stack(#include&lt;stack&gt;)</p><p><strong>Declaration of a Stack:-<br>stack&lt;data-type&gt; stack-name;</strong></p><p><strong>( </strong>Here data type can be any <strong><em>inbuilt</em></strong> or <strong><em>user defined</em></strong> datatype )</p><p>Here are some <strong>member functions</strong> of queue stl:-</p><ol><li><strong>size</strong>():- returns <strong><em>number of elements</em></strong> in the stack.</li><li><strong>push</strong>(arg):- <strong><em>pushes arg</em></strong> in the stack.</li><li><strong>pop</strong>():- <strong><em>pops the first element</em></strong> from the stack and <strong>does not return anything</strong>.</li><li><strong>empty</strong>():- returns <strong><em>true</em></strong> if the stack is empty else <strong><em>false</em></strong>.</li><li><strong>top</strong>():- returns the element which is at the<strong> top </strong>of the stack<strong>.</strong></li></ol><p><strong>Example code:-</strong></p><pre>#<strong>include</strong>&lt;iostream&gt;<br>#<strong>include</strong>&lt;stack&gt; <br><strong>using</strong> <strong>namespace</strong> <strong>std</strong>;</pre><pre><strong>int</strong> main() {<br>    <strong>stack</strong>&lt;int &gt; st;<br>    <strong>for</strong>(<strong>int</strong> i=0;i&lt;4;i++) {<br>        st.<strong>push</strong>(i);<br>    }</pre><pre>    <strong>cout</strong>&lt;&lt;&quot;Size of the stack is:- &quot;&lt;&lt;st.size()&lt;&lt;endl;</pre><pre>    <strong>while</strong>(st.empty()==false) {<br>        <strong>cout</strong>&lt;&lt;&quot;Element at the top of the stack:- &quot;&lt;&lt;st.top()&lt;&lt;end;<br>        st.<strong>pop</strong>();</pre><pre>        <strong>cout</strong>&lt;&lt;&quot;Element popped&quot;&lt;&lt;endl;<br>    }</pre><pre>    <strong>return</strong> 0;<br>}</pre><pre><strong><br>Output of the above program:-</strong></pre><pre>Size of the stack is:- 4<br>Element at the top of the stack:- 3<br>Element popped<br>Element at the top of the stack:- 2<br>Element popped<br>Element at the top of the stack:- 1<br>Element popped<br>Element at the top of the stack:- 0<br>Element popped</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5eace21acfc7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/programming-club-nit-raipur/stl-standard-template-library-5eace21acfc7">STL — Standard Template Library</a> was originally published in <a href="https://medium.com/programming-club-nit-raipur">Programming Club, NIT Raipur</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>