Witam,
Robie zadanie domowe jednak z nieznanych mi przyczyn program sie sypie. Prosilbym o pomoc w rozwiazaniu. Juz kilka godzin staram sie cos wymyslec, ale program i tak sie wywala.
Ponizej zamieszczam kod.
Cos jest nie tak z kasowaniem, ale nie mam pojecia co to moze byc.
Program powinien wydrukowac:
data1=1
data1=1 data2=2
1 2 3 4 5 6
Del:1 Del:2 Del:3
Natomiast po wydrukowaniu 1 2 3 4 5 6 sie zawiesza.
#include <iostream>
template<typename T>
class Queue {
struct Node {
bool empt;
T data;
Node* next;
};
bool empt;
Node* head;
Node* tail;
public:
Queue() {
head = new Node();
head->data = NULL;
head->next = NULL;
tail = NULL;
empt = true;
}
bool empty() const {
return empt;
}
void put(const T& dat) {
Queue::Node* tmp = new Queue::Node();
tmp->data = dat;
// std::cout << tmp->data << std::endl;
tmp->next = NULL;
if (head->data == NULL) {
head = tmp;
}
if (tail != NULL)
tail->next = tmp;
tail = tmp;
empt = false;
}
T get() {
T result;
Node* tmp;
result = head->data;
if (head->next != NULL){
tmp = head->next;
tail = NULL;
}
else {
empt = true;
}
delete head;
head = tmp;
return result;
}
~Queue() {
Node* tmp = head;
Node* deltmp = NULL;
while (tmp != NULL) {
deltmp = tmp;
tmp = deltmp->next;
std::cout<< "DEL: "<< deltmp->data <<std::endl;
delete deltmp;
}
}
};
int main() {
int data1, data2;
Queue<int>* q = new Queue<int>();
q->put(1);
data1 = q->get();
std::cout << " data1=" << data1 << std::endl;
q->put(1);
q->put(2);
data1 = q->get();
data2 = q->get();
std::cout << " data1=" << data1
<< " data2=" << data2 << std::endl;
q->put(1); q->put(2); q->put(3);
q->put(4); q->put(5); q->put(6);
while (!q->empty()) std::cout << " " << q->get();
std::cout << std::endl;
q->put(1); q->put(2); q->put(3);
delete q;
}
dodanie znacznika <code> dla wyjścia programu - fp