-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTAREA2_ColaDePrioridadSimple.cpp
More file actions
152 lines (134 loc) · 4.81 KB
/
TAREA2_ColaDePrioridadSimple.cpp
File metadata and controls
152 lines (134 loc) · 4.81 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<string>
using namespace std;
//estructura nodo
template<typename T>
struct Node{
int priority;
Node<T>* next;
struct QueueNode {
T data;
QueueNode* next;
};
QueueNode* queueHead;
Node(int p) : priority(p), next(nullptr), queueHead(nullptr) {}
};
//clase de lista enlazada simple
template <typename T>
class SimpleList{
private:
Node<T>* head;
public:
SimpleList() : head(nullptr) {}
~SimpleList() {
while (head) {
Node<T>* temp = head;
head = head->next;
//liberar la cola interna
typename Node<T>::QueueNode* queueTemp = temp->queueHead;
while (queueTemp) {
typename Node<T>::QueueNode* qTemp = queueTemp;
queueTemp = queueTemp->next;
delete qTemp;
}
delete temp;
}
}
//funcion para insertar un nodo de prioridad
void insertPriority(int priority) {
//verificar si la prioridad ya existe
Node<T>* current = head;
while (current) {
if (current->priority == priority) {
cout<<"Prioridad: "<<priority<<" ya existe, no se duplicara\n";
return;
}
current = current->next;
}
Node<T>* newNode = new Node<T>(priority);
//insertar en orden de prioridad
if (!head || head->priority > priority) {
newNode->next = head;
head = newNode;
} else {
Node<T>* current = head;
while (current->next && current->next->priority < priority) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
//funcion para insertar datos en la cola de un nodo especifico
void insertData(int priority, T data) {
Node<T>* current = head;
while (current && current->priority != priority) {
current = current->next;
}
if (current) {
typename Node<T>::QueueNode* newQueueNode = new typename Node<T>::QueueNode{data, nullptr};
if (!current->queueHead) {
current->queueHead = newQueueNode;
} else {
typename Node<T>::QueueNode* queueCurrent = current->queueHead;
while (queueCurrent->next) {
queueCurrent = queueCurrent->next;
}
queueCurrent->next = newQueueNode;
}
} else {
cout<<"Prioridad no encontrada: "<<priority<<endl;
}
}
//funcion para imprimir la lista
void printList() {
Node<T>* current = head;
while (current) {
cout << "Prioridad: " << current->priority <<"\nDatos: ";
typename Node<T>::QueueNode* queueCurrent = current->queueHead;
while (queueCurrent) {
cout << queueCurrent->data << " ";
queueCurrent = queueCurrent->next;
}
cout<<"\n";
current = current->next;
}
}
};
//implementacion
int main() {
//crear lista
SimpleList<string> lista;
//insertar prioridades
lista.insertPriority(2);
lista.insertPriority(1);
lista.insertPriority(3);
//insertar datos en colas internas
lista.insertData(1, "persona 1");
lista.insertData(2, "persona 2");
lista.insertData(3, "persona 3");
lista.insertData(3, "persona 4");
lista.insertData(1, "persona 5");
// intentar agregar datos a una prioridad inexistente
lista.insertData(4, "persona 6"); //prioridad 4 no existe
//imprimir lista inicial
cout << "Lista actual:\n";
lista.printList();
//agregar más prioridades y datos
lista.insertPriority(0); //nueva prioridad 0
lista.insertData(0, "persona 7");
lista.insertData(2, "persona 8");
//insertar datos en una cola interna ya llena
lista.insertData(3, "persona 9");
//lista despues de insersion
cout << "\nLista despues de agregar mas prioridades y datos:\n";
lista.printList();
//caso: agregar prioridad ya existente (sin efecto)
lista.insertPriority(1); //prioridad 1 ya existe, no debe duplicarse
//caso: agregar datos a una prioridad con solo un elemento
lista.insertData(0, "persona 10");
//imprimir lista final
cout << "\nLista final despues de todos los cambios:\n";
lista.printList();
return 0;
}