<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Iswarya</title>
    <description>The latest articles on DEV Community by Iswarya (@iswarya).</description>
    <link>https://dev.to/iswarya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F611114%2F62ba4870-4746-4a6a-a297-93a47d42267c.png</url>
      <title>DEV Community: Iswarya</title>
      <link>https://dev.to/iswarya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iswarya"/>
    <language>en</language>
    <item>
      <title>Stack Data Structure in Python</title>
      <dc:creator>Iswarya</dc:creator>
      <pubDate>Fri, 09 Apr 2021 06:35:41 +0000</pubDate>
      <link>https://dev.to/iswarya/stack-data-structure-in-python-1o2d</link>
      <guid>https://dev.to/iswarya/stack-data-structure-in-python-1o2d</guid>
      <description>&lt;p&gt;&lt;strong&gt;A great analogy we can utilize is stacking a heap of books. We tend to ceaselessly keep a shiny new book on top and take away the top most book&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a stack ?
&lt;/h2&gt;

&lt;p&gt;Stack is a linear data structure which stores items using &lt;code&gt;Last In, First Out(LIFO)&lt;/code&gt;strategy. Whenever a new element is added to a stack, it is added to the highest point of the stack, and the top element is taken out first from the stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack in Python can be executed in following ways:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;List&lt;/li&gt;
&lt;li&gt;collections.deque&lt;/li&gt;
&lt;li&gt;queue.LifoQueue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Some of the functions related with stack are:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;push():When this function is called, new element is added at the top of the stack&lt;/li&gt;
&lt;li&gt;pop():This function removes the top most element of the stack&lt;/li&gt;
&lt;li&gt;empty():Return True if the stack is empty else returns False&lt;/li&gt;
&lt;li&gt;peek(): This function returns the top element of the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation of Stack&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack():
    def __init__(self):
        self.items = []
    def push(self, item):
        self.items.append(item)
    def pop(self):
        return self.items.pop()
    def is_empty(self):
        return self.items == []
    def peek(self):
        if not self.is_empty():
            return self.items[-1]        
    def get_stack(self):
        return self.items
s=Stack()
print("Stack is Empty:",s.is_empty())
s.push("A")
s.push("B")
s.push("C")
s.push("D")
s.push("E")
s.push("F")
print("Stack after appending =",s.get_stack())
s.pop()
s.pop()
print("Stack after removing elements =",s.get_stack())
print("Peek element =",s.peek())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stack is Empty: True
Stack after appending = ['A', 'B', 'C', 'D', 'E', 'F']
Stack after removing elements = ['A', 'B', 'C', 'D']
Peek element = D 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>datastructure</category>
      <category>stack</category>
    </item>
  </channel>
</rss>
