read violation resolved (#1496)

This commit is contained in:
GeorgianaK 2021-05-25 08:22:52 +03:00 committed by GitHub
parent a9d33871a9
commit 5ba8bc2c86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,34 +2,27 @@
struct node { struct node {
int data; int data;
struct node *next; struct node* next;
}; };
class Queue { class Queue {
node *front; node* front=nullptr;
node *rear; node* rear=nullptr;
public: public:
Queue() { Queue() = default;
front = NULL;
rear = NULL;
}
void createNode(int val) { void createNode(int val) {
node *ptr; auto* nn = new node;
node *nn;
nn = new node;
ptr = front;
nn->data = val; nn->data = val;
nn->next = NULL; nn->next = nullptr;
front = nn; front = nn;
rear = nn; rear = nn;
} }
void enqueue(int val) { void enqueue(int val) {
if (front == NULL || rear == NULL) { if (front == nullptr || rear == nullptr) {
createNode(val); createNode(val);
} else { }
node *ptr; else {
node *nn; node* nn;
ptr = front;
nn = new node; nn = new node;
nn->data = val; nn->data = val;
rear->next = nn; rear->next = nn;
@ -38,19 +31,23 @@ class Queue {
} }
} }
void dequeue() { void dequeue() {
node *n; node* n;
n = front; n = front;
front = front->next; if (n) {
delete (n); front = front->next;
delete n;
}
} }
void traverse() { void traverse() {
node *ptr; node* ptr;
ptr = front; ptr = front;
do { if (ptr) {
std::cout << ptr->data << " "; do {
ptr = ptr->next; std::cout << ptr->data << " ";
} while (ptr != rear->next); ptr = ptr->next;
std::cout << front->data << std::endl; } while (ptr != rear->next);
std::cout << front->data << std::endl;
}
} }
}; };
int main(void) { int main(void) {