Untitled
shinta0x01
python
8 months ago
15 kB
15
Indexable
Never
class Node: def __init__(self, task_name, priority=None): self.task_name = task_name self.priority = priority self.next = None class Priority: def __init__(self): self.front = self.rear = None def add_task(self, task_name, priority=None): node = Node(task_name, priority) if self.front is None or self.rear is None: self.front = self.rear = node return if priority is None or self.rear.priority and priority > self.rear.priority: self.rear.next = node self.rear = node return if self.front is None or self.front.priority and priority < self.front.priority: node.next = self.front self.front = node return current = self.front while current.next: if current.next.priority is None or priority <= current.next.priority: break current = current.next temp = current.next current.next = node node.next = temp def get_task(self): current = self.front while current.next: print(current.task_name, current.priority, ' ', end='') current = current.next def change_task(self, task_name, new_priority): current = self.front if self.front.task_name == task_name: self.front = self.front.next self.add_task(task_name, new_priority) else: while current.task_name != task_name: pre_current = current current = current.next if self.rear.task_name == task_name: pre_current.next = None self.rear = pre_current else: pre_current.next = current.next self.add_task(task_name, new_priority) p = Priority() p.add_task(3, 4) p.add_task(1, 2) p.add_task(5, 6) p.add_task(4, 9) p.add_task(7, 1) p.change_task(4, 3) p.get_task() ####### concatenate, palindrome class Node: def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): self.top = None def push(self, data): node = Node(data) node.next = self.top self.top = node def pop(self): popped_data = self.top.data self.top = self.top.next return popped_data def peek(self): if self.top is None: return None else: return self.top.data def is_palindrome(self, str1): for i in str1: self.push(i) for i in str1: if i == self.peek(): self.pop() else: self.pop() return False return True def backtrack(self): current = self.top while current: if current.data < 0: for i in range(5): self.pop() current = current.next def concatenate(self, other_stack): if other_stack.top is None: return # Traverse to the end of the current stack current = self.top while current.next: current = current.next # Concatenate the other stack to the end of the current stack current.next = other_stack.top # Clear the other stack after concatenation other_stack.top = None def display(self): current = self.top while current: print(current.data, '', end='') current = current.next def min(self): current = self.top minimum = self.top.data while current: if minimum < current.data: minimum = minimum else: minimum = current.data current = current.next return minimum def max(self): current = self.top maximum = self.top.data while current: if maximum > current.data: maximum = maximum else: maximum = current.data current = current.next return maximum def is_empty(self): return self.top is None def size(self): count = 0 current = self.top while current: count += 1 current = current.next return count def clear(self): self.top = None def search(self, data): current = self.top position = 0 while current: if current.data == data: return position position += 1 current = current.next return None def reverse(self): temp_stack = Stack() while self.top: temp_stack.push(self.pop()) self.top = temp_stack.top def copy(self): copied_stack = Stack() current = self.top while current is not None: copied_stack.push(current.data) current = current.next return copied_stack stack = Stack() stack1 = Stack() stack.push(0) stack.push(2) stack.push(4) stack.push(8) stack.display() print() print('Minimum', stack.min()) print('Maximum', stack.max()) ######## Is Balance class Node: def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): self.top = None def push(self, data): node = Node(data) node.next = self.top self.top = node def pop(self): if self.top is None: return None popped_data = self.top.data self.top = self.top.next return popped_data def peek(self): if self.top is None: return None else: return self.top.data def is_balanced_parentheses(s): stack = Stack() # Iterate through each character in the string for char in s: if char in "({[": stack.push(char) elif char in ")}]": # Check if the stack is empty (unmatched closing parenthesis) if stack.peek() is None: return False # Check if the current closing parenthesis matches the top of the stack if (char == ")" and stack.peek() == "(") or \ (char == "}" and stack.peek() == "{") or \ (char == "]" and stack.peek() == "["): stack.pop() else: return False # Check if there are unmatched opening parentheses return stack.peek() is None # Test cases print(is_balanced_parentheses("()")) # True print(is_balanced_parentheses("()[]{}")) # True print(is_balanced_parentheses("(]")) # False print(is_balanced_parentheses("([)]")) # False print(is_balanced_parentheses("{[]}")) # True ##### infix to postfix class Node: def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): self.top = None def push(self, data): node = Node(data) node.next = self.top self.top = node def pop(self): popped = self.top.data self.top = self.top.next return popped def peek(self): if self.top is None: return None else: return self.top.data def display(self): current = self.top while current: print(current.data, '', end='') current = current.next def is_balance(self, str1): open_brackets = '[{(' close_brackets = ']})' for i in str1: if i in open_brackets: self.push(i) elif i in close_brackets: if i == ')' and self.peek() == '(' or i == '}' and self.peek() == '{' or i == ']' and self.peek() == '[': self.pop() else: return False if self.peek() is None: return True else: return False def remove_wspace(self, str1): new = '' for i in str1: if i != ' ': new += i return new def infix_postfix(self, str1): if self.is_balance(str1): nospace = self.remove_wspace(str1) operator = '+-*/()^' expression = '' for i in nospace: if i in operator: if (i == '-' or i == '+') and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'): while self.peek() is not None and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'): temp = self.pop() expression += temp if (i == '-' or i == '+') and (self.peek() == '-' or self.peek() == '+'): while self.peek() is not None and (self.peek() == '-' or self.peek() == '+'): temp = self.pop() expression += temp self.push(i) elif i == ')': while self.peek() is not None and self.peek() != '(': temp = self.pop() expression += temp self.pop() # Pop the open parenthesis elif (i == '-' or i == '+') and (self.peek() == '-' or self.peek() == '+'): while self.peek() is not None and (self.peek() == '-' or self.peek() == '+'): temp = self.pop() expression += temp self.push(i) elif (i == '*' or i == '/') and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'): while self.peek() is not None and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'): temp = self.pop() expression += temp self.push(i) elif i == '^' and self.peek() == '^': self.push(i) else: self.push(i) else: expression += i while self.peek() is not None: temp = self.pop() expression += temp return expression else: print('Not balance') return False stack = Stack() print(stack.infix_postfix('((f*g-h)+(i*(j/k-l))+(m*n/o)')) ###### infix to postfix class Node: def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): self.top = None def push(self, data): node = Node(data) node.next = self.top self.top = node def pop(self): popped = self.top.data self.top = self.top.next return popped def peek(self): if self.top is None: return None else: return self.top.data def display(self): current = self.top while current: print(current.data, '', end='') current = current.next def is_balance(self, str1): open_brackets = '[{(' close_brackets = ']})' for i in str1: if i in open_brackets: self.push(i) elif i in close_brackets: if i == ')' and self.peek() == '(' or i == '}' and self.peek() == '{' or i == ']' and self.peek() == '[': self.pop() else: return False if self.peek() is None: return True else: return False def reverse(self, str1): new = '' prev = '' for i in str1: if prev == ')' and i == '(': new = i + '*' +new else: new = i + new prev = i return new def remove_space(self, str1): new = '' for i in str1: if i != ' ': new += i return new def infix_prefix(self, str1): if self.is_balance(str1): final = self.remove_space(str1) reverse = self.reverse(final) operator = '+-*/()^[]{}' expression = '' for i in reverse: if i in operator: if (i == '-' or i == '+') and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'): while self.peek() == '*' or self.peek() == '/' or self.peek() == '^': temp = self.pop() expression += temp self.push(i) elif (i == '*' or i == '/' or i == '^') and (self.peek() == '^'): temp = self.pop() expression += temp self.push(i) elif i == '(': while self.peek() != ')': temp = self.pop() expression += temp self.pop() elif i == '[': while self.peek() != ']': temp = self.pop() expression += temp self.pop() elif i == '{': while self.peek() != '}': temp = self.pop() expression += temp self.pop() else: self.push(i) else: expression += i while self.peek() is not None: temp = self.pop() expression += temp return self.reverse(expression) else: print('Not balance') return False stack = Stack() print('Infix to Prefix\n', stack.infix_prefix('((a+b)*c)-(d*e/(f+g))-(h*i)')) stack.display()
Leave a Comment