Last active
October 6, 2020 18:56
-
-
Save nestorsgarzonc/3aafea3631d9f978812a202ace61c05f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node: | |
| def __init__(self, item, priority): | |
| """ | |
| Clase elemento en el cual se almacena la prioridad y el elemento | |
| item: Dynamic | |
| priority: Int | |
| """ | |
| self.item = item | |
| self.priority = priority | |
| class PriorityQueue(): | |
| def __init__(self): | |
| """ | |
| Iniciamos el PriorityQueue usando una pila implementada por medio de un arrreglo | |
| """ | |
| self.head = [] | |
| self.counter = 0 | |
| def append(self, value, priority): | |
| """ | |
| Se inserta un nuevo elemento, se reordena según la prioridad de los elementos | |
| """ | |
| self.head.append(Node(value, priority)) | |
| self.counter += 1 | |
| def extract_max(self): | |
| """ Se otiene el valor de mayor prioridad en la cola """ | |
| max_index = -1 | |
| max_element = None | |
| for i in self.head: | |
| if i.priority > max_index: | |
| max_index = i.priority | |
| max_element = i | |
| self.head = [i for i in self.head if i != max_element] | |
| return f'Priority: {max_element.priority} Element: {max_element.item}' | |
| myList = PriorityQueue() | |
| myList.append('Sebastian Garzon', 2) | |
| myList.append('Jaider Pinto', 7) | |
| myList.append('David Zambrano', 5) | |
| myList.append('Juan Andrés', 1) | |
| print(myList.extract_max()) | |
| print(myList.extract_max()) | |
| print(myList.extract_max()) | |
| print(myList.extract_max()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Implementacion de Priority Queue | |
| Grupo 7 estructuras de datos | |
| Universidad Nacional de Colombia | |
| """ | |
| class Node: | |
| def __init__(self, data, priority): | |
| """ | |
| Clase elemento en el cual se almacena la prioridad y el elemento | |
| data: Dynamic | |
| priority: Int | |
| """ | |
| self.data = data | |
| self.priority = priority | |
| self.next = None | |
| class Stack: | |
| def __init__(self): | |
| self.head = None | |
| def append(self, data, priority): | |
| if self.head == None: | |
| self.head = Node(data, priority) | |
| else: | |
| temp = self.head | |
| self.head = Node(data, priority) | |
| self.head.next = temp | |
| def pop(self): | |
| if self.head == None: | |
| return None | |
| if self.head.next == None: | |
| temp = self.head | |
| self.head = None | |
| return temp | |
| temp = self.head | |
| self.head = self.head.next | |
| return temp | |
| class PriorityQueue: | |
| def __init__(self): | |
| """ | |
| Iniciamos el PriorityQueue usando una pila implementada por medio de un arrreglo | |
| """ | |
| self.node = Stack() | |
| def append(self, value, priority): | |
| """ | |
| Se inserta un nuevo elemento, se reordena según la prioridad de los elementos | |
| """ | |
| self.node.append(value, priority) | |
| def extract_max(self): | |
| """ Se otiene el valor de mayor prioridad en la cola """ | |
| max_index = -1 | |
| max_element = None | |
| temp = self.node.head | |
| while temp != None: | |
| prior = temp.priority | |
| if prior > max_index: | |
| max_index = prior | |
| max_element = temp | |
| temp = temp.next | |
| temp_queue = Stack() | |
| temp = self.node.head | |
| while temp != None: | |
| if temp != max_element: | |
| temp_queue.append(temp.data, temp.priority) | |
| temp = temp.next | |
| self.node = temp_queue | |
| return f'Priority: {max_element.priority} Element: {max_element.data}' | |
| myList = PriorityQueue() | |
| myList.append('Sebastian Garzon', 2) | |
| myList.append('Jaider Pinto', 7) | |
| myList.append('David Zambrano', 5) | |
| myList.append('Juan Andrés', 1) | |
| print(myList.extract_max()) | |
| print(myList.extract_max()) | |
| print(myList.extract_max()) | |
| print(myList.extract_max()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment