<jupyter_start><jupyter_text>Stacks and QueuesBags, stacks, and queues are fundamental data structures that hold a collection of objects.They have operations to insert, remove, iterate, and test if they're empty. Stacks and queues differ on how items are inserted and removed.- **Stack** uses LIFO (last in first out), where you examine the item most recently added    - add new items: *push*    - remove items: *pop*- **Queue** uses FIFO (first in first out), where you examine the item least recently added    - add new items: *enqueue*    - remove items: *dequeue*Modular programming is the idea to completely separate the interface (the client) and the details of an implementation. The client has many implementations from which to choose, but only performs basic operations. Alternatively, the implementation doesn't know the client's needs, it only implements operations:- Client: program using operations defined in the interface- Implementation: the actual code implementing the operations- Interface: description of data type, basic operations  StacksUse a linked list to implement a stack. A **linked list** is group of nodes, where each node has the item and a reference (pointer) to the next node. New items become the first item in the list and pop operations remove the current first item.For a pop operation, save the first node's item, update your first pointer to point to the next item (the second node), then delete the first node.For a push operation, save a link to the existing first node in a variable, create a new node where the item is the new item and the pointer to the next node is the saved link.<jupyter_code>class Node(object):
    def __init__(self, item=None, next_node=None):
        self.item = item
        self.next_node = next_node

    def get_item(self):
        return self.item

    def get_next(self):
        return self.next_node

    def set_next(self, new_next):
        self.next_node = new_next

class LinkedStackOfStrings(object):
    def __init__(self, head=None):
        self.head = head
    
    def push(self, s):
        first = Node(s)
        first.set_next(self.head)
        self.head = first
    
    def pop(self):
        item = self.head.item
        self.head = self.head.get_next()
        return item
    
    def size(self):
        current = self.head
        count = 0
        while current:
            count += 1
            current = current.get_next()
        return count
    
    def isEmpty(self):
        return self.size() == 0
linked_list = LinkedStackOfStrings()
linked_list.isEmpty()
linked_list.push('to')
linked_list.push('be')
linked_list.push('or')
linked_list.push('not')
linked_list.pop()
linked_list.size()
linked_list.pop()
linked_list.isEmpty()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07589447054571738, 'fraction_numerical': 0.0010842067220816769, 'mean_word_length': 3.4217252396166136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '416824', 'license': 'MIT', 'n_tokens_mistral': 742, 'n_tokens_neox': 724, 'n_words': 330, 'path': '02_StacksAndQueues/StacksAndQueues.ipynb', 'repo_name': 'HKuz/PythonAlgos', 'size': '2767'}