<jupyter_start><jupyter_text>StackStack is a linear data structure in which the elements are inserted and deleted from only one end.They follow a FILO (First in last out) or LIFO (last in first out) approach.  Let's see how we can implement the stack using array.<jupyter_code>class Stack:
    
    max_length = 0

    #initializing Stack with its parameters
    def __init__(self, maxlength):
        self.stack = []
        self.max_length = maxlength
        self.length = 0

    # Inserting new element in stack
    def push(self,value):
        if self.length < self.max_length:
            if self.length == 0:
                self.stack.append(value) 
                self.length += 1
                return self.stack
            self.stack.append(value)
            self.length += 1
            return self.stack
        else:
            print("Overflow")

    # Deleting top element from stack
    def pop(self):
        if self.length == 0:
            return 'Under Flow'
        deleted = self.stack.pop()
        self.length -= 1
        return deleted

    # Returning the top element of stack
    def peek(self):
        if self.length:
            print(self.stack[-1])
        else:
            print("Under Flow")

    def isEmpty(self):
        return self.length == 0
    
    def printStack(self):
        print(self.stack)
stack = Stack(3)            # ceating an object of stack
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)           # Getting Overflow condition
stack.printStack()      # Printing values of stack
stack.pop()
stack.printStack()
stack.pop()
stack.peek()
stack.printStack()
stack.pop()
stack.printStack() 
stack.pop()             # Getting underflow condition<jupyter_output>Overflow
[1, 2, 3]
[1, 2]
1
[1]
[]<jupyter_text>Now that we have already done the stack using array let's implement stack using a linked list.<jupyter_code>class Node():
    def __init__(self, value):
        self.value = value
        self.next = None


class Stack():
    def __init__(self):
        self.top = None
        self.length = 0

    def push(self, value):
        newNode = Node(value)
        if self.length == 0:
            self.top = newNode
            self.length += 1
        else:
            prevTop = self.top
            self.top = newNode
            newNode.next = prevTop
            self.length += 1

    def pop(self):
        if self.length == 0:
            print("Under Flow")
        
        else:
            delNode = self.top
            self.top = delNode.next
            self.length -= 1
            return delNode
    
    def peek(self):
        print(self.top.value)
    
    def isEmpty(self):
        return self.length == 0

    def printStack(self):
        curNode = self.top
        arr = []
       
        for _ in range(self.length):
            arr.append(curNode.value)
            curNode = curNode.next
        print(*arr)
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)           # Getting Overflow condition
stack.printStack()      # Printing values of stack
stack.pop()
stack.printStack()
stack.pop()
stack.peek()
stack.printStack()
stack.pop()
stack.printStack()<jupyter_output>4 3 2 1
3 2 1
2
2 1
1<jupyter_text>String reversalProgram to reverse a string.<jupyter_code>stack = Stack()
str = input("Enter a string : ")

for i in str:
    stack.push(i)

for i in range(stack.length):
    print(stack.pop().value, end="")<jupyter_output>Enter a string : Sandeep
peednaS<jupyter_text>Balanced Parantheses problemProgram to check if the parantheses in a given string are balanced or not.<jupyter_code>stack = Stack()

str = input("Enter the expression : ")

open_brackets = ['(', '[', '{']
close_brackets = [')', ']', '}']

for i in str: 
    if i in open_brackets : 
        stack.push(i)
    elif i in close_brackets :
        close_bracket = close_brackets.index(i)
        if stack.length > 0 and close_bracket == open_brackets.index(stack.top.value):
            stack.pop()
        else:
            break           # forget to add this else block
        
    #stack.printStack()


if stack.length == 0:
    print("Balanced")
else:
    print("Unbalanced")<jupyter_output>Enter the expression : { a + [ b - 0 ) }
{
{
{
{
{
{
[ {
[ {
[ {
[ {
[ {
[ {
[ {
[ {
Unbalanced
{'fraction_non_alphanumeric': 0.10957943925233644, 'fraction_numerical': 0.01074766355140187, 'mean_word_length': 2.1111918604651163, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '249931', 'license': 'MIT', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1315, 'n_words': 372, 'path': '10_Stack.ipynb', 'repo_name': 'Ks226/upgard_code', 'size': '4280'}