From 6cbc1a5d43661d8f1f5e20586e3d46dd26de836e Mon Sep 17 00:00:00 2001 From: NyancheraMakaye Date: Sat, 31 Aug 2024 23:51:18 +0300 Subject: [PATCH] Implemented a stack data structure to reverse a string,implemented a queue using two stacks,Implemented a sinly linked list and found the maximum element in the list. --- task1.py | 39 +++++++++++++++++++++++++++++++++++++++ task2.py | 30 ++++++++++++++++++++++++++++++ task3.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 task1.py create mode 100644 task2.py create mode 100644 task3.py diff --git a/task1.py b/task1.py new file mode 100644 index 0000000..8c753d1 --- /dev/null +++ b/task1.py @@ -0,0 +1,39 @@ +class Stack: + def __init__(self): + self.items = [] + + def push(self, item): + self.items.append(item) + + def pop(self): + if not self.is_empty(): + return self.items.pop() + else: + return None + + def is_empty(self): + return len(self.items) == 0 + +def reverse_string(s: str) -> str: + """ + Reverses a string using a stack data structure. + + Args: + s (str): The input string to be reversed. + + Returns: + str: The reversed string. + """ + stack = Stack() + for char in s: + stack.push(char) + + reversed_s = "" + while not stack.is_empty(): + reversed_s += stack.pop() + + return reversed_s + +# Example usage: +input_str = "Hello, World!" +print(reverse_string(input_str)) # Output: "!dlroW ,olleH" \ No newline at end of file diff --git a/task2.py b/task2.py new file mode 100644 index 0000000..64e9e88 --- /dev/null +++ b/task2.py @@ -0,0 +1,30 @@ +class QueueWithStacks: + def __init__(self): + # Initialize two empty stacks + self.stack1 = [] + self.stack2 = [] + + def enqueue(self, x: int): + # Add an element to the queue by pushing it onto stack1 + self.stack1.append(x) + + def dequeue(self) -> int: + # If both stacks are empty, the queue is empty + if not self.stack1 and not self.stack2: + raise IndexError("Cannot dequeue from an empty queue") + + # If stack2 is empty, pop all elements from stack1 and push them onto stack2 + if not self.stack2: + while self.stack1: + self.stack2.append(self.stack1.pop()) + + # The front element of the queue is now at the top of stack2 + return self.stack2.pop() + +queue = QueueWithStacks() +queue.enqueue(1) +queue.enqueue(2) +queue.enqueue(3) +print(queue.dequeue()) +print(queue.dequeue()) +print(queue.dequeue()) \ No newline at end of file diff --git a/task3.py b/task3.py new file mode 100644 index 0000000..aff59bf --- /dev/null +++ b/task3.py @@ -0,0 +1,45 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, data): + new_node = Node(data) + if not self.head: + self.head = new_node + return + current = self.head + while current.next: + current = current.next + current.next = new_node + + def find_max(self): + """ + Finds the maximum element in the linked list. + + Returns: + int: The maximum element in the list. + """ + if not self.head: + return None + max_element = self.head.data + current = self.head.next + while current: + if current.data > max_element: + max_element = current.data + current = current.next + return max_element + +# Example usage: +linked_list = LinkedList() +linked_list.append(50) +linked_list.append(10) +linked_list.append(37) +linked_list.append(20) +linked_list.append(15) + +print(linked_list.find_max()) \ No newline at end of file