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,20 +31,24 @@ class Queue {
} }
} }
void dequeue() { void dequeue() {
node *n; node* n;
n = front; n = front;
if (n) {
front = front->next; front = front->next;
delete (n); delete n;
}
} }
void traverse() { void traverse() {
node *ptr; node* ptr;
ptr = front; ptr = front;
if (ptr) {
do { do {
std::cout << ptr->data << " "; std::cout << ptr->data << " ";
ptr = ptr->next; ptr = ptr->next;
} while (ptr != rear->next); } while (ptr != rear->next);
std::cout << front->data << std::endl; std::cout << front->data << std::endl;
} }
}
}; };
int main(void) { int main(void) {
Queue q; Queue q;