-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTAREA2_ProcesarDatos.cpp
More file actions
99 lines (92 loc) · 2.52 KB
/
TAREA2_ProcesarDatos.cpp
File metadata and controls
99 lines (92 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<iostream>
#include<string>
using namespace std;
//class Node
template <class T>
class Node {
public:
char priority;
T data;
Node<T>* next;
Node(char priority, T data) : priority(priority), data(data), next(nullptr) {}
};
// class Queue para first in first out
template <class T>
class Queue {
private:
Node <T>* head;
Node <T>* tail;
public:
Queue() : head(nullptr), tail(nullptr) {}
void push (T data, char priority) {
Node <T>* newNode = new Node<T>(priority,data);
if (tail) {
tail->next = newNode;
} else {
head = newNode;
}
tail = newNode;
}
// Procesar los elementos en orden de prioridad sin alterar el orden original de la cola
void processByPriority(const string& priorityOrder) {
for (char priority : priorityOrder) {
int count = 0;
int size =this->getSize();
cout<<"procesando prioridad "<<priority<<":"<<endl;
for (int i = 0; i < size; i++) {
Node<T>* current = this->pop();
if (current->priority == priority) {
cout<<current->data<<endl;
delete current; //procesar y eliminar
} else {
//re-queue el elemento
this->push(current->data, current->priority);
delete current; //se eelimina nodo tempora
}
}
cout<<endl;
}
}
//funcion para (FIFO)
Node<T>* pop() {
if (head==nullptr) {
throw runtime_error("La cola esta vacia");
}
Node <T>* temp = head;
head = head->next;
if (!head) {
tail=nullptr; // si la lista queda vacia se actualiza tail
}
return temp;
}
//funcion para saber el tamaño de la cola
int getSize() {
int size = 0;
Node<T>* temp = head;
while (temp) {
size++;
temp = temp->next;
}
return size;
}
//funcion isEmpty para verificar cola vacia
bool isEmpty() const {
return head == nullptr;
}
};
int main () {
Queue<string> cola;
cola.push("Daniel", 'B');
cola.push("Pablo", 'C');
cola.push("Coraline", 'D');
cola.push("Alfonzo", 'A');
cola.push("Lara", 'C');
cola.push("Paula", 'A');
cola.push("Chancho", 'A');
cola.push("Luz", 'A');
cola.push("Daniel", 'E');
//procesarlos
string prioridades = "ABCDE";
cola.processByPriority(prioridades);
return 0;
}